/* * Test authorization within a virtual host */ static void auth(MprTestGroup *gp) { MprHttp *http; http = getHttp(gp); assert(get(gp, NULL, 2, "/acme.html", 0)); mprSetHttpCredentials(http, "mary", "pass2"); assert(get(gp, NULL, 2, "/protected/private.html", 0)); mprResetHttpCredentials(http); }
static bool authGet(MprTestGroup *gp, char *uri, char *realm, char *user, char *password, int expectCode) { MprHttp *http; char uriBuf[MPR_MAX_STRING]; cchar *content; int rc, code, contentLength; http = getHttp(gp); if (authType) { mprSprintf(uriBuf, sizeof(uriBuf), "/%s%s", authType, uri); mprSetHttpCredentials(http, user, password); } else { mprStrcpy(uriBuf, sizeof(uriBuf), uri); mprResetHttpCredentials(http); } rc = httpRequest(http, "GET", uriBuf); if (!assert(rc == 0)) { return 0; } code = mprGetHttpCode(http); assert(code == expectCode); if (code != expectCode) { mprLog(gp, 0, "Client failed for %s, response code: %d, expected %d, msg %s\n", uriBuf, code, expectCode, mprGetHttpMessage(http)); return 0; } if (expectCode != 200) { contentLength = mprGetHttpContentLength(http); content = mprGetHttpContent(http); if (! assert(content != 0 && contentLength > 0)) { return 0; } } return 1; }
static void processThread(MprCtx ctx) { MprHttp *http; MprList *files; cchar *path; char *url; int next; http = mprCreateHttp(ctx); mprSetHttpTimeout(http, timeout); mprSetHttpFollowRedirects(http, !nofollow); if (chunkSize) { mprSetHttpChunked(http, 1); } if (httpVersion == 0) { mprSetHttpKeepAlive(http, 0); mprSetHttpProtocol(http, "HTTP/1.0"); } if (username) { if (password == 0 && !strchr(username, ':')) { password = getPassword(http); } mprSetHttpCredentials(http, username, password); } while (!mprIsExiting(http) && (success || continueOnErrors)) { if (singleStep) waitForUser(http); if (fileData && !upload) { for (next = 0; (path = mprGetNextItem(fileData, &next)) != 0; ) { if (target[strlen(target) - 1] == '/') { url = mprJoinPath(http, target, mprGetPathBase(http, path)); } else { url = target; } files = mprCreateList(http); mprAddItem(files, path); url = resolveUrl(http, url); if (verbose) { mprPrintf(http, "putting: %s to %s\n", path, url); } if (doRequest(http, url, formData, files) < 0) { success = 0; mprFree(files); mprFree(url); break; } mprFree(files); mprFree(url); } } else { url = resolveUrl(http, target); if (doRequest(http, url, formData, fileData) < 0) { success = 0; mprFree(url); break; } } if (iterationsComplete(http)) break; } mprFree(http); }