static void parse_response( void ) { char res[] = "HTTP/1.1 200 OK\r\n" "Server: GitHub.com\r\n" "Date: Sat, 27 Jun 2015 04:10:06 GMT\r\n" "Content-Type: text/html; charset=utf-8\r\n" "Transfer-Encoding: chunked\r\n" "Status: 200 OK\r\n" "Content-Security-Policy: default-src *; script-src assets-cdn.github.com collector-cdn.github.com; object-src assets-cdn.github.com; style-src 'self' 'unsafe-inline' 'unsafe-eval' assets-cdn.github.com; img-src 'self' data: assets-cdn.github.com identicons.github.com www.google-analytics.com collector.githubapp.com *.githubusercontent.com *.gravatar.com *.wp.com; media-src 'none'; frame-src 'self' render.githubusercontent.com gist.github.com www.youtube.com player.vimeo.com checkout.paypal.com; font-src assets-cdn.github.com; connect-src 'self' live.github.com wss://live.github.com uploads.github.com status.github.com api.github.com www.google-analytics.com github-cloud.s3.amazonaws.com\r\n" "Cache-Control: no-cache\r\n" "Vary: X-PJAX\r\n" "X-UA-Compatible: IE=Edge,chrome=1\r\n" "Set-Cookie: logged_in=no; domain=.github.com; path=/; expires=Wed, 27 Jun 2035 04:10:06 -0000; secure; HttpOnly\r\n" "X-Request-Id: 8ea8d359ded1d2d30094d38b2a4e73d3\r\n" "X-Runtime: 0.037659\r\n" "X-GitHub-Request-Id: 999DDDE5:0A3A:102882A:558E221D\r\n" "Strict-Transport-Security: max-age=31536000; includeSubdomains; preload\r\n" "X-Content-Type-Options: nosniff\r\n" "X-XSS-Protection: 1; mode=block\r\n" "X-Frame-Options: deny\r\n" "Vary: Accept-Encoding\r\n" "X-Served-By: 1868c9f28a71d80b2987f48dbd1824a0\r\n" "\r\n"; size_t len = sizeof( res ); uint64_t i = 0; int rc = 0; float start = 0, end = 0, elapsed = 0; uint16_t maxhdrlen = UINT16_MAX; http_t *r = http_alloc(20); start = (float)clock()/CLOCKS_PER_SEC; for( i = 0; i < NLOOP; i++ ){ rc = http_res_parse( r, res, len, maxhdrlen ); assert( rc == HTTP_SUCCESS ); assert( http_version(r) == HTTP_V11 ); assert( http_status(r) == HTTP_OK ); assert( r->nheader == 19 ); 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 ) ); }
static inline int parse_response( 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_res_parse( r, buf, len, 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, "status", http_status( r ) ); lstate_strl2tbl( L, "reason", buf + r->msg, r->msglen ); // initialize http_init( r ); } // add status lua_pushinteger( L, rc ); return 1; }