Esempio n. 1
0
/*  
    Run the web client to retrieve a URI
    This will create the MPR and Http service on demand. As such, it is not the most
    efficient way to run a web request.
    @return HTTP status code or negative MPR error code. Returns a malloc string in response.
 */
PUBLIC int maRunWebClient(cchar *method, cchar *uri, cchar *data, char **response, char **err)
{
    Mpr         *mpr;
    HttpConn    *conn;
    int         status;

    if (err) {
        *err = 0;
    }
    if (response) {
        *response = 0;
    }
    if ((mpr = mprCreate(0, NULL, 0)) == 0) {
        mprLog("error appweb", 0, "Cannot create the MPR runtime");
        return MPR_ERR_CANT_CREATE;
    }
    if (mprStart() < 0) {
        mprLog("error appweb", 0, "Cannot start the web server runtime");
        return MPR_ERR_CANT_INITIALIZE;
    }
    httpCreate(HTTP_CLIENT_SIDE);
    conn = httpRequest(method, uri, data, err);
    status = httpGetStatus(conn);
    if (response) {
        *response = httpReadString(conn);
    }
    mprDestroy();
    return status;
}
Esempio n. 2
0
/*  
    Return the value of a keyword in the content returned from the last request
    Format either: 
        KEYWORD=value<
        KEYWORD: value,
        "KEYWORD": value,
        "KEYWORD": value,
    Return 0 on errors. Caller must free result.
 */
char *lookupValue(MprTestGroup *gp, char *key)
{
    char    *nextToken, *bp, *result;

    if (gp->content == NULL) {
        gp->content = httpReadString(getConn(gp));
    }
    if (gp->content == 0 || (nextToken = strstr(gp->content, key)) == 0) {
        return 0;
    }
    nextToken += slen(key);
    if (*nextToken != '=' && *nextToken != ':' && *nextToken != '"') {
        return 0;
    }
    if (*nextToken == '"') {
        nextToken++;
    }
    if (*nextToken == ':') {
        nextToken += 2;
    } else {
        nextToken += 1;
    }
    result = sclone(nextToken);
    for (bp = result; *bp && *bp != '<' && *bp != ','; bp++) {
        ;
    }
    *bp++ = '\0';
    if (scmp(result, "null") == 0) {
        return 0;
    }
    return result;
}
Esempio n. 3
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;
}
Esempio n. 4
0
MAIN(simpleClient, int argc, char** argv)
{
    Mpr     *mpr;
    App     *app;
    cchar   *content;
    int     code;

    /* 
       Create the Multithreaded Portable Runtime
     */
    mpr = mprCreate(argc, argv, MPR_USER_EVENTS_THREAD);
    if ((app = mprAllocObj(App, manageApp)) == 0) {
        return MPR_ERR_MEMORY;
    }
    mprAddRoot(app);
    mprAddStandardSignals(app);

    /* 
       Start the Multithreaded Portable Runtime
     */
    mprStart();

    /* 
       Create an Http service object
     */
    app->http = httpCreate(mpr);

    /* 
       Get a client http object to work with. We can issue multiple requests with this one object.
     */
    app->conn = httpCreateConn(app->http, NULL, NULL);

    /* 
       Get a URL
     */
    if (httpConnect(app->conn, "GET", "http://www.embedthis.com/index.html") < 0) {
        mprError("Can't get URL");
        exit(2);
    }

    /* 
       Examine the HTTP response HTTP code. 200 is success.
     */
    code = httpGetStatus(app->conn);
    if (code != 200) {
        mprError("Server responded with code %d\n", code);
        exit(1);
    } 

    /* 
       Get the actual response content
     */
    content = httpReadString(app->conn);
    if (content) {
        mprPrintf("Server responded with: %s\n", content);
    }
    mprDestroy(MPR_EXIT_DEFAULT);
    return 0;
}
Esempio n. 5
0
MAIN(simpleClient, int argc, char **argv, char **envp)
{
    Http        *http;
    HttpConn    *conn;
    cchar       *content;
    int         code;

    /* 
       Create the Multithreaded Portable Runtime and start it.
     */
    mprCreate(argc, argv, 0);
    mprStart();

    /* 
       Get a client http object to work with. We can issue multiple requests with this one object.
       Add the conn as a root object so the GC won't collect it while we are using it.
     */
    http = httpCreate(HTTP_CLIENT_SIDE);
    conn = httpCreateConn(http, NULL, NULL);
    mprAddRoot(conn);

    /* 
        Open a connection to issue the GET. Then finalize the request output - this forces the request out.
     */
    if (httpConnect(conn, "GET", "http://www.embedthis.com/index.html", NULL) < 0) {
        mprError("Can't get URL");
        exit(2);
    }
    httpFinalizeOutput(conn);

    /*
        Wait for a response
     */
    if (httpWait(conn, HTTP_STATE_PARSED, 10000) < 0) {
        mprError("No response");
        exit(2);
    }

    /* 
       Examine the HTTP response HTTP code. 200 is success.
     */
    code = httpGetStatus(conn);
    if (code != 200) {
        mprError("Server responded with code %d\n", code);
        exit(1);
    } 

    /* 
       Get the actual response content
     */
    content = httpReadString(conn);
    if (content) {
        mprPrintf("Server responded with: %s\n", content);
    }
    mprDestroy(MPR_EXIT_DEFAULT);
    return 0;
}
Esempio n. 6
0
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;
}
Esempio n. 7
0
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;
}