/* Open an instance of the ESP for a new request */ static int openEsp(HttpQueue *q) { HttpConn *conn; HttpRx *rx; HttpRoute *rp; EspRoute *eroute; EspReq *req; conn = q->conn; rx = conn->rx; if ((req = mprAllocObj(EspReq, manageReq)) == 0) { httpMemoryError(conn); return MPR_ERR_MEMORY; } /* If unloading a module, this lock will cause a wait here while ESP applications are reloaded. */ lock(esp); esp->inUse++; unlock(esp); /* Find the ESP route configuration. Search up the route parent chain. */ for (eroute = 0, rp = rx->route; rp; rp = rp->parent) { if (rp->eroute) { eroute = rp->eroute; break; } } if (!eroute) { eroute = createEspRoute(rx->route); } rx->route->eroute = eroute; conn->reqData = req; req->esp = esp; req->route = rx->route; req->autoFinalize = 1; /* If a cookie is not explicitly set, use the application name for the session cookie so that cookies are unique per esp application. */ if (!rx->route->cookie) { httpSetRouteCookie(rx->route, sfmt("esp-%s", eroute->appName)); } return 0; }
/* Define login URLs by creating routes. Used by Appweb AuthType directive. Web frameworks like ESP should NOT use this. */ PUBLIC void httpSetAuthFormDetails(HttpRoute *route, cchar *loginPage, cchar *loginService, cchar *logoutService, cchar *loggedInPage, cchar *loggedOutPage) { HttpRoute *loginRoute; HttpAuth *auth; auth = route->auth; if (!route->cookie) { httpSetRouteCookie(route, HTTP_SESSION_COOKIE); } if (loggedInPage) { auth->loggedInPage = sclone(loggedInPage); } if (loginPage) { auth->loginPage = sclone(loginPage); createLoginRoute(route, auth->loginPage, 0); } if (loggedOutPage) { if (smatch(loginPage, loggedOutPage)) { auth->loggedOutPage = auth->loginPage; } else { auth->loggedOutPage = sclone(loggedOutPage); createLoginRoute(route, auth->loggedOutPage, 0); } } /* Put services last so they inherit the auth settings above */ if (loginService) { loginRoute = createLoginRoute(route, loginService, loginServiceProc); httpAddRouteCondition(loginRoute, "auth", 0, 0); } if (logoutService) { createLoginRoute(route, logoutService, logoutServiceProc); } }
/* WARNING: may yield */ PUBLIC int espLoadConfig(HttpRoute *route) { EspRoute *eroute; cchar *name, *package; bool modified; eroute = route->eroute; if (!route->update) { return 0; } package = mprJoinPath(mprGetPathDir(eroute->configFile), "package.json"); modified = 0; ifConfigModified(route, eroute->configFile, &modified); ifConfigModified(route, package, &modified); if (modified) { lock(esp); httpInitConfig(route); #if DEPRECATED || 1 /* Don't reload if configFile == package.json */ if (!mprSamePath(package, eroute->configFile)) { #endif if (mprPathExists(package, R_OK)) { if (httpLoadConfig(route, package) < 0) { unlock(esp); return MPR_ERR_CANT_LOAD; } } } if (httpLoadConfig(route, eroute->configFile) < 0) { unlock(esp); return MPR_ERR_CANT_LOAD; } if ((name = espGetConfig(route, "name", 0)) != 0) { eroute->appName = name; } if (espLoadCompilerRules(route) < 0) { return MPR_ERR_CANT_OPEN; } unlock(esp); } if (!route->cookie) { httpSetRouteCookie(route, sfmt("esp-%s", eroute->appName)); } if (route->database && !eroute->edi) { if (espOpenDatabase(route, route->database) < 0) { mprLog("error esp", 0, "Cannot open database %s", route->database); return MPR_ERR_CANT_LOAD; } } #if !ME_STATIC if (!(route->flags & HTTP_ROUTE_NO_LISTEN)) { MprJson *preload, *item; cchar *errMsg, *source; char *kind; int i; /* WARNING: may yield when compiling modules */ if (eroute->combine) { source = mprJoinPaths(route->home, httpGetDir(route, "CACHE"), sfmt("%s.c", eroute->appName), NULL); } else { source = mprJoinPaths(route->home, httpGetDir(route, "SRC"), "app.c", NULL); } lock(esp); if (espLoadModule(route, NULL, "app", source, &errMsg) < 0) { if (eroute->combine) { mprLog("error esp", 0, "%s", errMsg); unlock(esp); return MPR_ERR_CANT_LOAD; } } if (!eroute->combine && (preload = mprGetJsonObj(route->config, "esp.preload")) != 0) { for (ITERATE_JSON(preload, item, i)) { source = ssplit(sclone(item->value), ":", &kind); if (*kind == '\0') { kind = "controller"; } source = mprJoinPaths(route->home, httpGetDir(route, "CONTROLLERS"), source, NULL); if (espLoadModule(route, NULL, kind, source, &errMsg) < 0) { mprLog("error esp", 0, "Cannot preload esp module %s. %s", source, errMsg); unlock(esp); return MPR_ERR_CANT_LOAD; } } } unlock(esp); }