static int createEndpoints(int argc, char **argv) { cchar *endpoint; char *ip; int argind, port, secure; ip = 0; port = -1; endpoint = 0; argind = 0; if ((app->appweb = maCreateAppweb()) == 0) { mprLog("error appweb", 0, "Cannot create HTTP service"); return MPR_ERR_CANT_CREATE; } if ((app->server = maCreateServer(app->appweb, "default")) == 0) { mprLog("error appweb", 0, "Cannot create HTTP server"); return MPR_ERR_CANT_CREATE; } loadStaticModules(); mprGC(MPR_GC_FORCE | MPR_GC_COMPLETE); if (argind == argc) { if (maParseConfig(app->server, app->configFile, 0) < 0) { return MPR_ERR_CANT_CREATE; } } else { app->documents = sclone(argv[argind++]); if (argind == argc) { if (maConfigureServer(app->server, NULL, app->home, app->documents, NULL, ME_HTTP_PORT, 0) < 0) { return MPR_ERR_CANT_CREATE; } } else while (argind < argc) { endpoint = argv[argind++]; mprParseSocketAddress(endpoint, &ip, &port, &secure, 80); if (maConfigureServer(app->server, NULL, app->home, app->documents, ip, port, 0) < 0) { return MPR_ERR_CANT_CREATE; } } } if (app->workers >= 0) { mprSetMaxWorkers(app->workers); } /* Call any ESP initializers from slink.c */ appwebStaticInitialize(); #if ME_WIN_LIKE writePort(app->server); #elif ME_UNIX_LIKE addSignals(); #endif mprGC(MPR_GC_FORCE | MPR_GC_COMPLETE); return 0; }
/* Create the listening endoints */ static int createEndpoints(int argc, char **argv) { cchar *endpoint; char *ip; int argind, port, secure; ip = 0; port = -1; endpoint = 0; argind = 0; if ((app->appweb = maCreateAppweb()) == 0) { mprError("Cannot create HTTP service for %s", mprGetAppName()); return MPR_ERR_CANT_CREATE; } if ((app->server = maCreateServer(app->appweb, "default")) == 0) { mprError("Cannot create HTTP server for %s", mprGetAppName()); return MPR_ERR_CANT_CREATE; } loadStaticModules(); if (argc > argind) { app->documents = sclone(argv[argind++]); mprLog(2, "Documents %s", app->documents); } if (argind == argc) { if (maParseConfig(app->server, app->configFile, 0) < 0) { return MPR_ERR_CANT_CREATE; } } else { while (argind < argc) { endpoint = argv[argind++]; mprParseSocketAddress(endpoint, &ip, &port, &secure, 80); if (maConfigureServer(app->server, NULL, app->home, app->documents, ip, port) < 0) { return MPR_ERR_CANT_CREATE; } } } return 0; }
int httpSecureEndpointByName(cchar *name, struct MprSsl *ssl) { HttpEndpoint *endpoint; Http *http; char *ip; int port, next, count; http = MPR->httpService; mprParseSocketAddress(name, &ip, &port, -1); if (ip == 0) { ip = ""; } for (count = 0, next = 0; (endpoint = mprGetNextItem(http->endpoints, &next)) != 0; ) { if (endpoint->port <= 0 || port <= 0 || endpoint->port == port) { mprAssert(endpoint->ip); if (*endpoint->ip == '\0' || *ip == '\0' || scmp(endpoint->ip, ip) == 0) { httpSecureEndpoint(endpoint, ssl); count++; } } } return (count == 0) ? MPR_ERR_CANT_FIND : 0; }
/* function listen(endpoint): Void An endpoint can be either a "port" or "ip:port", or null. If hosted, this call does little -- just add to the ejs->httpServers list. */ static EjsVoid *hs_listen(Ejs *ejs, EjsHttpServer *sp, int argc, EjsObj **argv) { HttpEndpoint *endpoint; HttpHost *host; HttpRoute *route; EjsString *address; EjsObj *loc; EjsPath *documents; if (!sp->hosted) { loc = (argc >= 1) ? argv[0] : ESV(null); if (loc != ESV(null)) { address = ejsToString(ejs, loc); // TODO should permit https://IP:PORT mprParseSocketAddress(address->value, &sp->ip, &sp->port, NULL, 0); } else { address = 0; } if (address == 0) { ejsThrowArgError(ejs, "Missing listen endpoint"); return 0; } if (sp->endpoint) { httpDestroyEndpoint(sp->endpoint); sp->endpoint = 0; } /* The endpoint uses the ejsDispatcher. This is VERY important. All connections will inherit this also. This serializes all activity on one dispatcher. */ if ((endpoint = httpCreateEndpoint(sp->ip, sp->port, ejs->dispatcher)) == 0) { ejsThrowIOError(ejs, "Cannot create Http endpoint object"); return 0; } sp->endpoint = endpoint; host = httpCreateHost(NULL); httpSetHostName(host, sfmt("%s:%d", sp->ip, sp->port)); route = httpCreateConfiguredRoute(host, 1); httpAddRouteMethods(route, "DELETE, HEAD, OPTIONS, PUT"); httpAddRouteHandler(route, "ejsHandler", ""); httpSetRouteTarget(route, "run", 0); httpFinalizeRoute(route); httpSetHostDefaultRoute(host, route); httpAddHostToEndpoint(endpoint, host); if (sp->limits) { ejsSetHttpLimits(ejs, endpoint->limits, sp->limits, 1); } if (sp->incomingStages || sp->outgoingStages || sp->connector) { setHttpPipeline(ejs, sp); } if (sp->ssl) { httpSecureEndpoint(endpoint, sp->ssl); } if (sp->name) { httpSetHostName(host, sp->name); } httpSetSoftware(EJS_HTTPSERVER_NAME); httpSetEndpointAsync(endpoint, sp->async); httpSetEndpointContext(endpoint, sp); httpSetEndpointNotifier(endpoint, stateChangeNotifier); /* This is only required when http is using non-ejs handlers and/or filters */ documents = ejsGetProperty(ejs, sp, ES_ejs_web_HttpServer_documents); if (ejsIs(ejs, documents, Path)) { httpSetRouteDocuments(route, documents->value); } #if KEEP // TODO -- what to do with home? // TODO - if removed, then the "home" property should be removed? home = ejsGetProperty(ejs, sp, ES_ejs_web_HttpServer_home); if (ejsIs(ejs, home, Path)) { httpSetRoutDir(host, home->value); } #endif if (httpStartEndpoint(endpoint) < 0) { httpDestroyEndpoint(sp->endpoint); sp->endpoint = 0; ejsThrowIOError(ejs, "Cannot listen on %s", address->value); } } if (ejs->httpServers == 0) { ejs->httpServers = mprCreateList(-1, MPR_LIST_STATIC_VALUES); } /* Remove to make sure old listening() registrations are removed */ mprRemoveItem(ejs->httpServers, sp); mprAddItem(ejs->httpServers, sp); return 0; }
MAIN(appweb, int argc, char **argv, char **envp) { Mpr *mpr; cchar *ipAddrPort, *argp, *jail; char *ip, *logSpec; int argind, port, status, verbose; ipAddrPort = 0; ip = 0; jail = 0; port = -1; verbose = 0; logSpec = 0; argv[0] = BLD_APPWEB_PATH; if ((mpr = mprCreate(argc, argv, MPR_USER_EVENTS_THREAD)) == NULL) { exit(1); } mprSetAppName(BLD_PRODUCT, BLD_NAME, BLD_VERSION); if ((app = mprAllocObj(App, manageApp)) == NULL) { exit(2); } mprAddRoot(app); mprAddStandardSignals(); #if BLD_FEATURE_ROMFS extern MprRomInode romFiles[]; mprSetRomFileSystem(romFiles); #endif app->mpr = mpr; app->workers = -1; app->configFile = BLD_CONFIG_FILE; app->home = BLD_SERVER_ROOT; app->documents = app->home; argc = mpr->argc; argv = mpr->argv; for (argind = 1; argind < argc; argind++) { argp = argv[argind]; if (*argp != '-') { break; } if (smatch(argp, "--config") || smatch(argp, "--conf")) { if (argind >= argc) { usageError(); } app->configFile = sclone(argv[++argind]); #if BLD_UNIX_LIKE } else if (smatch(argp, "--chroot")) { if (argind >= argc) { usageError(); } jail = mprGetAbsPath(argv[++argind]); #endif } else if (smatch(argp, "--debugger") || smatch(argp, "-D")) { mprSetDebugMode(1); } else if (smatch(argp, "--exe")) { if (argind >= argc) { usageError(); } mpr->argv[0] = mprGetAbsPath(argv[++argind]); mprSetAppPath(mpr->argv[0]); mprSetModuleSearchPath(NULL); } else if (smatch(argp, "--home")) { if (argind >= argc) { usageError(); } app->home = mprGetAbsPath(argv[++argind]); #if UNUSED && KEEP if (chdir(app->home) < 0) { mprError("%s: Can't change directory to %s", mprGetAppName(), app->home); exit(4); } #endif } else if (smatch(argp, "--log") || smatch(argp, "-l")) { if (argind >= argc) { usageError(); } logSpec = argv[++argind]; } else if (smatch(argp, "--name") || smatch(argp, "-n")) { if (argind >= argc) { usageError(); } mprSetAppName(argv[++argind], 0, 0); } else if (smatch(argp, "--threads")) { if (argind >= argc) { usageError(); } app->workers = atoi(argv[++argind]); } else if (smatch(argp, "--verbose") || smatch(argp, "-v")) { verbose++; } else if (smatch(argp, "--version") || smatch(argp, "-V")) { mprPrintf("%s %s-%s\n", mprGetAppTitle(), BLD_VERSION, BLD_NUMBER); exit(0); } else { mprError("Unknown switch \"%s\"", argp); usageError(); exit(5); } } if (logSpec) { mprStartLogging(logSpec, 1); mprSetCmdlineLogging(1); } else if (verbose) { mprStartLogging(sfmt("stderr:%d", verbose + 1), 1); mprSetCmdlineLogging(1); } if (mprStart() < 0) { mprUserError("Can't start MPR for %s", mprGetAppName()); mprDestroy(MPR_EXIT_DEFAULT); return MPR_ERR_CANT_INITIALIZE; } if (checkEnvironment(argv[0]) < 0) { exit(6); } if (argc > argind) { if (argc > (argind + 2)) { usageError(); } ipAddrPort = argv[argind++]; if (argc > argind) { app->documents = sclone(argv[argind++]); } mprParseSocketAddress(ipAddrPort, &ip, &port, HTTP_DEFAULT_PORT); } else if (findConfigFile() < 0) { exit(7); } if (jail && changeRoot(jail) < 0) { exit(8); } if (initialize(ip, port) < 0) { return MPR_ERR_CANT_INITIALIZE; } if (maStartAppweb(app->appweb) < 0) { mprUserError("Can't start HTTP service, exiting."); exit(9); } /* Service I/O events until instructed to exit */ while (!mprIsStopping()) { mprServiceEvents(-1, 0); } status = mprGetExitStatus(); mprLog(1, "Stopping Appweb ..."); maStopAppweb(app->appweb); mprDestroy(MPR_EXIT_DEFAULT); return status; }