static int getVars(MaQueue *q, char ***keys, char *buf, int len) { char** keyList; char *eq, *cp, *pp, *tok; int i, keyCount; *keys = 0; /* * Change all plus signs back to spaces */ keyCount = (len > 0) ? 1 : 0; for (cp = buf; cp < &buf[len]; cp++) { if (*cp == '+') { *cp = ' '; } else if (*cp == '&' && (cp > buf && cp < &buf[len - 1])) { keyCount++; } } if (keyCount == 0) { return 0; } /* * Crack the input into name/value pairs */ keyList = (char**) mprAlloc(q, (keyCount * 2) * sizeof(char**)); i = 0; tok = 0; for (pp = mprStrTok(buf, "&", &tok); pp; pp = mprStrTok(0, "&", &tok)) { if ((eq = strchr(pp, '=')) != 0) { *eq++ = '\0'; mprUrlDecode(pp, (int) strlen(pp) + 1, pp); mprUrlDecode(eq, (int) strlen(eq) + 1, eq); } else { mprUrlDecode(pp, (int) strlen(pp) + 1, pp); } if (i < (keyCount * 2)) { keyList[i++] = pp; keyList[i++] = eq; } } *keys = keyList; return keyCount; }
static void upload(MaQueue *q) { MaConn *conn; char *sw; char *newLocation; int responseStatus; conn = q->conn; newLocation = 0; responseStatus = 0; sw = (char*) strstr(maGetFormVar(conn, "QUERY_STRING", ""), "SWITCHES="); if (sw) { sw = mprStrdup(q, sw + 9); mprUrlDecode(sw, (int) strlen(sw) + 1, sw); if (*sw == '-') { if (sw[1] == 'l') { newLocation = sw + 3; } else if (sw[1] == 's') { responseStatus = atoi(sw + 3); } } } maSetResponseCode(conn, 200); maSetResponseMimeType(conn, "text/html"); maDontCacheResponse(conn); /* * Test writing headers. The Server header overwrote the "Server" header * * maSetHeader(conn, "MyCustomHeader: true"); * maSetHeader(conn, "Server: private"); */ if (maGetCookies(conn) == 0) { maSetCookie(conn, "appwebTest", "Testing can be fun", 43200, "/", 0); } if (newLocation) { maRedirect(conn, 302, newLocation); } else if (responseStatus) { maFailRequest(conn, responseStatus, "Custom Status"); } else { maWrite(q, "<HTML><TITLE>egiProgram: EGI Output</TITLE><BODY>\r\n"); printRequestHeaders(q); printQueryData(q); printBodyData(q); maWrite(q, "</BODY></HTML>\r\n"); } if (sw) { mprFree(sw); } }
/* * Decode the query and post form data into formVars */ static void decodeFormData(cchar *data) { char *value, *key, *buf; int buflen; buf = mprStrdup(mpr, data); buflen = strlen(buf); /* * Crack the input into name/value pairs */ for (key = strtok(buf, "&"); key; key = strtok(0, "&")) { if ((value = strchr(key, '=')) != 0) { *value++ = '\0'; mprUrlDecode(value, (int) strlen(value) + 1, value); } mprUrlDecode(key, (int) strlen(key) + 1, key); mprAddHash(formVars, key, value); } }
int maSetRequestUri(MaConn *conn, cchar *uri, cchar *query) { MaRequest *req; MaResponse *resp; MaHost *host; MprUri *prior; char *cp; if (uri == 0 || *uri == 0) { uri = "/"; } host = conn->host; req = conn->request; resp = conn->response; prior = req->parsedUri; if ((req->parsedUri = mprParseUri(req, uri)) == 0) { return MPR_ERR_BAD_ARGS; } if (prior) { if ((cp = strstr(uri, "://")) == 0) { req->parsedUri->scheme = prior->scheme; req->parsedUri->host = prior->host; } else if (strchr(&cp[3], ':') == 0) { req->parsedUri->port = prior->port; } } if (query == 0 && prior) { req->parsedUri->query = prior->query; } else if (*query) { req->parsedUri->query = mprStrdup(req->parsedUri, query); } req->url = mprValidateUrl(req, mprUrlDecode(req, req->parsedUri->url)); req->alias = maGetAlias(host, req->url); resp->filename = maMakeFilename(conn, req->alias, req->url, 1); if ((req->dir = maLookupBestDir(req->host, resp->filename)) != 0) { if (req->dir->auth) { req->auth = req->dir->auth; } } req->location = maLookupBestLocation(host, req->url); if (req->auth == 0) { req->auth = req->location->auth; } mprGetPathInfo(conn, resp->filename, &resp->fileInfo); resp->extension = maGetExtension(conn); if ((resp->mimeType = (char*) maLookupMimeType(host, resp->extension)) == 0) { resp->mimeType = (char*) "text/html"; } return 0; }
/* Build the command arguments. NOTE: argv is untrusted input. */ static void buildArgs(MaConn *conn, MprCmd *cmd, int *argcp, char ***argvp) { MaRequest *req; MaResponse *resp; char *fileName, **argv, status[8], *indexQuery, *cp, *tok; cchar *actionProgram; int argc, argind, len; req = conn->request; resp = conn->response; fileName = resp->filename; mprAssert(fileName); actionProgram = 0; argind = 0; argc = *argcp; if (resp->extension) { actionProgram = maGetMimeActionProgram(req->host, resp->extension); if (actionProgram != 0) { argc++; } } /* This is an Apache compatible hack for PHP 5.3 */ mprItoa(status, sizeof(status), MPR_HTTP_CODE_MOVED_TEMPORARILY, 10); mprAddHash(req->headers, "REDIRECT_STATUS", mprStrdup(req, status)); /* Count the args for ISINDEX queries. Only valid if there is not a "=" in the query. If this is so, then we must not have these args in the query env also? */ indexQuery = req->parsedUri->query; if (indexQuery && !strchr(indexQuery, '=')) { argc++; for (cp = indexQuery; *cp; cp++) { if (*cp == '+') { argc++; } } } else { indexQuery = 0; } #if BLD_WIN_LIKE || VXWORKS { char *bangScript, *cmdBuf, *program, *cmdScript; /* On windows we attempt to find an executable matching the fileName. We look for *.exe, *.bat and also do unix style processing "#!/program" */ findExecutable(conn, &program, &cmdScript, &bangScript, fileName); mprAssert(program); if (cmdScript) { /* Cmd/Batch script (.bat | .cmd) Convert the command to the form where there are 4 elements in argv that cmd.exe can interpret. argv[0] = cmd.exe argv[1] = /Q argv[2] = /C argv[3] = ""script" args ..." */ argc = 4; len = (argc + 1) * sizeof(char*); argv = (char**) mprAlloc(cmd, len); memset(argv, 0, len); argv[argind++] = program; /* Duped in findExecutable */ argv[argind++] = mprStrdup(cmd, "/Q"); argv[argind++] = mprStrdup(cmd, "/C"); len = (int) strlen(cmdScript) + 2 + 1; cmdBuf = (char*) mprAlloc(cmd, len); mprSprintf(cmdBuf, len, "\"%s\"", cmdScript); argv[argind++] = cmdBuf; mprSetCmdDir(cmd, cmdScript); mprFree(cmdScript); /* program will get freed when argv[] gets freed */ } else if (bangScript) { /* Script used "#!/program". NOTE: this may be overridden by a mime Action directive. */ argc++; /* Adding bangScript arg */ len = (argc + 1) * sizeof(char*); argv = (char**) mprAlloc(cmd, len); memset(argv, 0, len); argv[argind++] = program; /* Will get freed when argv[] is freed */ argv[argind++] = bangScript; /* Will get freed when argv[] is freed */ mprSetCmdDir(cmd, bangScript); } else { /* Either unknown extension or .exe (.out) program. */ len = (argc + 1) * sizeof(char*); argv = (char**) mprAlloc(cmd, len); memset(argv, 0, len); if (actionProgram) { argv[argind++] = mprStrdup(cmd, actionProgram); } argv[argind++] = program; } } #else len = (argc + 1) * (int) sizeof(char*); argv = (char**) mprAlloc(cmd, len); memset(argv, 0, len); if (actionProgram) { argv[argind++] = mprStrdup(cmd, actionProgram); } argv[argind++] = mprStrdup(cmd, fileName); #endif /* ISINDEX queries. Only valid if there is not a "=" in the query. If this is so, then we must not have these args in the query env also? */ if (indexQuery) { indexQuery = mprStrdup(cmd, indexQuery); cp = mprStrTok(indexQuery, "+", &tok); while (cp) { argv[argind++] = mprEscapeCmd(resp, mprUrlDecode(resp, cp), 0); cp = mprStrTok(NULL, "+", &tok); } } mprAssert(argind <= argc); argv[argind] = 0; *argcp = argc; *argvp = argv; }