static void setHttpPipeline(Ejs *ejs, EjsHttpServer *sp) { EjsString *vs; HttpHost *host; HttpRoute *route; Http *http; HttpStage *stage; cchar *name; int i; assure(sp->endpoint); http = sp->endpoint->http; host = mprGetFirstItem(sp->endpoint->hosts); route = mprGetFirstItem(host->routes); if (sp->outgoingStages) { httpClearRouteStages(route, HTTP_STAGE_TX); for (i = 0; i < sp->outgoingStages->length; i++) { vs = ejsGetProperty(ejs, sp->outgoingStages, i); if (vs && ejsIs(ejs, vs, String)) { name = vs->value; if (httpLookupStage(http, name) == 0) { ejsThrowArgError(ejs, "Cannot find pipeline stage name %s", name); return; } httpAddRouteFilter(route, name, NULL, HTTP_STAGE_TX); } } } if (sp->incomingStages) { httpClearRouteStages(route, HTTP_STAGE_RX); for (i = 0; i < sp->incomingStages->length; i++) { vs = ejsGetProperty(ejs, sp->incomingStages, i); if (vs && ejsIs(ejs, vs, String)) { name = vs->value; if (httpLookupStage(http, name) == 0) { ejsThrowArgError(ejs, "Cannot find pipeline stage name %s", name); return; } httpAddRouteFilter(route, name, NULL, HTTP_STAGE_RX); } } } if (sp->connector) { if ((stage = httpLookupStage(http, sp->connector)) == 0) { ejsThrowArgError(ejs, "Cannot find pipeline stage name %s", sp->connector); return; } route->connector = stage; } }
/* Add cache configuration to the route. This can be called multiple times. Uris, extensions and methods may optionally provide a space or comma separated list of items. If URI is NULL or "*", cache all URIs for this route. Otherwise, cache only the given URIs. The URIs may contain an ordered set of request parameters. For example: "/user/show?name=john&posts=true" Note: the URI should not include the route prefix (scriptName) The extensions should not contain ".". The methods may contain "*" for all methods. */ void httpAddCache(HttpRoute *route, cchar *methods, cchar *uris, cchar *extensions, cchar *types, MprTime clientLifespan, MprTime serverLifespan, int flags) { HttpCache *cache; char *item, *tok; cache = 0; if (!route->caching) { httpAddRouteHandler(route, "cacheHandler", ""); httpAddRouteFilter(route, "cacheFilter", "", HTTP_STAGE_TX); route->caching = mprCreateList(0, 0); } else if (flags & HTTP_CACHE_RESET) { route->caching = mprCreateList(0, 0); } else if (route->parent && route->caching == route->parent->caching) { route->caching = mprCloneList(route->parent->caching); } if ((cache = mprAllocObj(HttpCache, manageHttpCache)) == 0) { return; } if (extensions) { cache->extensions = mprCreateHash(0, 0); for (item = stok(sclone(extensions), " \t,", &tok); item; item = stok(0, " \t,", &tok)) { if (smatch(item, "*")) { extensions = 0; } else { mprAddKey(cache->extensions, item, cache); } } } else if (types) { cache->types = mprCreateHash(0, 0); for (item = stok(sclone(types), " \t,", &tok); item; item = stok(0, " \t,", &tok)) { if (smatch(item, "*")) { extensions = 0; } else { mprAddKey(cache->types, item, cache); } } } if (methods) { cache->methods = mprCreateHash(0, MPR_HASH_CASELESS); for (item = stok(sclone(methods), " \t,", &tok); item; item = stok(0, " \t,", &tok)) { if (smatch(item, "*")) { methods = 0; } else { mprAddKey(cache->methods, item, cache); } } } if (uris) { cache->uris = mprCreateHash(0, 0); for (item = stok(sclone(uris), " \t,", &tok); item; item = stok(0, " \t,", &tok)) { if (flags & HTTP_CACHE_ONLY && route->prefix && !scontains(item, sfmt("prefix=%s", route->prefix))) { /* Auto-add ?prefix=ROUTE_NAME if there is no query */ if (!schr(item, '?')) { item = sfmt("%s?prefix=%s", item, route->prefix); } } mprAddKey(cache->uris, item, cache); } } if (clientLifespan <= 0) { clientLifespan = route->lifespan; } cache->clientLifespan = clientLifespan; if (serverLifespan <= 0) { serverLifespan = route->lifespan; } cache->serverLifespan = serverLifespan; cache->flags = flags; mprAddItem(route->caching, cache); #if UNUSED && KEEP mprLog(3, "Caching route %s for methods %s, URIs %s, extensions %s, types %s, client lifespan %d, server lifespan %d", route->name, (methods) ? methods: "*", (uris) ? uris: "*", (extensions) ? extensions: "*", (types) ? types: "*", cache->clientLifespan / MPR_TICKS_PER_SEC); cache->serverLifespan / MPR_TICKS_PER_SEC); #endif }