static int join(Ejs *ejs, EjsObj *workers, int timeout) { MprTicks mark; int result, remaining; mprTrace(5, "Worker.join: joining %d", ejs->joining); mark = mprGetTicks(); remaining = timeout; do { ejs->joining = !reapJoins(ejs, workers); if (!ejs->joining) { break; } if (mprShouldAbortRequests()) { ejsThrowStateError(ejs, "Program instructed to exit"); break; } mprWaitForEvent(ejs->dispatcher, remaining); remaining = (int) mprGetRemainingTicks(mark, timeout); } while (remaining > 0 && !ejs->exception); if (ejs->exception) { return 0; } result = (ejs->joining) ? MPR_ERR_TIMEOUT: 0; ejs->joining = 0; mprTrace(7, "Worker.join: result %d", result); return result; }
static int issueRequest(HttpConn *conn, cchar *url, MprList *files) { HttpRx *rx; HttpUri *target, *location; char *redirect; cchar *msg, *sep; int count, redirectCount, rc; httpSetRetries(conn, app->retries); httpSetTimeout(conn, app->timeout, app->timeout); for (redirectCount = count = 0; count <= conn->retries && redirectCount < 16 && !mprShouldAbortRequests(conn); count++) { if (prepRequest(conn, files, count) < 0) { return MPR_ERR_CANT_OPEN; } if (sendRequest(conn, app->method, url, files) < 0) { return MPR_ERR_CANT_WRITE; } if ((rc = httpWait(conn, HTTP_STATE_PARSED, conn->limits->requestTimeout)) == 0) { if (httpNeedRetry(conn, &redirect)) { if (redirect) { location = httpCreateUri(redirect, 0); target = httpJoinUri(conn->tx->parsedUri, 1, &location); url = httpUriToString(target, HTTP_COMPLETE_URI); count = 0; } /* Count redirects and auth retries */ redirectCount++; count--; } else { break; } } else if (!conn->error) { if (rc == MPR_ERR_TIMEOUT) { httpError(conn, HTTP_ABORT | HTTP_CODE_REQUEST_TIMEOUT, "Inactive request timed out, exceeded request timeout %d", app->timeout); } else { httpError(conn, HTTP_ABORT | HTTP_CODE_COMMS_ERROR, "Connection I/O error"); } } if ((rx = conn->rx) != 0) { if (rx->status == HTTP_CODE_REQUEST_TOO_LARGE || rx->status == HTTP_CODE_REQUEST_URL_TOO_LARGE || (rx->status == HTTP_CODE_UNAUTHORIZED && conn->authUser == 0)) { /* No point retrying */ break; } } mprLog(MPR_DEBUG, "retry %d of %d for: %s %s", count, conn->retries, app->method, url); } // MOB - comment out errorMsg as auth errors were returning errors here. if (conn->error /* || conn->errorMsg */) { msg = (conn->errorMsg) ? conn->errorMsg : ""; sep = (msg && *msg) ? "\n" : ""; mprError("http: failed \"%s\" request for %s after %d attempt(s).%s%s", app->method, url, count, sep, msg); return MPR_ERR_CANT_CONNECT; } return 0; }
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; }
PUBLIC void httpEnableConnEvents(HttpConn *conn) { if (mprShouldAbortRequests() || conn->borrowed) { return; } /* Used by ejs */ if (conn->workerEvent) { MprEvent *event = conn->workerEvent; conn->workerEvent = 0; mprQueueEvent(conn->dispatcher, event); return; } httpSetupWaitHandler(conn, httpGetConnEventMask(conn)); }
/* Wait for a command to complete. Return 0 if the command completed, otherwise it will return MPR_ERR_TIMEOUT. */ PUBLIC int mprWaitForCmd(MprCmd *cmd, MprTicks timeout) { MprTicks expires, remaining, delay; int64 dispatcherMark; assert(cmd); if (timeout < 0) { timeout = MAXINT; } if (mprGetDebugMode()) { timeout = MAXINT; } if (cmd->stopped) { timeout = 0; } expires = mprGetTicks() + timeout; remaining = timeout; /* Add root to allow callers to use mprRunCmd without first managing the cmd */ mprAddRoot(cmd); dispatcherMark = mprGetEventMark(cmd->dispatcher); while (!cmd->complete && remaining > 0) { if (mprShouldAbortRequests()) { break; } delay = (cmd->eofCount >= cmd->requiredEof) ? 10 : remaining; if (!MPR->eventing) { mprServiceEvents(delay, MPR_SERVICE_NO_BLOCK); delay = 0; } mprWaitForEvent(cmd->dispatcher, delay, dispatcherMark); remaining = (expires - mprGetTicks()); dispatcherMark = mprGetEventMark(cmd->dispatcher); } mprRemoveRoot(cmd); if (cmd->pid) { return MPR_ERR_TIMEOUT; } return 0; }
static int issueRequest(HttpConn *conn, cchar *url, MprList *files) { HttpRx *rx; HttpUri *target, *location; char *redirect; cchar *msg, *sep, *authType; int count, redirectCount, rc; httpSetRetries(conn, app->retries); httpSetTimeout(conn, app->timeout, app->timeout); authType = conn->authType; for (redirectCount = count = 0; count <= conn->retries && redirectCount < 10 && !mprShouldAbortRequests(conn); count++) { if (prepRequest(conn, files, count) < 0) { return MPR_ERR_CANT_OPEN; } if (sendRequest(conn, app->method, url, files) < 0) { return MPR_ERR_CANT_WRITE; } if ((rc = httpWait(conn, HTTP_STATE_PARSED, conn->limits->requestTimeout)) == 0) { if (httpNeedRetry(conn, &redirect)) { if (redirect) { httpRemoveHeader(conn, "Host"); location = httpCreateUri(redirect, 0); if (!location || !location->valid) { httpError(conn, HTTP_ABORT, "Invalid location URI"); break; } target = httpJoinUri(conn->tx->parsedUri, 1, &location); url = httpUriToString(target, HTTP_COMPLETE_URI); count = 0; } if (conn->rx && conn->rx->status == HTTP_CODE_UNAUTHORIZED && authType && smatch(authType, conn->authType)) { /* Supplied authentication details and failed */ break; } redirectCount++; count--; } else { break; } } else if (!conn->error) { if (rc == MPR_ERR_TIMEOUT) { httpError(conn, HTTP_ABORT | HTTP_CODE_REQUEST_TIMEOUT, "Inactive request timed out, exceeded request timeout %lld", app->timeout); } else { httpError(conn, HTTP_ABORT | HTTP_CODE_COMMS_ERROR, "Connection I/O error"); } } if ((rx = conn->rx) != 0) { if (rx->status == HTTP_CODE_REQUEST_TOO_LARGE || rx->status == HTTP_CODE_REQUEST_URL_TOO_LARGE || rx->status == HTTP_CODE_NOT_ACCEPTABLE || (rx->status == HTTP_CODE_UNAUTHORIZED && conn->username == 0)) { /* No point retrying */ break; } if (conn->sock->flags & MPR_SOCKET_CERT_ERROR) { break; } } mprDebug("http", 4, "retry %d of %d for: %s %s", count, conn->retries, app->method, url); } if (conn->error) { msg = (conn->errorMsg) ? conn->errorMsg : ""; sep = (msg && *msg) ? "\n" : ""; mprLog("error http", 0, "Failed \"%s\" request for %s%s%s", app->method, url, sep, msg); return MPR_ERR_CANT_CONNECT; } return 0; }