static void testOrderedInserts(MprTestGroup *gp) { MprList *lp; int i, item, next; lp = mprCreateList(0, 0); tassert(lp != 0); /* Add items such that the final list is ordered */ mprAddItem(lp, (void*) (long) 4); mprAddItem(lp, (void*) (long) 5); mprInsertItemAtPos(lp, 0, (void*) 2); mprInsertItemAtPos(lp, 0, (void*) 1); mprInsertItemAtPos(lp, 2, (void*) 3); mprAddItem(lp, (void*) (long) 6); i = 1; next = 0; item = (int) (long) mprGetNextItem(lp, &next); while (item > 0) { tassert(item == i); i++; item = (int) (long) mprGetNextItem(lp, &next); } }
PUBLIC void httpCreateRxPipeline(HttpConn *conn, HttpRoute *route) { HttpTx *tx; HttpRx *rx; HttpQueue *q; HttpStage *stage, *filter; int next; assert(conn); assert(route); rx = conn->rx; tx = conn->tx; rx->inputPipeline = mprCreateList(-1, MPR_LIST_STABLE); if (route) { for (next = 0; (filter = mprGetNextItem(route->inputStages, &next)) != 0; ) { if (matchFilter(conn, filter, route, HTTP_STAGE_RX) == HTTP_ROUTE_OK) { mprAddItem(rx->inputPipeline, filter); } } } mprAddItem(rx->inputPipeline, tx->handler ? tx->handler : conn->http->clientHandler); /* Create the incoming queue heads and open the queues. */ q = tx->queue[HTTP_QUEUE_RX]; for (next = 0; (stage = mprGetNextItem(rx->inputPipeline, &next)) != 0; ) { q = httpCreateQueue(conn, stage, HTTP_QUEUE_RX, q); } if (httpClientConn(conn)) { pairQueues(conn); openQueues(conn); } }
static MprList *expandWild(Ejs *ejs, int argc, char **argv) { MprList *list; EjsArray *files; EjsPath *path, *dir; cchar *arg; int i, j; if ((list = mprCreateList(-1, 0)) == 0) { return 0; } for (i = 0; i < argc; i++) { if (schr(argv[i], '*')) { arg = mprNormalizePath(argv[i]); dir = ejsCreatePathFromAsc(ejs, mprGetPathDir(arg)); path = ejsCreatePathFromAsc(ejs, mprGetPathBase(arg)); if ((files = ejsGetPathFiles(ejs, dir, 1, (EjsObj**) (void*) &path)) == 0) { ejsClearException(ejs); mprAddItem(list, sclone(argv[i])); } else { for (j = 0; j < files->length; j++) { mprAddItem(list, ((EjsPath*) files->data[j])->value); } } } else { mprAddItem(list, sclone(argv[i])); } } return list; }
static void processing() { MprThread *tp; ThreadData *data; int j; if (app->chunkSize > 0) { mprAddItem(app->headers, mprCreateKeyPair("X-Appweb-Chunk-Size", sfmt("%d", app->chunkSize))); } app->activeLoadThreads = app->loadThreads; app->threadData = mprCreateList(app->loadThreads, 0); for (j = 0; j < app->loadThreads; j++) { char name[64]; if ((data = mprAllocObj(ThreadData, manageThreadData)) == 0) { return; } mprAddItem(app->threadData, data); mprSprintf(name, sizeof(name), "http.%d", j); tp = mprCreateThread(name, threadMain, NULL, 0); tp->data = data; mprStartThread(tp); } }
PUBLIC EjsXML *ejsAppendToXML(Ejs *ejs, EjsXML *xml, EjsXML *node) { EjsXML *elt; int next; if (xml == 0 || node == 0) { return 0; } if (xml->elements == 0) { xml->elements = mprCreateList(-1, 0); } if (node->kind == EJS_XML_LIST) { for (next = 0; (elt = mprGetNextItem(node->elements, &next)) != 0; ) { if (xml->kind != EJS_XML_LIST) { elt->parent = xml; } mprAddItem(xml->elements, elt); } xml->targetObject = node->targetObject; xml->targetProperty = node->targetProperty; } else { if (xml->kind != EJS_XML_LIST) { node->parent = xml; } mprAddItem(xml->elements, node); } return xml; }
void httpCreateRxPipeline(HttpConn *conn, HttpRoute *route) { HttpTx *tx; HttpRx *rx; HttpQueue *q; HttpStage *stage, *filter; int next; mprAssert(conn); mprAssert(route); rx = conn->rx; tx = conn->tx; rx->inputPipeline = mprCreateList(-1, 0); if (route) { for (next = 0; (filter = mprGetNextItem(route->inputStages, &next)) != 0; ) { if (matchFilter(conn, filter, route, HTTP_STAGE_RX) == HTTP_ROUTE_OK) { mprAddItem(rx->inputPipeline, filter); } } } mprAddItem(rx->inputPipeline, tx->handler); /* Create the incoming queue heads and open the queues. */ q = tx->queue[HTTP_QUEUE_RX]; for (next = 0; (stage = mprGetNextItem(rx->inputPipeline, &next)) != 0; ) { q = httpCreateQueue(conn, stage, HTTP_QUEUE_RX, q); } conn->readq = tx->queue[HTTP_QUEUE_RX]->prevQ; if (!conn->endpoint) { pairQueues(conn); openQueues(conn); } }
static void testInsertAndRemove(MprTestGroup *gp) { MprList *lp; int index; lp = mprCreateList(0, 0); tassert(lp != 0); /* Do one insert and remove */ index = mprAddItem(lp, (void*) 1); tassert(index >= 0); tassert(mprGetListLength(lp) == 1); mprRemoveItem(lp, (void*) 1); tassert(mprGetListLength(lp) == 0); /* Test remove will compact */ mprAddItem(lp, (void*) 1); mprAddItem(lp, (void*) 2); mprAddItem(lp, (void*) 3); mprRemoveItem(lp, (void*) 2); tassert(mprGetListLength(lp) == 2); mprRemoveItem(lp, (void*) 3); tassert(mprGetListLength(lp) == 1); }
static void getDepends(Ejs *ejs, MprList *list, EjsModule *mp) { EjsModule *module; int next; if (mprLookupItem(list, mp) < 0) { mprAddItem(list, mp); } for (next = 0; (module = mprGetNextItem(mp->dependencies, &next)) != 0; ) { if (mprLookupItem(list, module) < 0) { mprAddItem(list, module); } } }
static int blendEnv(MprCmd *cmd, cchar **env, int flags) { cchar **ep, *prior; int next; cmd->env = 0; if ((cmd->env = mprCreateList(128, MPR_LIST_STATIC_VALUES | MPR_LIST_STABLE)) == 0) { return MPR_ERR_MEMORY; } #if !VXWORKS /* Add prior environment to the list */ if (!(flags & MPR_CMD_EXACT_ENV)) { for (ep = (cchar**) environ; ep && *ep; ep++) { #if MACOSX if (sstarts(*ep, "DYLD_LIBRARY_PATH=")) { continue; } #endif mprAddItem(cmd->env, *ep); } } #endif /* Add new env keys. Detect and overwrite duplicates */ for (ep = env; ep && *ep; ep++) { prior = 0; for (ITERATE_ITEMS(cmd->env, prior, next)) { if (matchEnvKey(*ep, prior)) { mprSetItem(cmd->env, next - 1, *ep); break; } } if (prior == 0) { mprAddItem(cmd->env, *ep); } } #if ME_WIN_LIKE /* Windows requires a caseless sort with two trailing nulls */ mprSortList(cmd->env, (MprSortProc) sortEnv, 0); #endif mprAddItem(cmd->env, NULL); return 0; }
MprCmd *mprCreateCmd(MprCtx ctx) { MprCmdService *cs; MprCmd *cmd; MprCmdFile *files; int i; cmd = mprAllocObjWithDestructorZeroed(ctx, MprCmd, cmdDestructor); if (cmd == 0) { return 0; } cmd->completeCond = mprCreateCond(cmd); cmd->timeoutPeriod = MPR_TIMEOUT_CMD; cmd->timestamp = mprGetTime(cmd); cmd->forkCallback = (MprForkCallback) closeFiles; #if VXWORKS cmd->startCond = semCCreate(SEM_Q_PRIORITY, SEM_EMPTY); cmd->exitCond = semCCreate(SEM_Q_PRIORITY, SEM_EMPTY); #endif files = cmd->files; for (i = 0; i < MPR_CMD_MAX_PIPE; i++) { files[i].clientFd = -1; files[i].fd = -1; } #if BLD_FEATURE_MULTITHREAD cmd->mutex = mprCreateLock(cmd); #endif cs = mprGetMpr(ctx)->cmdService; mprLock(cs->mutex); mprAddItem(cs->cmds, cmd); mprUnlock(cs->mutex); return cmd; }
static void require(cchar *name) { assert(app->modules); if (name && *name) { mprAddItem(app->modules, sclone(name)); } }
int maInsertAlias(MaHost *host, MaAlias *newAlias) { MaAlias *alias, *old; int rc, next, index; if (mprGetParent(host->aliases) == host->parent) { host->aliases = mprDupList(host, host->parent->aliases); } /* * Sort in reverse collating sequence. Must make sure that /abc/def sorts before /abc. But we sort redirects with * status codes first. */ for (next = 0; (alias = mprGetNextItem(host->aliases, &next)) != 0; ) { rc = strcmp(newAlias->prefix, alias->prefix); if (rc == 0) { index = mprLookupItem(host->aliases, alias); old = (MaAlias*) mprGetItem(host->aliases, index); mprRemoveItem(host->aliases, alias); mprInsertItemAtPos(host->aliases, next - 1, newAlias); return 0; } else if (rc > 0) { if (newAlias->redirectCode >= alias->redirectCode) { mprInsertItemAtPos(host->aliases, next - 1, newAlias); return 0; } } } mprAddItem(host->aliases, newAlias); return 0; }
PUBLIC MprList *mprListRegistry(cchar *key) { HKEY top, h; wchar name[ME_MAX_PATH]; MprList *list; int index, size; assert(key && *key); /* Get the registry hive */ if ((key = getHive(key, &top)) == 0) { return 0; } if (RegOpenKeyEx(top, wide(key), 0, KEY_READ, &h) != ERROR_SUCCESS) { return 0; } list = mprCreateList(0, 0); index = 0; while (1) { size = sizeof(name) / sizeof(wchar); if (RegEnumValue(h, index, name, &size, 0, NULL, NULL, NULL) != ERROR_SUCCESS) { break; } mprAddItem(list, sclone(multi(name))); index++; } RegCloseKey(h); return list; }
int maInsertDir(MaHost *host, MaDir *newDir) { MaDir *dir; int rc, next; mprAssert(newDir); mprAssert(newDir->path); if (mprGetParent(host->dirs) == host->parent) { host->dirs = mprDupList(host, host->parent->dirs); } /* * Sort in reverse collating sequence. Must make sure that /abc/def sorts before /abc */ for (next = 0; (dir = mprGetNextItem(host->dirs, &next)) != 0; ) { mprAssert(dir->path); rc = strcmp(newDir->path, dir->path); if (rc == 0) { mprRemoveItem(host->dirs, dir); mprInsertItemAtPos(host->dirs, next, newDir); return 0; } else if (rc > 0) { mprInsertItemAtPos(host->dirs, next - 1, newDir); return 0; } } mprAddItem(host->dirs, newDir); return 0; }
static int mdbAddValidation(Edi *edi, cchar *tableName, cchar *columnName, EdiValidation *vp) { Mdb *mdb; MdbTable *table; MdbCol *col; mprAssert(edi); mprAssert(tableName && *tableName); mprAssert(columnName && *columnName); mdb = (Mdb*) edi; lock(mdb); if ((table = lookupTable(mdb, tableName)) == 0) { unlock(mdb); return MPR_ERR_CANT_FIND; } if ((col = lookupColumn(table, columnName)) == 0) { unlock(mdb); return MPR_ERR_CANT_FIND; } if (col->validations == 0) { col->validations = mprCreateList(0, 0); } mprAddItem(col->validations, vp); unlock(mdb); return 0; }
/* Return a list of column names */ static MprList *mdbGetColumns(Edi *edi, cchar *tableName) { Mdb *mdb; MdbTable *table; MdbSchema *schema; MprList *list; int i; mprAssert(edi); mprAssert(tableName && *tableName); mdb = (Mdb*) edi; lock(mdb); if ((table = lookupTable(mdb, tableName)) == 0) { unlock(mdb); return 0; } schema = table->schema; mprAssert(schema); list = mprCreateList(schema->ncols, 0); for (i = 0; i < schema->ncols; i++) { /* No need to clone */ mprAddItem(list, schema->cols[i].name); } unlock(mdb); return list; }
static int mdbAddTable(Edi *edi, cchar *tableName) { Mdb *mdb; MdbTable *table; mprAssert(edi); mprAssert(tableName && *tableName); mdb = (Mdb*) edi; lock(mdb); if ((table = lookupTable(mdb, tableName)) != 0) { unlock(mdb); return MPR_ERR_ALREADY_EXISTS; } if ((table = mprAllocObj(MdbTable, manageTable)) == 0) { unlock(mdb); return MPR_ERR_MEMORY; } if ((table->rows = mprCreateList(0, 0)) == 0) { unlock(mdb); return MPR_ERR_MEMORY; } table->name = sclone(tableName); if (mdb->tables == 0) { mdb->tables = mprCreateList(0, 0); } if (!growSchema(table)) { unlock(mdb); return MPR_ERR_MEMORY; } mprAddItem(mdb->tables, table); autoSave(mdb, lookupTable(mdb, tableName)); unlock(mdb); return 0; }
static EjsXML *shallowCopy(Ejs *ejs, EjsXML *xml) { EjsXML *root, *elt; int next; assure(xml->kind == EJS_XML_LIST); if (xml == 0) { return 0; } if ((root = ejsCreateXMLList(ejs, xml->targetObject, xml->targetProperty)) == NULL) { return 0; } if (xml->elements) { root->elements = mprCreateList(-1, 0); for (next = 0; (elt = mprGetNextItem(xml->elements, &next)) != 0; ) { assure(ejsIsXML(ejs, elt)); if (elt) { mprAddItem(root->elements, elt); } } } if (mprHasMemError(ejs)) { return 0; } return root; }
int maAddLocation(MaHost *host, MaLocation *newLocation) { MaLocation *location; int next, rc; mprAssert(newLocation); mprAssert(newLocation->prefix); if (mprGetParent(host->locations) == host->parent) { host->locations = mprDupList(host, host->parent->locations); } /* * Sort in reverse collating sequence. Must make sure that /abc/def sorts before /abc */ for (next = 0; (location = mprGetNextItem(host->locations, &next)) != 0; ) { rc = strcmp(newLocation->prefix, location->prefix); if (rc == 0) { mprRemoveItem(host->locations, location); mprInsertItemAtPos(host->locations, next - 1, newLocation); return 0; } if (strcmp(newLocation->prefix, location->prefix) > 0) { mprInsertItemAtPos(host->locations, next - 1, newLocation); return 0; } } mprAddItem(host->locations, newLocation); return 0; }
/* String to list. This parses the string into space separated arguments. Single and double quotes are supported. This returns a stable list. */ PUBLIC MprList *stolist(cchar *src) { MprList *list; cchar *start; int quote; list = mprCreateList(0, MPR_LIST_STABLE); while (src && *src != '\0') { while (isspace((uchar) *src)) { src++; } if (*src == '\0') { break; } for (quote = 0, start = src; *src; src++) { if (*src == '\\') { src++; } else if (*src == '"' || *src == '\'') { if (*src == quote) { src++; break; } else if (quote == 0) { quote = *src; } } else if (isspace((uchar) *src) && !quote) { break; } } mprAddItem(list, snclone(start, src - start)); } return list; }
static int runCommand(HttpConn *conn, cchar *command, cchar *csource, cchar *module) { EspReq *req; EspRoute *eroute; MprCmd *cmd; MprKey *var; MprList *elist; cchar **env; char *err, *out; req = conn->data; eroute = req->route->eroute; cmd = mprCreateCmd(conn->dispatcher); if ((req->commandLine = espExpandCommand(eroute, command, csource, module)) == 0) { httpError(conn, HTTP_CODE_INTERNAL_SERVER_ERROR, "Missing EspCompile directive for %s", csource); return MPR_ERR_CANT_READ; } mprLog(4, "ESP command: %s\n", req->commandLine); if (eroute->env) { elist = mprCreateList(0, 0); for (ITERATE_KEYS(eroute->env, var)) { mprAddItem(elist, sfmt("%s=%s", var->key, var->data)); } mprAddNullItem(elist); env = (cchar**) &elist->items[0]; } else {
PUBLIC MprModule *mprCreateModule(cchar *name, cchar *path, cchar *entry, void *data) { MprModuleService *ms; MprModule *mp; int index; ms = MPR->moduleService; assert(ms); if ((mp = mprAllocObj(MprModule, manageModule)) == 0) { return 0; } mp->name = sclone(name); if (path && *path) { mp->path = sclone(path); } if (entry && *entry) { mp->entry = sclone(entry); } mp->moduleData = data; mp->lastActivity = mprGetTicks(); index = mprAddItem(ms->modules, mp); if (index < 0 || mp->name == 0) { return 0; } return mp; }
static int runCommand(HttpRoute *route, MprDispatcher *dispatcher, cchar *command, cchar *csource, cchar *module, char **errMsg) { MprCmd *cmd; MprKey *var; MprList *elist; EspRoute *eroute; cchar **env, *commandLine; char *err, *out; int rc; *errMsg = 0; eroute = route->eroute; cmd = mprCreateCmd(dispatcher); if ((commandLine = espExpandCommand(route, command, csource, module)) == 0) { *errMsg = sfmt("Missing EspCompile directive for %s", csource); return MPR_ERR_CANT_READ; } mprTrace(4, "ESP command: %s\n", commandLine); if (eroute->env) { elist = mprCreateList(0, MPR_LIST_STABLE); for (ITERATE_KEYS(eroute->env, var)) { mprAddItem(elist, sfmt("%s=%s", var->key, var->data)); } mprAddNullItem(elist); env = (cchar**) &elist->items[0]; } else {
void httpAddHostToEndpoint(HttpEndpoint *endpoint, HttpHost *host) { mprAddItem(endpoint->hosts, host); if (endpoint->limits == 0) { endpoint->limits = host->defaultRoute->limits; } }
/* * Convenience function to create a new default host */ MaHost *maCreateDefaultHost(MaServer *server, cchar *docRoot, cchar *ipAddr, int port) { MaHost *host; MaListen *listen; MaHostAddress *address; if (ipAddr == 0) { /* * If no IP:PORT specified, find the first listening endpoint. In this case, we expect the caller to * have setup the lisenting endponts and to have added them to the host address hash. */ listen = mprGetFirstItem(server->listens); if (listen) { ipAddr = listen->ipAddr; port = listen->port; } else { listen = maCreateListen(server, "localhost", MA_SERVER_DEFAULT_PORT_NUM, 0); maAddListen(server, listen); ipAddr = "localhost"; port = MA_SERVER_DEFAULT_PORT_NUM; } host = maCreateHost(server, ipAddr, NULL); } else { /* * Create a new listening endpoint */ listen = maCreateListen(server, ipAddr, port, 0); maAddListen(server, listen); host = maCreateHost(server, ipAddr, NULL); } if (maOpenMimeTypes(host, "mime.types") < 0) { maAddStandardMimeTypes(host); } /* * Insert the host and create a directory object for the docRoot */ maAddHost(server, host); maInsertDir(host, maCreateBareDir(host, docRoot)); maSetDocumentRoot(host, docRoot); /* * Ensure we are in the hash lookup of all the addresses to listen to acceptWrapper uses this hash to find * the host to serve the request. */ address = maLookupHostAddress(server, ipAddr, port); if (address == 0) { address = maCreateHostAddress(server, ipAddr, port); mprAddItem(server->hostAddresses, address); } maInsertVirtualHost(address, host); if (server->defaultHost == 0) { server->defaultHost = host; } return host; }
static void testListIterate(MprTestGroup *gp) { MprList *lp; int max, i, item, next; max = 50; lp = mprCreateList(max, 0); tassert(lp != 0); for (i = 0; i < max; i++) { mprAddItem(lp, (void*) (long) (i + 1)); } i = next = 0; item = (int) (long) mprGetNextItem(lp, &next); while (item > 0) { i++; item = (int) (long) mprGetNextItem(lp, &next); } tassert(i == max); /* Abbreviated form with no GetFirst */ i = 0; next = 0; while ((item = (int) (long) mprGetNextItem(lp, &next)) != 0) { i++; } tassert(i == max); }
static int processThread(HttpConn *conn, MprEvent *event) { ThreadData *td; cchar *path; char *url; int next; td = mprGetCurrentThread()->data; httpFollowRedirects(conn, !app->nofollow); httpSetTimeout(conn, app->timeout, app->timeout); if (strcmp(app->protocol, "HTTP/1.0") == 0) { httpSetKeepAliveCount(conn, 0); httpSetProtocol(conn, "HTTP/1.0"); } if (app->username) { if (app->password == 0 && !strchr(app->username, ':')) { app->password = getPassword(); } httpSetCredentials(conn, app->username, app->password); } while (!mprShouldDenyNewRequests(conn) && (app->success || app->continueOnErrors)) { if (app->singleStep) waitForUser(); if (app->files && !app->upload) { for (next = 0; (path = mprGetNextItem(app->files, &next)) != 0; ) { /* If URL ends with "/", assume it is a directory on the target and append each file name */ if (app->target[strlen(app->target) - 1] == '/') { url = mprJoinPath(app->target, mprGetPathBase(path)); } else { url = app->target; } app->requestFiles = mprCreateList(-1, MPR_LIST_STATIC_VALUES); mprAddItem(app->requestFiles, path); td->url = url = resolveUrl(conn, url); if (app->verbose) { mprPrintf("putting: %s to %s\n", path, url); } if (doRequest(conn, url, app->requestFiles) < 0) { app->success = 0; break; } } } else { td->url = url = resolveUrl(conn, app->target); if (doRequest(conn, url, app->files) < 0) { app->success = 0; break; } } if (iterationsComplete()) { break; } } httpDestroyConn(conn); finishThread((MprThread*) event->data); return -1; }
static void require(MprList *list, cchar *name) { mprAssert(list); if (name && *name) { mprAddItem(list, name); } }
PUBLIC int ejsAppendAttributeToXML(Ejs *ejs, EjsXML *parent, EjsXML *node) { if (parent->attributes == 0) { parent->attributes = mprCreateList(-1, 0); } node->parent = parent; return mprAddItem(parent->attributes, node); }
PUBLIC EjsAny *cloneXml(Ejs *ejs, EjsXML *xml, bool deep) { EjsXML *root, *elt; int next; if (xml == 0) { return 0; } if (xml->kind == EJS_XML_LIST) { root = ejsCreateXMLList(ejs, xml->targetObject, xml->targetProperty); } else { root = ejsCreateXML(ejs, xml->kind, xml->qname, NULL, xml->value); } if (root == 0) { return 0; } // TODO - must copy inScopeNamespaces? if (xml->attributes) { root->attributes = mprCreateList(-1, 0); for (next = 0; (elt = (EjsXML*) mprGetNextItem(xml->attributes, &next)) != 0; ) { elt = ejsClone(ejs, elt, 1); if (elt) { elt->parent = root; mprAddItem(root->attributes, elt); } } } if (xml->elements) { root->elements = mprCreateList(-1, 0); for (next = 0; (elt = mprGetNextItem(xml->elements, &next)) != 0; ) { assert(ejsIsXML(ejs, elt)); elt = ejsClone(ejs, elt, 1); if (elt) { elt->parent = root; mprAddItem(root->elements, elt); } } } if (mprHasMemError(ejs)) { return 0; } return root; }