static void testMakeDir(MprTestGroup *gp) { TestPath *ts; int rc; ts = (TestPath*) gp->data; mprGlobalLock(gp); rc = mprMakeDir(ts->dir1, DIRMODE, -1, -1, 1); assert(rc == 0); assert(access(ts->dir1, X_OK) == 0); rmdir(ts->dir1); assert(access(ts->dir1, X_OK) < 0); rmdir(ts->dir2); rmdir(ts->dir1); /* Dir should not exist before test */ assert(access(ts->dir1, X_OK) < 0); /* Make directory path */ rc = mprMakeDir(ts->dir2, DIRMODE, -1, -1, 1); assert(rc == 0); assert(access(ts->dir2, X_OK) == 0); rc = mprDeletePath(ts->dir2); assert(rc == 0); rc = mprDeletePath(ts->dir1); assert(rc == 0); assert(access(ts->dir1, X_OK) < 0); assert(access(ts->dir2, X_OK) < 0); mprGlobalUnlock(gp); }
static int fetch(MaClient *client, char *method, char *url, char *data, int len) { struct stat sbuf; FILE *fp; MaUrl *parsedUrl; char *header, *content, *diffBuf, *msg; char path[MPR_MAX_PATH], dir[MPR_MAX_PATH], urlBuf[MPR_HTTP_MAX_URL]; char tmp[MPR_MAX_PATH]; int i, code, rc, contentLen, mark, elapsed, c; fp = 0; lock(); fetchCount++; unlock(); if (url == 0) { return MPR_ERR_BAD_ARGS; } if (*url == '/') { mprSprintf(urlBuf, sizeof(urlBuf), "http://127.0.0.1%s", url); url = urlBuf; } else if ((strstr(url, "http://")) == 0) { mprSprintf(urlBuf, sizeof(urlBuf), "http://%s", url); url = urlBuf; } mprLog(MPR_DEBUG, tMod, "fetch: %s %s\n", method, url); mark = mprGetTime(0); if (mprStrCmpAnyCase(method, "GET") == 0) { rc = client->getRequest(url); } else if (mprStrCmpAnyCase(method, "HEAD") == 0) { rc = client->headRequest(url); } else if (mprStrCmpAnyCase(method, "OPTIONS") == 0) { rc = client->optionsRequest(url); } else if (mprStrCmpAnyCase(method, "POST") == 0) { rc = client->postRequest(url, data, len); } else if (mprStrCmpAnyCase(method, "TRACE") == 0) { rc = client->traceRequest(url); } if (rc < 0) { return MPR_ERR_CANT_OPEN; } code = client->getResponseCode(); content = client->getResponseContent(&contentLen); header = client->getResponseHeader(); msg = client->getResponseMessage(); elapsed = mprGetTime(0) - mark; if (code == 200 || code == 302) { mprLog(6, tMod, "Response code %d, content len %d, header: \n%s\n", code, contentLen, header); } else { mprLog(2, tMod, "Response code %d, content len %d, header: \n%s\n%s", code, contentLen, header, content); mprError(MPR_L, MPR_USER, "Can't retrieve \"%s\" (%d), %s", url, code, msg); return MPR_ERR_CANT_READ; } if (cmpDir) { client->getParsedUrl(&parsedUrl); mprSprintf(path, sizeof(path), "%s%s", cmpDir, parsedUrl->uri); if (path[strlen(path) - 1] == '/') { path[strlen(path) - 1] = '\0'; } if (stat(path, &sbuf) < 0) { mprError(MPR_L, MPR_USER, "Can't access %s", path); return MPR_ERR_CANT_ACCESS; } if (sbuf.st_mode & S_IFDIR) { strcpy(tmp, path); mprSprintf(path, sizeof(path), "%s/_DEFAULT_.html", tmp); } if (stat(path, &sbuf) < 0) { mprError(MPR_L, MPR_USER, "Can't access %s", path); return MPR_ERR_CANT_ACCESS; } if ((int) sbuf.st_size != contentLen) { mprError(MPR_L, MPR_USER, "Failed comparison for %s" "ContentLen %d, size %d\n", url, contentLen, sbuf.st_size); return MPR_ERR_CANT_ACCESS; } if ((fp = fopen(path, "r" MPR_BINARY)) == 0) { mprError(MPR_L, MPR_USER, "Can't open %s", path); return MPR_ERR_CANT_OPEN; } diffBuf = (char*) mprMalloc(contentLen); if ((int) fread(diffBuf, 1, contentLen, fp) != contentLen) { mprError(MPR_L, MPR_USER, "Can't read content from %s", path); return MPR_ERR_CANT_READ; } for (i = 0; i < contentLen; i++) { if (diffBuf[i] != content[i]) { mprError(MPR_L, MPR_USER, "Failed comparison for %s" "At byte %d: %x vs %x\n", i, (uchar) diffBuf[i], (uchar) content[i]); return MPR_ERR_GENERAL; } } fclose(fp); mprFree(diffBuf); } if (writeDir) { client->getParsedUrl(&parsedUrl); mprSprintf(path, sizeof(path), "%s%s", writeDir, parsedUrl->uri); if (path[strlen(path) - 1] == '/') { path[strlen(path) - 1] = '\0'; } mprGetDirName(dir, sizeof(dir), path); mprMakeDir(dir); if (stat(path, &sbuf) == 0 && sbuf.st_mode & S_IFDIR) { strcpy(tmp, path); mprSprintf(path, sizeof(path), "%s/_DEFAULT_.html", tmp); } if ((fp = fopen(path, "w" MPR_BINARY)) == 0) { mprError(MPR_L, MPR_USER, "Can't open %s", path); return MPR_ERR_CANT_OPEN; } if (outputHeader) { if (fputs(header, fp) != EOF) { mprError(MPR_L, MPR_USER, "Can't write header to %s", path); return MPR_ERR_CANT_WRITE; } fputc('\n', fp); } if ((int) fwrite(content, 1, contentLen, fp) != contentLen) { mprError(MPR_L, MPR_USER, "Can't write content to %s", path); return MPR_ERR_CANT_WRITE; } fclose(fp); } lock(); if (trace) { if (strstr(url, "http://") != 0) { url += 7; } if ((fetchCount % 100) == 1) { if (fetchCount == 1 || (fetchCount % 2500) == 1) { if (fetchCount > 1) { mprPrintf("\n"); } mprPrintf( " Count Fd Thread Op Code Bytes Time Url\n"); } mprPrintf("%8d %4d %7s %4s %5d %7d %5.2f %s\n", fetchCount - 1, client->getFd(), #if BLD_FEATURE_MULTITHREAD mprGetCurrentThreadName(), #else "", #endif method, code, contentLen, elapsed / 1000.0, url); } } if (outputHeader) { mprPrintf("%s\n", header); } if (!quietMode) { for (i = 0; i < contentLen; i++) { if (!isprint((uchar) content[i]) && content[i] != '\n' && content[i] != '\r' && content[i] != '\t') { break; } } if (contentLen > 0) { if (i != contentLen && 0) { mprPrintf("Content has non-printable data\n"); } else { // mprPrintf("Length of content %d\n", contentLen); for (i = 0; i < contentLen; i++) { c = (uchar) content[i]; if (isprint((uchar) c) || isspace((uchar) c)) { putchar(content[i]); } else { mprPrintf("0x%x", c); } } fflush(stdout); } } } unlock(); if (singleStep) { mprPrintf("Pause: "); read(0, (char*) &rc, 1); } return 0; }
/* Compile a view or controller cacheName MD5 cache name (not a full path) source ESP source file name module Module file name */ bool espCompile(HttpConn *conn, cchar *source, cchar *module, cchar *cacheName, int isView) { MprFile *fp; HttpRx *rx; HttpRoute *route; EspRoute *eroute; cchar *csource; char *layout, *script, *page, *err; ssize len; rx = conn->rx; route = rx->route; eroute = route->eroute; layout = 0; if (isView) { if ((page = mprReadPathContents(source, &len)) == 0) { httpError(conn, HTTP_CODE_INTERNAL_SERVER_ERROR, "Can't read %s", source); return 0; } /* Use layouts iff there is a source defined on the route. Only MVC/controllers based apps do this. */ if (eroute->layoutsDir) { #if UNUSED layout = mprSamePath(eroute->layoutsDir, route->dir) ? 0 : mprJoinPath(eroute->layoutsDir, "default.esp"); #else layout = mprJoinPath(eroute->layoutsDir, "default.esp"); #endif } if ((script = espBuildScript(route, page, source, cacheName, layout, &err)) == 0) { httpError(conn, HTTP_CODE_INTERNAL_SERVER_ERROR, "Can't build %s, error %s", source, err); return 0; } csource = mprJoinPathExt(mprTrimPathExt(module), ".c"); if ((fp = mprOpenFile(csource, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0664)) == 0) { httpError(conn, HTTP_CODE_INTERNAL_SERVER_ERROR, "Can't open compiled script file %s", csource); return 0; } len = slen(script); if (mprWriteFile(fp, script, len) != len) { httpError(conn, HTTP_CODE_INTERNAL_SERVER_ERROR, "Can't write compiled script file %s", csource); mprCloseFile(fp); return 0; } mprCloseFile(fp); } else { csource = source; } mprMakeDir(eroute->cacheDir, 0775, -1, -1, 1); /* WARNING: GC yield here */ if (runCommand(conn, eroute->compile, csource, module) < 0) { return 0; } if (eroute->link) { /* WARNING: GC yield here */ if (runCommand(conn, eroute->link, csource, module) < 0) { return 0; } #if !(BLD_DEBUG && MACOSX) /* MAC needs the object for debug information */ mprDeletePath(mprJoinPathExt(mprTrimPathExt(module), BLD_OBJ)); #endif } #if BLD_WIN_LIKE { /* Windows leaves intermediate object in the current directory */ cchar *obj; obj = mprReplacePathExt(mprGetPathBase(csource), BLD_OBJ); if (mprPathExists(obj, F_OK)) { mprDeletePath(obj); } } #endif if (!eroute->keepSource && isView) { mprDeletePath(csource); } return 1; }