/* Parse the CGI output first line */ static bool parseFirstCgiResponse(Cgi *cgi, HttpPacket *packet) { MprBuf *buf; char *protocol, *status, *msg; buf = packet->content; protocol = getCgiToken(buf, " "); if (protocol == 0 || protocol[0] == '\0') { httpError(cgi->conn, HTTP_CODE_BAD_GATEWAY, "Bad CGI HTTP protocol response"); return 0; } if (strncmp(protocol, "HTTP/1.", 7) != 0) { httpError(cgi->conn, HTTP_CODE_BAD_GATEWAY, "Unsupported CGI protocol"); return 0; } status = getCgiToken(buf, " "); if (status == 0 || *status == '\0') { httpError(cgi->conn, HTTP_CODE_BAD_GATEWAY, "Bad CGI header response"); return 0; } msg = getCgiToken(buf, "\n"); mprNop(msg); mprDebug("http cgi", 4, "CGI response status: %s %s %s", protocol, status, msg); return 1; }
static EjsAny *createException(Ejs *ejs, EjsType *type, cchar* fmt, va_list fmtArgs) { EjsError *error; EjsAny *argv[1]; char *msg; assert(type); #if ME_DEBUG /* Breakpoint opportunity */ if (!ejs->empty) { mprNop(0); } #endif msg = sfmtv(fmt, fmtArgs); argv[0] = ejsCreateStringFromAsc(ejs, msg); if (argv[0] == 0) { assert(argv[0]); return 0; } if (EST(Error)->constructor.body.proc) { error = (EjsError*) ejsCreateInstance(ejs, type, 1, argv); } else { error = ejsCreatePot(ejs, type, 0); ejsSetProperty(ejs, error, ES_Error_message, ejsCreateStringFromAsc(ejs, msg)); } return error; }
/* If doing a static build, must now reference required modules to force the linker to include them. Don't actually call init routines here. They will be called via maConfigureServer. */ static void loadStaticModules() { #if BIT_STATIC #if BIT_PACK_CGI mprNop(maCgiHandlerInit); #endif #if BIT_PACK_ESP mprNop(maEspHandlerInit); #endif #if BIT_PACK_PHP mprNop(maPhpHandlerInit); #endif #if BIT_SSL mprNop(maSslModuleInit); #endif #endif /* BIT_STATIC */ }
static int growArray(Ejs *ejs, EjsArray *ap, int len) { EjsObj **dp; ssize size, factor, count; int i; assert(ap); if (len <= 0) { return 0; } if (len <= ap->length) { return 0; } size = mprGetBlockSize(ap->data); size = (int) (mprGetBlockSize(ap->data) / sizeof(EjsObj*)); /* Allocate or grow the data structures. */ if (len > size) { if (size > EJS_LOTSA_PROP) { /* Looks like a big object so grow by a bigger chunk */ factor = max(size / 4, EJS_ROUND_PROP); count = (len + factor) / factor * factor; } else { count = len; } // OPT - this is currently 16 count = EJS_PROP_ROUNDUP(count); if (ap->data == 0) { assert(ap->length == 0); assert(count > 0); if ((ap->data = mprAllocZeroed(sizeof(EjsObj*) * count)) == 0) { return EJS_ERR; } } else { assert(size > 0); if ((ap->data = mprRealloc(ap->data, sizeof(EjsObj*) * count)) == 0) { return EJS_ERR; } } dp = &ap->data[ap->length]; for (i = ap->length; i < count; i++) { *dp++ = ESV(undefined); } } else { mprNop(ITOP(size)); } ap->length = len; return 0; }
static void loadStaticModules() { #if BIT_STATIC /* If doing a static build, must now reference required modules to force the linker to include them. Don't actually call init routines here. They will be called via maConfigureServer. */ #if BIT_PACK_CGI mprNop(maCgiHandlerInit); #endif #if BIT_PACK_ESP mprNop(maEspHandlerInit); #endif #if BIT_PACK_PHP mprNop(maPhpHandlerInit); #endif #if BIT_PACK_SSL mprNop(maSslModuleInit); #endif #endif /* BIT_STATIC */ }
MAIN(httpMain, int argc, char **argv, char **envp) { MprTime start; double elapsed; if (mprCreate(argc, argv, MPR_USER_EVENTS_THREAD) == 0) { return MPR_ERR_MEMORY; } if ((app = mprAllocObj(App, manageApp)) == 0) { return MPR_ERR_MEMORY; } mprAddRoot(app); mprAddStandardSignals(); initSettings(); if (parseArgs(argc, argv) < 0) { return MPR_ERR_BAD_ARGS; } mprSetMaxWorkers(app->workers); if (mprStart() < 0) { mprError("Cannot start MPR for %s", mprGetAppTitle()); exit(2); } start = mprGetTime(); app->http = httpCreate(HTTP_CLIENT_SIDE); httpEaseLimits(app->http->clientLimits); #if BIT_STATIC && BIT_PACK_SSL extern MprModuleEntry mprSslInit; mprNop(mprSslInit); #endif processing(); mprServiceEvents(-1, 0); if (app->benchmark) { elapsed = (double) (mprGetTime() - start); if (app->fetchCount == 0) { elapsed = 0; app->fetchCount = 1; } mprPrintf("\nRequest Count: %13d\n", app->fetchCount); mprPrintf("Time elapsed: %13.4f sec\n", elapsed / 1000.0); mprPrintf("Time per request: %13.4f sec\n", elapsed / 1000.0 / app->fetchCount); mprPrintf("Requests per second: %13.4f\n", app->fetchCount * 1.0 / (elapsed / 1000.0)); mprPrintf("Load threads: %13d\n", app->loadThreads); mprPrintf("Worker threads: %13d\n", app->workers); } if (!app->success && app->verbose) { mprError("Request failed"); } mprDestroy(MPR_EXIT_DEFAULT); return (app->success) ? 0 : 255; }
static void testMalloc() { MprTime start; char *ptr; int count, i, pin; #if KEEP ssize base; #endif mprPrintf("Alloc/Malloc overhead\n"); count = 2000000 * app->iterations; pin = 0; mprGC(MPR_GC_FORCE); #if MALLOC /* malloc(1) */ base = mprGetMem(); start = startMark(); for (i = 0; i < count; i++) { ptr = malloc(1); if (pin) memset(ptr, 0, 1); } endMark(start, count, "Alloc malloc(1)"); mprPrintf("\tMalloc overhead per block %d\n\n", ((mprGetMem() - base) / count) - 1); /* malloc(8) */ base = mprGetMem(); start = startMark(); for (i = 0; i < count; i++) { ptr = malloc(8); if (pin) memset(ptr, 0, 8); } endMark(start, count, "Alloc malloc(8)"); mprPrintf("\tMalloc overhead per block %d (approx)\n\n", ((mprGetMem() - base) / count) - 8); /* malloc(16) */ base = mprGetMem(); start = startMark(); for (i = 0; i < count; i++) { ptr = malloc(16); if (pin) memset(ptr, 0, 16); } endMark(start, count, "Alloc malloc(16)"); mprPrintf("\tMalloc overhead per block %d (approx)\n\n", ((mprGetMem() - base) / count) - 16); /* malloc(32) */ base = mprGetMem(); start = startMark(); for (i = 0; i < count; i++) { ptr = malloc(32); if (pin) memset(ptr, 0, 32); } endMark(start, count, "Alloc malloc(32)"); mprPrintf("\tMalloc overhead per block %d (approx)\n\n", ((mprGetMem() - base) / count) - 32); /* malloc+free(8) */ start = startMark(); for (i = 0; i < count; i++) { ptr = malloc(8); if (pin) memset(ptr, 0, 8); mprNop(ptr); free(ptr); } endMark(start, count, "Alloc malloc+free(8)"); mprPrintf("\n"); #endif /* mprAlloc(1) */ // base = mprGetMem(); start = startMark(); for (i = 0; i < count; i++) { ptr = mprAlloc(1); if (pin) memset(ptr, 0, 1); } endMark(start, count, "Alloc mprAlloc(1)"); // mprPrintf("\tMpr overhead per block %d (approx)\n\n", ((mprGetMem() - base) / count) - 1); /* mprAlloc(8) */ // base = mprGetMem(); start = startMark(); for (i = 0; i < count; i++) { ptr = mprAlloc(8); if (pin) memset(ptr, 0, 8); } endMark(start, count, "Alloc mprAlloc(8)"); // mprPrintf("\tMpr overhead per block %d (approx)\n\n", ((mprGetMem() - base) / count) - 8); /* mprAlloc(16) */ // base = mprGetMem(); start = startMark(); for (i = 0; i < count; i++) { ptr = mprAlloc(16); if (pin) memset(ptr, 0, 16); } endMark(start, count, "Alloc mprAlloc(16)"); // mprPrintf("\tMpr overhead per block %d (approx)\n\n", ((mprGetMem() - base) / count) - 16); /* mprAlloc(32) */ // base = mprGetMem(); start = startMark(); for (i = 0; i < count; i++) { ptr = mprAlloc(32); if (pin) memset(ptr, 0, 32); } endMark(start, count, "Alloc mprAlloc(32)"); // mprPrintf("\tMpr overhead per block %d (approx)\n\n", ((mprGetMem() - base) / count) - 32); /* mprAlloc(64) */ // base = mprGetMem(); start = startMark(); for (i = 0; i < count; i++) { ptr = mprAlloc(64); if (pin) memset(ptr, 0, 64); } endMark(start, count, "Alloc mprAlloc(32)"); // mprPrintf("\tMpr overhead per block %d (approx)\n\n", ((mprGetMem() - base) / count) - 64); mprPrintf("\n"); }
static int parseArgs(int argc, char **argv) { char *argp, *key, *value; int i, setWorkers, nextArg, ssl; setWorkers = 0; ssl = 0; for (nextArg = 1; nextArg < argc; nextArg++) { argp = argv[nextArg]; if (*argp != '-') { break; } if (smatch(argp, "--auth")) { if (nextArg >= argc) { return showUsage(); } else { app->authType = slower(argv[++nextArg]); } } else if (smatch(argp, "--benchmark") || smatch(argp, "-b")) { app->benchmark++; } else if (smatch(argp, "--ca")) { if (nextArg >= argc) { return showUsage(); } else { app->ca = sclone(argv[++nextArg]); if (!mprPathExists(app->ca, R_OK)) { mprError("Cannot find ca file %s", app->ca); return MPR_ERR_BAD_ARGS; } } ssl = 1; } else if (smatch(argp, "--cert")) { if (nextArg >= argc) { return showUsage(); } else { app->cert = sclone(argv[++nextArg]); if (!mprPathExists(app->cert, R_OK)) { mprError("Cannot find cert file %s", app->cert); return MPR_ERR_BAD_ARGS; } } ssl = 1; } else if (smatch(argp, "--chunk")) { if (nextArg >= argc) { return showUsage(); } else { value = argv[++nextArg]; app->chunkSize = atoi(value); if (app->chunkSize < 0) { mprError("Bad chunksize %d", app->chunkSize); return MPR_ERR_BAD_ARGS; } } } else if (smatch(argp, "--ciphers")) { if (nextArg >= argc) { return showUsage(); } else { app->ciphers = sclone(argv[++nextArg]); } ssl = 1; } else if (smatch(argp, "--continue") || smatch(argp, "-c")) { app->continueOnErrors++; } else if (smatch(argp, "--cookie")) { if (nextArg >= argc) { return showUsage(); } else { mprAddItem(app->headers, mprCreateKeyPair("Cookie", argv[++nextArg], 0)); } } else if (smatch(argp, "--data")) { if (nextArg >= argc) { return showUsage(); } else { if (app->bodyData == 0) { app->bodyData = mprCreateBuf(-1, -1); } mprPutStringToBuf(app->bodyData, argv[++nextArg]); } } else if (smatch(argp, "--debugger") || smatch(argp, "-D")) { mprSetDebugMode(1); app->retries = 0; app->timeout = MAXINT; } else if (smatch(argp, "--delete")) { app->method = "DELETE"; } else if (smatch(argp, "--form") || smatch(argp, "-f")) { if (nextArg >= argc) { return showUsage(); } else { if (app->formData == 0) { app->formData = mprCreateList(-1, 0); } addFormVars(argv[++nextArg]); } } else if (smatch(argp, "--header")) { if (nextArg >= argc) { return showUsage(); } else { key = argv[++nextArg]; if ((value = strchr(key, ':')) == 0) { mprError("Bad header format. Must be \"key: value\""); return MPR_ERR_BAD_ARGS; } *value++ = '\0'; while (isspace((uchar) *value)) { value++; } mprAddItem(app->headers, mprCreateKeyPair(key, value, 0)); } } else if (smatch(argp, "--host")) { if (nextArg >= argc) { return showUsage(); } else { app->host = argv[++nextArg]; if (*app->host == ':') { app->host = &app->host[1]; } if (isPort(app->host)) { app->host = sfmt("http://127.0.0.1:%s", app->host); } else { app->host = sclone(app->host); } } } else if (smatch(argp, "--iterations") || smatch(argp, "-i")) { if (nextArg >= argc) { return showUsage(); } else { app->iterations = atoi(argv[++nextArg]); } } else if (smatch(argp, "--key")) { if (nextArg >= argc) { return showUsage(); } else { app->key = sclone(argv[++nextArg]); if (!mprPathExists(app->key, R_OK)) { mprError("Cannot find key file %s", app->key); return MPR_ERR_BAD_ARGS; } } ssl = 1; } else if (smatch(argp, "--log") || smatch(argp, "-l")) { if (nextArg >= argc) { return showUsage(); } else { mprStartLogging(argv[++nextArg], 0); } } else if (smatch(argp, "--method") || smatch(argp, "-m")) { if (nextArg >= argc) { return showUsage(); } else { app->method = argv[++nextArg]; } } else if (smatch(argp, "--out") || smatch(argp, "-o")) { if (nextArg >= argc) { return showUsage(); } else { app->outFilename = sclone(argv[++nextArg]); } } else if (smatch(argp, "--noout") || smatch(argp, "-n") || smatch(argp, "--quiet") || smatch(argp, "-q")) { app->noout++; } else if (smatch(argp, "--nofollow")) { app->nofollow++; } else if (smatch(argp, "--password") || smatch(argp, "-p")) { if (nextArg >= argc) { return showUsage(); } else { app->password = sclone(argv[++nextArg]); } } else if (smatch(argp, "--post")) { app->method = "POST"; } else if (smatch(argp, "--printable")) { app->printable++; } else if (smatch(argp, "--protocol")) { if (nextArg >= argc) { return showUsage(); } else { app->protocol = supper(argv[++nextArg]); } } else if (smatch(argp, "--provider")) { /* Undocumented SSL provider selection */ if (nextArg >= argc) { return showUsage(); } else { app->provider = sclone(argv[++nextArg]); } ssl = 1; } else if (smatch(argp, "--put")) { app->method = "PUT"; } else if (smatch(argp, "--range")) { if (nextArg >= argc) { return showUsage(); } else { if (app->ranges == 0) { app->ranges = sfmt("bytes=%s", argv[++nextArg]); } else { app->ranges = srejoin(app->ranges, ",", argv[++nextArg], NULL); } } } else if (smatch(argp, "--retries") || smatch(argp, "-r")) { if (nextArg >= argc) { return showUsage(); } else { app->retries = atoi(argv[++nextArg]); } } else if (smatch(argp, "--self")) { /* Undocumented. Allow self-signed certs. Users should just not set --verify */ app->verifyIssuer = 0; ssl = 1; } else if (smatch(argp, "--sequence")) { app->sequence++; } else if (smatch(argp, "--showHeaders") || smatch(argp, "--show") || smatch(argp, "-s")) { app->showHeaders++; } else if (smatch(argp, "--showStatus") || smatch(argp, "--showCode")) { app->showStatus++; } else if (smatch(argp, "--single") || smatch(argp, "-s")) { app->singleStep++; } else if (smatch(argp, "--text")) { app->text++; } else if (smatch(argp, "--threads") || smatch(argp, "-t")) { if (nextArg >= argc) { return showUsage(); } else { app->loadThreads = atoi(argv[++nextArg]); } } else if (smatch(argp, "--timeout")) { if (nextArg >= argc) { return showUsage(); } else { app->timeout = atoi(argv[++nextArg]) * MPR_TICKS_PER_SEC; } } else if (smatch(argp, "--upload") || smatch(argp, "-u")) { app->upload++; } else if (smatch(argp, "--user") || smatch(argp, "--username")) { if (nextArg >= argc) { return showUsage(); } else { app->username = argv[++nextArg]; } // DEPRECATE validate. Preserve verify. } else if (smatch(argp, "--validate") || smatch(argp, "--verify")) { app->verifyPeer = 1; ssl = 1; } else if (smatch(argp, "--verbose") || smatch(argp, "-v")) { app->verbose++; } else if (smatch(argp, "--version") || smatch(argp, "-V")) { mprEprintf("%s %s\n" "Copyright (C) Embedthis Software 2003-2013\n" "Copyright (C) Michael O'Brien 2003-2013\n", BIT_TITLE, BIT_VERSION); exit(0); } else if (smatch(argp, "--workerTheads") || smatch(argp, "-w")) { if (nextArg >= argc) { return showUsage(); } else { app->workers = atoi(argv[++nextArg]); } setWorkers++; } else if (smatch(argp, "--zero")) { app->zeroOnErrors++; } else if (smatch(argp, "--")) { nextArg++; break; } else if (smatch(argp, "-")) { break; } else { return showUsage(); } } if (argc == nextArg) { return showUsage(); } app->nextArg = nextArg; argc = argc - nextArg; argv = &argv[nextArg]; app->target = argv[argc - 1]; if (--argc > 0) { /* Files present on command line */ app->files = mprCreateList(argc, MPR_LIST_STATIC_VALUES); for (i = 0; i < argc; i++) { mprAddItem(app->files, argv[i]); } } if (!setWorkers) { app->workers = app->loadThreads + 2; } if (app->method == 0) { if (app->bodyData || app->formData || app->upload) { app->method = "POST"; } else if (app->files) { app->method = "PUT"; } else { app->method = "GET"; } } #if BIT_PACK_SSL { HttpUri *uri = httpCreateUri(app->target, 0); if (uri->secure || ssl) { app->ssl = mprCreateSsl(0); if (app->provider) { mprSetSslProvider(app->ssl, app->provider); } if (app->cert) { if (!app->key) { mprError("Must specify key file"); return 0; } mprSetSslCertFile(app->ssl, app->cert); mprSetSslKeyFile(app->ssl, app->key); } if (app->ca) { mprLog(4, "Using CA: \"%s\"", app->ca); mprSetSslCaFile(app->ssl, app->ca); } if (app->verifyIssuer == -1) { app->verifyIssuer = app->verifyPeer ? 1 : 0; } mprVerifySslPeer(app->ssl, app->verifyPeer); mprVerifySslIssuer(app->ssl, app->verifyIssuer); if (app->ciphers) { mprSetSslCiphers(app->ssl, app->ciphers); } } else { mprVerifySslPeer(NULL, 0); } } #else /* Suppress comp warning */ mprNop(&ssl); #endif return 0; }