static int reportResponse(HttpConn *conn, cchar *url, MprTime elapsed) { HttpRx *rx; MprOff bytesRead; char *responseHeaders; int status; if (mprShouldAbortRequests(conn)) { return 0; } app->status = status = httpGetStatus(conn); bytesRead = httpGetContentLength(conn); if (bytesRead < 0 && conn->rx) { bytesRead = conn->rx->bytesRead; } mprLog(6, "Response status %d, elapsed %Ld", status, elapsed); if (conn->error) { app->success = 0; } if (conn->rx && bytesRead > 0) { if (!app->noout) { mprPrintf("\n"); } if (app->showHeaders) { responseHeaders = httpGetHeaders(conn); rx = conn->rx; mprPrintf("%s %d %s\n", conn->protocol, status, rx->statusMessage); if (responseHeaders) { mprPrintf("%s\n", responseHeaders); } } else if (app->showStatus) { mprPrintf("%d\n", status); } } if (status < 0) { mprError("Can't process request for \"%s\" %s", url, httpGetError(conn)); return MPR_ERR_CANT_READ; } else if (status == 0 && conn->protocol == 0) { /* Ignore */; } else if (!(200 <= status && status <= 206) && !(301 <= status && status <= 304)) { if (!app->zeroOnErrors) { app->success = 0; } if (!app->showStatus) { mprError("Can't process request for \"%s\" (%d) %s", url, status, httpGetError(conn)); return MPR_ERR_CANT_READ; } } mprLock(app->mutex); if (app->verbose && app->noout) { trace(conn, url, app->fetchCount, app->method, status, bytesRead); } mprUnlock(app->mutex); return 0; }
bool simpleGet(MprTestGroup *gp, cchar *uri, int expectStatus) { HttpConn *conn; int status; if (expectStatus <= 0) { expectStatus = 200; } if (startRequest(gp, "GET", uri) < 0) { return 0; } conn = getConn(gp); httpFinalizeOutput(conn); if (httpWait(conn, HTTP_STATE_COMPLETE, -1) < 0) { return MPR_ERR_CANT_READ; } status = httpGetStatus(gp->conn); tassert(status == expectStatus); if (status != expectStatus) { mprLog("appweb test get", 0, "HTTP response code %d, expected %d", status, expectStatus); return 0; } tassert(httpGetError(gp->conn) != 0); gp->content = httpReadString(gp->conn); tassert(gp->content != NULL); httpDestroyConn(gp->conn); gp->conn = 0; return 1; }
static int sendRequest(HttpConn *conn, cchar *method, cchar *url, MprList *files) { if (httpConnect(conn, method, url, app->ssl) < 0) { mprError("Can't process request for \"%s\". %s.", url, httpGetError(conn)); return MPR_ERR_CANT_OPEN; } /* This program does not do full-duplex writes with reads. ie. if you have a request that sends and receives data in parallel -- http will do the writes first then read the response. */ if (app->bodyData || app->formData || files) { if (writeBody(conn, files) < 0) { mprError("Can't write body data to \"%s\". %s", url, httpGetError(conn)); return MPR_ERR_CANT_WRITE; } } mprAssert(!mprGetCurrentThread()->yielded); httpFinalize(conn); return 0; }
static ssize writeBody(HttpConn *conn, MprList *files) { MprFile *file; char buf[HTTP_BUFSIZE], *path, *pair; ssize bytes, len, count, nbytes, sofar; int next, oldMode; if (app->upload) { if (httpWriteUploadData(conn, app->files, app->formData) < 0) { mprError("Can't write upload data %s", httpGetError(conn)); return MPR_ERR_CANT_WRITE; } } else { if (app->formData) { count = mprGetListLength(app->formData); for (next = 0; (pair = mprGetNextItem(app->formData, &next)) != 0; ) { len = strlen(pair); if (next < count) { len = slen(pair); if (httpWrite(conn->writeq, pair, len) != len || httpWrite(conn->writeq, "&", 1) != 1) { return MPR_ERR_CANT_WRITE; } } else { if (httpWrite(conn->writeq, pair, len) != len) { return MPR_ERR_CANT_WRITE; } } } } if (files) { mprAssert(mprGetListLength(files) == 1); for (next = 0; (path = mprGetNextItem(files, &next)) != 0; ) { if (strcmp(path, "-") == 0) { file = mprAttachFileFd(0, "stdin", O_RDONLY | O_BINARY); } else { file = mprOpenFile(path, O_RDONLY | O_BINARY, 0); } if (file == 0) { mprError("Can't open \"%s\"", path); return MPR_ERR_CANT_OPEN; } app->inFile = file; if (app->verbose) { mprPrintf("uploading: %s\n", path); } oldMode = mprSetSocketBlockingMode(conn->sock, 1); while ((bytes = mprReadFile(file, buf, sizeof(buf))) > 0) { sofar = 0; while (bytes > 0) { if ((nbytes = httpWriteBlock(conn->writeq, &buf[sofar], bytes)) < 0) { mprCloseFile(file); return MPR_ERR_CANT_WRITE; } bytes -= nbytes; sofar += nbytes; mprAssert(bytes >= 0); } mprYield(0); } httpFlushQueue(conn->writeq, 1); mprSetSocketBlockingMode(conn->sock, oldMode); mprCloseFile(file); app->inFile = 0; } } if (app->bodyData) { mprAddNullToBuf(app->bodyData); len = mprGetBufLength(app->bodyData); if (httpWriteBlock(conn->writeq, mprGetBufStart(app->bodyData), len) != len) { return MPR_ERR_CANT_WRITE; } } } return 0; }