/// returns a string representation of the HTTP version (i.e. "HTTP/1.1") inline std::string get_version_string(void) const { std::string http_version(STRING_HTTP_VERSION); http_version += boost::lexical_cast<std::string>(get_version_major()); http_version += '.'; http_version += boost::lexical_cast<std::string>(get_version_minor()); return http_version; }
std::string request_line::to_string() const { std::string output(method_); output += ' ' + uri_ + ' ' + http_version(major_version_, minor_version_) + CRLF; return output; }
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 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 ) ); }
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; }
/* creates a request structure with all the relevant information */ request *request_info(int fd, const char *addr, const char *req) { struct tm *tm_time; request *r; time_t tim; int len = PATH_MAX; r = calloc(1, sizeof(request)); r->fd = fd; r->client = strdup((char*)addr); r->req = (char*)req; r->meth = method_type(req); r->reqfile = requ_file(req); r->file = filename(r->reqfile); r->http = http_version(req); r->status = 200; r->keep_alive = 300; /* get the document root */ r->doc_root = malloc(len); while((getcwd(r->doc_root, len++)) == NULL) r->doc_root = realloc(r->doc_root, len); /* get date */ time(&tim); tm_time = gmtime(&tim); r->date = malloc(64); strftime(r->date, 64, "%a, %d %b %Y %T GMT", tm_time); /* bad method */ if(r->meth == INVALID) { r->status = 400; return r; } /* unsupported method? */ if((r->meth != GET) && (r->meth != HEAD) && (r->meth != POST)) { r->status = 501; return r; } /* no http version */ if(!r->http) { r->close_conn = 1; r->status = 400; return r; } /* unsupported major http version */ if(strncmp(r->http, "HTTP/1.", 7) != 0) { r->status = 505; return r; } /* check the file is proper */ if(*r->reqfile != '/') { r->status = 400; return r; } /* close connection for HTTP 1.0 */ if(strcmp(r->http, "HTTP/1.0") == 0) r->close_conn = 1; file_stuff(r); return r; }