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; }
/* function get contentLength(): Number */ static EjsNumber *http_contentLength(Ejs *ejs, EjsHttp *hp, int argc, EjsObj **argv) { MprOff length; if (!waitForResponseHeaders(hp)) { return 0; } length = httpGetContentLength(hp->conn); return ejsCreateNumber(ejs, (MprNumber) length); }
/* function get available(): Number DEPRECATED 1.0.0B3 (11/09) */ static EjsNumber *http_available(Ejs *ejs, EjsHttp *hp, int argc, EjsObj **argv) { MprOff len; if (!waitForResponseHeaders(hp)) { return 0; } len = httpGetContentLength(hp->conn); if (len > 0) { return ejsCreateNumber(ejs, (MprNumber) len); } /* Probably should be returning null here */ return (EjsNumber*) ESV(minusOne); }
/* function read(buffer: ByteArray, offset: Number = 0, count: Number = -1): Number Returns a count of bytes read. Non-blocking if a callback is defined. Otherwise, blocks. Offset: -1 then read to the buffer write position, >= 0 then read to that offset count: -1 then read as much as the buffer will hold. If buffer is growable, read all content. If not growable, read the buffer size. If count >= 0, then read that number of bytes. */ static EjsNumber *http_read(Ejs *ejs, EjsHttp *hp, int argc, EjsObj **argv) { EjsByteArray *buffer; HttpConn *conn; MprOff contentLength; ssize offset, count; conn = hp->conn; buffer = (EjsByteArray*) argv[0]; offset = (argc >= 2) ? ejsGetInt(ejs, argv[1]) : 0; count = (argc >= 3) ? ejsGetInt(ejs, argv[2]): -1; if (!waitForResponseHeaders(hp)) { return 0; } contentLength = httpGetContentLength(conn); if (conn->state >= HTTP_STATE_PARSED && contentLength == hp->readCount) { /* End of input */ return ESV(null); } if (offset < 0) { offset = buffer->writePosition; } else if (offset < buffer->size) { ejsSetByteArrayPositions(ejs, buffer, offset, offset); } else { ejsThrowOutOfBoundsError(ejs, "Bad read offset value"); return 0; } if (count < 0 && !buffer->resizable) { count = buffer->size - offset; } if ((count = readHttpData(ejs, hp, count)) < 0) { assert(ejs->exception); return 0; } else if (count == 0 && conn->state > HTTP_STATE_CONTENT) { return ESV(null); } hp->readCount += count; if (ejsCopyToByteArray(ejs, buffer, offset, (char*) mprGetBufStart(hp->responseContent), count) != count) { ejsThrowMemoryError(ejs); } ejsSetByteArrayPositions(ejs, buffer, -1, buffer->writePosition + count); mprAdjustBufStart(hp->responseContent, count); mprResetBufIfEmpty(hp->responseContent); return ejsCreateNumber(ejs, (MprNumber) count); }
bool simpleForm(MprTestGroup *gp, char *uri, char *formData, int expectStatus) { HttpConn *conn; MprOff contentLen; ssize len; int status; contentLen = 0; if (expectStatus <= 0) { expectStatus = 200; } if (startRequest(gp, "POST", uri) < 0) { return 0; } conn = getConn(gp); if (formData) { httpSetHeader(conn, "Content-Type", "application/x-www-form-urlencoded"); len = slen(formData); if (httpWrite(conn->writeq, formData, len) != len) { return MPR_ERR_CANT_WRITE; } } httpFinalizeOutput(conn); if (httpWait(conn, HTTP_STATE_COMPLETE, -1) < 0) { return MPR_ERR_CANT_READ; } status = httpGetStatus(conn); if (status != expectStatus) { mprLog("appweb test form", 0, "Client failed for %s, response code: %d, msg %s", uri, status, httpGetStatusMessage(conn)); return 0; } gp->content = httpReadString(conn); contentLen = httpGetContentLength(conn); if (! tassert(gp->content != 0 && contentLen > 0)) { return 0; } return 1; }
bool simplePost(MprTestGroup *gp, char *uri, char *bodyData, ssize len, int expectStatus) { HttpConn *conn; MprOff contentLen; int status; contentLen = 0; conn = getConn(gp); if (expectStatus <= 0) { expectStatus = 200; } if (startRequest(gp, "POST", uri) < 0) { return 0; } if (bodyData) { if (httpWrite(conn->writeq, bodyData, len) != len) { return MPR_ERR_CANT_WRITE; } } httpFinalizeOutput(conn); if (httpWait(conn, HTTP_STATE_COMPLETE, -1) < 0) { return MPR_ERR_CANT_READ; } status = httpGetStatus(conn); if (status != expectStatus) { mprLog("appweb test post", 0, "Client failed for %s, response code: %d, msg %s", uri, status, httpGetStatusMessage(conn)); return 0; } gp->content = httpReadString(conn); contentLen = httpGetContentLength(conn); if (! tassert(gp->content != 0 && contentLen > 0)) { return 0; } return 1; }
int Http_PostDataGet(HTTPCONNECTION hConnection, int *piPostState, char **ppcPostBuf, int *piPostBufLen, int *piPostDataLen, char *pcFillData, int iFillDataLen, int iIsMoreData/*bool*/, void *pParam/*other parameter for extend use*/) { if (*piPostBufLen == 0) {//初始状态, 尚未接收任何post数据 *piPostBufLen = httpGetContentLength(hConnection) + 8; if (*piPostBufLen < 8) *piPostBufLen = 8; if (*piPostBufLen < MAX_POST_LENGTH) { if ((*ppcPostBuf = (char *)malloc(*piPostBufLen)) == NULL) { PRINT_MEM_OUT; } *piPostDataLen = 0; } } if (*ppcPostBuf == NULL) {//非初始状态, 分配失败或数据太长 if (iIsMoreData == 0) { httpAddBodyString(hConnection, "Too many post data or not enmough memory."); httpSetHeader(hConnection, 200, "OK", NULL, NULL, "text/plain", TRUE); return 1; } } else {//非初始状态, 分配成功 if (*piPostDataLen + iFillDataLen < *piPostBufLen) { memcpy(*ppcPostBuf + *piPostDataLen, pcFillData, iFillDataLen); *piPostDataLen += iFillDataLen; } if (iIsMoreData == 0) { (*ppcPostBuf)[*piPostDataLen] = '\0'; if (*piPostDataLen >= 1 && (*ppcPostBuf)[*piPostDataLen - 1] == '\n') { (*ppcPostBuf)[--*piPostDataLen] = '\0'; if (*piPostDataLen >= 1 && (*ppcPostBuf)[*piPostDataLen - 1] == '\r') (*ppcPostBuf)[--*piPostDataLen] = '\0'; } if (pParam != NULL) { CO_PARAM_T *pCoParam = (CO_PARAM_T *)pParam; (*pCoParam->funCmdCaller)(hConnection, *ppcPostBuf, *piPostDataLen, (pCoParam->pParam)); free(pCoParam); httpSetPostDataFun(hConnection, NULL, NULL); } free(*ppcPostBuf); *ppcPostBuf = NULL; *piPostDataLen = *piPostBufLen = 0; return 1; } } return 1; }
MprOff espGetContentLength(HttpConn *conn) { return httpGetContentLength(conn); }