Ejemplo n.º 1
0
Archivo: bench.c Proyecto: ifzz/libhttp
static void parse_request( void )
{
    char req[] = 
        "GET /mah0x211/libhttp HTTP/1.1\r\n"
        "Host: github.com\r\n"
        "Connection: keep-alive\r\n"
        "Keep-Alive: 115\r\n"
        "Cache-Control: max-age=0\r\n"
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n"
        "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36\r\n"
        "Accept-Encoding: gzip, deflate, sdch\r\n"
        "Accept-Language: ja,en-US;q=0.8,en;q=0.6\r\n"
        "Accept-Charset: Shift_JIS,utf-8;q=0.7,*;q=0.7\r\n"
        "Cookie: __utma=xxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.x; _octo=XXX.X.XXXXXXXX.XXXXXXXXXX; logged_in=XX; _ga=XXX.X.XXXXXXXXX.XXXXXXXXXX\r\n"
        "\r\n";
    size_t len = sizeof( req ) + 1;
    uint64_t i = 0;
    int rc = 0;
    float start = 0, end = 0, elapsed = 0;
    uint16_t maxurilen = UINT16_MAX;
    uint16_t maxhdrlen = UINT16_MAX;
    uint8_t nheader = 20;
    http_t *r = http_alloc( nheader );
    
    start = (float)clock()/CLOCKS_PER_SEC;
    for( i = 0; i < NLOOP; i++ ){
        rc = http_req_parse( r, req, len, maxurilen, maxhdrlen );
        assert( rc == HTTP_SUCCESS );
        assert( http_method(r) == HTTP_MGET );
        assert( http_version(r) == HTTP_V11 );
        assert( r->nheader == 10 );
        //keys( r, req );
        http_init( r );
    }
    end = (float)clock()/CLOCKS_PER_SEC;
    elapsed = end - start;
    
    http_free( r );
    
    printf("\tElapsed %f seconds.\n", elapsed );
    printf("\t%0.9f -> %f req/sec.\n", elapsed / NLOOP, 1.00000 / ( elapsed / NLOOP ) );
}
Ejemplo n.º 2
0
static inline int parse_request( lua_State *L, lhttp_t *h, char *buf, size_t len )
{
    http_t *r = h->r;
    int rc = 0;

    luaL_checktype( L, 2, LUA_TTABLE );
    luaL_checktype( L, 3, LUA_TTABLE );
    rc = http_req_parse( r, buf, len, h->maxurilen, h->maxhdrlen );
    if( rc == HTTP_SUCCESS )
    {
        uint8_t i = 0;
        uint8_t nheader = r->nheader;
        uintptr_t key = 0;
        uint16_t klen = 0;
        uintptr_t val = 0;
        uint16_t vlen = 0;

        // export to table
        lua_settop( L, 3 );
        // add to header table
        for(; i < nheader; i++ ){
            http_getheader_at( r, &key, &klen, &val, &vlen, i );
            lstate_strll2tbl( L, buf + key, klen, buf + val, vlen );
        }
        lua_pop( L, 1 );
        lstate_num2tbl( L, "version", http_version( r ) );
        lstate_num2tbl( L, "method", http_method( r ) );
        lstate_strl2tbl( L, "uri", buf + r->msg, r->msglen );
        // initialize
        http_init( r );
    }

    // add status
    lua_pushinteger( L, rc );

    return 1;
}