コード例 #1
0
ファイル: http.c プロジェクト: adammendoza/http
static int doRequest(HttpConn *conn, cchar *url, MprList *files)
{
    MprTicks        mark, remaining;
    HttpLimits      *limits;
    MprFile         *outFile;
    cchar           *path;

    assert(url && *url);
    limits = conn->limits;

    mprTrace(4, "fetch: %s %s", app->method, url);
    mark = mprGetTicks();

    if (issueRequest(conn, url, files) < 0) {
        return MPR_ERR_CANT_CONNECT;
    }
    remaining = limits->requestTimeout;

    if (app->outFilename) {
        path = app->loadThreads > 1 ? sfmt("%s-%s.tmp", app->outFilename, mprGetCurrentThreadName()): app->outFilename;
        if ((outFile = mprOpenFile(path, O_CREAT | O_WRONLY | O_TRUNC | O_TEXT, 0664)) == 0) {
            mprError("Cannot open %s", path);
            return MPR_ERR_CANT_OPEN;
        }
    } else {
        outFile = mprGetStdout();
    }
    mprAddRoot(outFile);
    while (!conn->tx->finalized && conn->state < HTTP_STATE_COMPLETE && remaining > 0) {
        remaining = mprGetRemainingTicks(mark, limits->requestTimeout);
        readBody(conn, outFile);
        httpWait(conn, 0, remaining);
    }
    if (conn->state < HTTP_STATE_COMPLETE && !conn->error) {
        httpError(conn, HTTP_ABORT | HTTP_CODE_REQUEST_TIMEOUT,
            "Inactive request timed out, exceeded request timeout %d", app->timeout);
    } else {
        readBody(conn, outFile);
    }
    if (app->outFilename) {
        mprCloseFile(outFile);
    }
    mprRemoveRoot(outFile);
    reportResponse(conn, url, mprGetTicks() - mark);
    httpDestroyRx(conn->rx);
    httpDestroyTx(conn->tx);
    return 0;
}
コード例 #2
0
ファイル: http.c プロジェクト: varphone/ejs-2
static void showOutput(HttpConn *conn, cchar *buf, ssize count)
{
    HttpRx      *rx;
    int         i, c;
    
    rx = conn->rx;

    if (app->noout) {
        return;
    }
    if (rx->status == 401 || (conn->followRedirects && (301 <= rx->status && rx->status <= 302))) {
        return;
    }
    if (app->outFile == 0) {
        if (app->outFilename) {
            if ((app->outFile = mprOpenFile(app->outFilename, O_CREAT | O_WRONLY | O_TRUNC | O_TEXT, 0664)) == 0) {
                mprError("Can't open %s", app->outFile);
                return;
            }
        } else {
            app->outFile = mprGetStdout();
        }
    }
    if (!app->printable) {
        mprWriteFile(app->outFile, buf, count);
        return;
    }
    for (i = 0; i < count; i++) {
        if (!isprint((uchar) buf[i]) && buf[i] != '\n' && buf[i] != '\r' && buf[i] != '\t') {
            app->isBinary = 1;
            break;
        }
    }
    if (!app->isBinary) {
        mprWriteFile(app->outFile, buf, count);
        return;
    }
    for (i = 0; i < count; i++) {
        c = (uchar) buf[i];
        if (app->printable && app->isBinary) {
            mprFprintf(app->outFile, "%02x ", c & 0xff);
        } else {
            mprFprintf(app->outFile, "%c", (uchar) buf[i]);
        }
    }
}
コード例 #3
0
ファイル: http.c プロジェクト: gitorup/appweb
static int doRequest(HttpConn *conn, cchar *url, MprList *files)
{
    MprFile     *outFile;
    cchar       *path;

    assert(url && *url);

    if (issueRequest(conn, url, files) < 0) {
        if (conn->rx && conn->rx->status) {
            reportResponse(conn, url);
        }
        return MPR_ERR_CANT_CONNECT;
    }
    if (app->outFilename) {
        path = app->loadThreads > 1 ? sfmt("%s-%s.tmp", app->outFilename, mprGetCurrentThreadName()): app->outFilename;
        if ((outFile = mprOpenFile(path, O_CREAT | O_WRONLY | O_TRUNC | O_TEXT, 0664)) == 0) {
            mprLog("error http", 0, "Cannot open %s", path);
            return MPR_ERR_CANT_OPEN;
        }
    } else {
        outFile = mprGetStdout();
    }
    mprAddRoot(outFile);
    readBody(conn, outFile);
    while (conn->state < HTTP_STATE_COMPLETE && !httpRequestExpired(conn, -1)) {
        readBody(conn, outFile);
        httpWait(conn, 0, -1);
    }
    if (conn->state < HTTP_STATE_COMPLETE && !conn->error) {
        httpError(conn, HTTP_ABORT | HTTP_CODE_REQUEST_TIMEOUT, "Request timed out");
    }
    if (app->outFilename) {
        mprCloseFile(outFile);
    }
    mprRemoveRoot(outFile);
    reportResponse(conn, url);
    httpDestroyRx(conn->rx);
    httpDestroyTx(conn->tx);
    return 0;
}
コード例 #4
0
ファイル: runProgram.c プロジェクト: embedthis/mpr-3
MAIN(runProgramMain, int argc, char* argv[])
{
    Mpr     *mpr;
    MprFile *out;
    char    buf[256], *ep;
    int     i, len, exitCode, sofar;

    mpr = mprCreate(argc, argv, NULL);

#if TRACE_PROGRESS
    MprFile *f = mprOpen(mpr, "/tmp/r.log", O_CREAT|O_TRUNC|O_WRONLY, 0664);
    mprWriteFormat(f, "runProgram: argc %d\n", argc);
    for (i = 0; i < argc; i++) {
        mprWriteFormat(f, "runProgram: arg[%d] = %s\n", i, argv[i]);
    }
    mprFree(f);
#endif

    if (argc < 2) {
        mprPrintfError(mpr, "Usage: runProgram exitCode args...\n");
        exit(3);
    }
    exitCode = atoi(argv[1]);
    out = mprGetStdout(mpr);

    if (exitCode != 99) {
        /*
         *  Echo the args
         */
        for (i = 2; i < argc; ) {
            mprPuts(out, argv[i]);
            if (++i != argc) {
                mprPutc(out, ' ');
            }
        }
        mprPutc(out, '\n');

        /*
         *  Echo the CMD_ENV environment variable value
         */
        ep = getenv("CMD_ENV");
        if (ep) {
            mprPuts(out, "CMD_ENV=");
            mprPuts(out, ep);
        } else {
            mprPuts(out, "Can't find CMD_ENV");
        }
        mprPutc(out, '\n');
        mprFlush(out);
    }

    /*
     *  Read the input
     */
    sofar = 0;
    while ((len = (int) read(0, buf, sizeof(buf))) > 0) {
        sofar += (int) write(1, buf, len);
    }
    if (exitCode != 99) {
        mprPuts(out, "END");
        mprPutc(out, '\n');
    }
    mprFlush(out);
    return exitCode;
}