Advertisement
Guest User

http parsing speed

a guest
Mar 6th, 2017
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | None | 0 0
  1. #include <ctime>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <map>
  5. #include <string>
  6. #include <string.h>
  7.  
  8. std::map<std::string, std::string> http_request;
  9.  
  10. void parse_header_old( const char *, std::size_t );
  11. void parse_header_new( const char *, const char * );
  12.  
  13. int main()
  14. {
  15.     const char msg[]= "GET / HTTP/1.1\r\n"
  16.                       "Host: 192.241.213.46:6880\r\n"
  17.                       "Upgrade-Insecure-Requests: 1\r\n"
  18.                       "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
  19.                       "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"
  20.                       "Accept-Language: en-us\r\n"
  21.                       "Accept-Encoding: gzip, deflate\r\n"
  22.                       "Connection: keep-alive\r\n\r\n";
  23.  
  24.     clock_t begin;
  25.  
  26.     begin = clock();
  27.    
  28.     parse_header_old( msg, sizeof( msg ) );
  29.    
  30.     printf("old: %f\n", (double)( clock() - begin ) / CLOCKS_PER_SEC);
  31.    
  32.    
  33.     begin = clock();
  34.    
  35.     parse_header_new( msg, &msg[ sizeof( msg ) - 1 ] );
  36.  
  37.     printf("new: %f\n", (double)( clock() - begin ) / CLOCKS_PER_SEC);
  38.    
  39. }
  40.  
  41. /*
  42.  * Before
  43.  */
  44. void parse_header_old( const char *msg, std::size_t len )
  45. {
  46.     char *head = (char *) msg;
  47.     char *mid;
  48.     char *tail = head;
  49.  
  50.     if( len == 0 )
  51.     {
  52.         return;
  53.     }
  54.  
  55.     // Find request type
  56.     while( *head++ != ' ');
  57.     http_request[ "Type" ] = std::string( ( char * ) msg ).substr( 0 , ( head - 1) - tail );
  58.  
  59.     // Find path
  60.     tail = head;
  61.     while( *head++ != ' ');
  62.     http_request[ "Path" ] = std::string( ( char * ) msg ).substr( tail - ( char *)msg , ( head - 1) - tail );
  63.  
  64.     // Find HTTP version
  65.     tail = head;
  66.     while( *head++ != '\r');
  67.     http_request[ "Version" ] = std::string( ( char * ) msg ).substr( tail - ( char *)msg , ( head - 1) - tail );
  68.  
  69.     // Map all headers from a key to a value
  70.     while( true )
  71.     {
  72.         tail = head + 1;
  73.         while( *head++ != '\r' );
  74.         mid = strstr( tail, ":" );  
  75.  
  76.         // Look for the failed strstr  
  77.         if( tail > mid )
  78.             break;
  79.  
  80.         http_request[ std::string( ( char * ) msg ).substr( tail - ( char *)msg , ( mid ) - tail  ) ] = std::string( ( char * ) msg ).substr( mid + 2 - ( char *) msg , ( head - 3 ) - mid );
  81.     }
  82. }
  83.  
  84. void parse_header_new( const char *msg, const char *msg_end )
  85. {
  86.     const char *head = msg;
  87.     const char *tail = msg;
  88.  
  89.     // Find request type
  90.     while (tail != msg_end && *tail != ' ') ++tail;
  91.     http_request["Type"] = std::string(head, tail);
  92.  
  93.     // Find path
  94.     while (tail != msg_end && *tail == ' ') ++tail;
  95.     head = tail;
  96.     while (tail != msg_end && *tail != ' ') ++tail;
  97.     http_request["Path"] = std::string(head, tail);
  98.  
  99.     // Find HTTP version
  100.     while (tail != msg_end && *tail == ' ') ++tail;
  101.     head = tail;
  102.     while (tail != msg_end && *tail != '\r') ++tail;
  103.     http_request["Version"] = std::string(head, tail);
  104.     if (tail != msg_end) ++tail;  // skip '\r'
  105.     if (tail != msg_end) ++tail;  // skip '\n'
  106.  
  107.     // Map all headers from a key to a value
  108.     head = tail;
  109.     while ( head + 2 != msg_end )
  110.     {
  111.         while (tail != msg_end && *tail != '\r') ++tail;
  112.         const char *colon = ( char * )memchr(head, ':', tail - head);
  113.         if (colon == NULL) {
  114.             std::cout << "colon not found." << std::endl;
  115.             return;//break;
  116.         }
  117.         const char *value = colon+1;
  118.         while (value != tail && *value == ' ') ++value;
  119.         http_request[ std::string(head, colon) ] = std::string(value, tail);
  120.         head = tail+2;
  121.         if (tail != msg_end) ++tail;  // skip '\r'
  122.         if (tail != msg_end) ++tail;  // skip '\n'
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement