Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ctime>
- #include <iostream>
- #include <iomanip>
- #include <map>
- #include <string>
- #include <string.h>
- std::map<std::string, std::string> http_request;
- void parse_header_old( const char *, std::size_t );
- void parse_header_new( const char *, const char * );
- int main()
- {
- const char msg[]= "GET / HTTP/1.1\r\n"
- "Host: 192.241.213.46:6880\r\n"
- "Upgrade-Insecure-Requests: 1\r\n"
- "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
- "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8\r\n"
- "Accept-Language: en-us\r\n"
- "Accept-Encoding: gzip, deflate\r\n"
- "Connection: keep-alive\r\n\r\n";
- clock_t begin;
- begin = clock();
- parse_header_old( msg, sizeof( msg ) );
- printf("old: %f\n", (double)( clock() - begin ) / CLOCKS_PER_SEC);
- begin = clock();
- parse_header_new( msg, &msg[ sizeof( msg ) - 1 ] );
- printf("new: %f\n", (double)( clock() - begin ) / CLOCKS_PER_SEC);
- }
- /*
- * Before
- */
- void parse_header_old( const char *msg, std::size_t len )
- {
- char *head = (char *) msg;
- char *mid;
- char *tail = head;
- if( len == 0 )
- {
- return;
- }
- // Find request type
- while( *head++ != ' ');
- http_request[ "Type" ] = std::string( ( char * ) msg ).substr( 0 , ( head - 1) - tail );
- // Find path
- tail = head;
- while( *head++ != ' ');
- http_request[ "Path" ] = std::string( ( char * ) msg ).substr( tail - ( char *)msg , ( head - 1) - tail );
- // Find HTTP version
- tail = head;
- while( *head++ != '\r');
- http_request[ "Version" ] = std::string( ( char * ) msg ).substr( tail - ( char *)msg , ( head - 1) - tail );
- // Map all headers from a key to a value
- while( true )
- {
- tail = head + 1;
- while( *head++ != '\r' );
- mid = strstr( tail, ":" );
- // Look for the failed strstr
- if( tail > mid )
- break;
- http_request[ std::string( ( char * ) msg ).substr( tail - ( char *)msg , ( mid ) - tail ) ] = std::string( ( char * ) msg ).substr( mid + 2 - ( char *) msg , ( head - 3 ) - mid );
- }
- }
- void parse_header_new( const char *msg, const char *msg_end )
- {
- const char *head = msg;
- const char *tail = msg;
- // Find request type
- while (tail != msg_end && *tail != ' ') ++tail;
- http_request["Type"] = std::string(head, tail);
- // Find path
- while (tail != msg_end && *tail == ' ') ++tail;
- head = tail;
- while (tail != msg_end && *tail != ' ') ++tail;
- http_request["Path"] = std::string(head, tail);
- // Find HTTP version
- while (tail != msg_end && *tail == ' ') ++tail;
- head = tail;
- while (tail != msg_end && *tail != '\r') ++tail;
- http_request["Version"] = std::string(head, tail);
- if (tail != msg_end) ++tail; // skip '\r'
- if (tail != msg_end) ++tail; // skip '\n'
- // Map all headers from a key to a value
- head = tail;
- while ( head + 2 != msg_end )
- {
- while (tail != msg_end && *tail != '\r') ++tail;
- const char *colon = ( char * )memchr(head, ':', tail - head);
- if (colon == NULL) {
- std::cout << "colon not found." << std::endl;
- return;//break;
- }
- const char *value = colon+1;
- while (value != tail && *value == ' ') ++value;
- http_request[ std::string(head, colon) ] = std::string(value, tail);
- head = tail+2;
- if (tail != msg_end) ++tail; // skip '\r'
- if (tail != msg_end) ++tail; // skip '\n'
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement