PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - sapi/cli - php_http_parser.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 0 695 0.0 %
Date: 2015-07-31 Functions: 0 4 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright 2009,2010 Ryan Dahl <ry@tinyclouds.org>
       2             :  *
       3             :  * Permission is hereby granted, free of charge, to any person obtaining a copy
       4             :  * of this software and associated documentation files (the "Software"), to
       5             :  * deal in the Software without restriction, including without limitation the
       6             :  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
       7             :  * sell copies of the Software, and to permit persons to whom the Software is
       8             :  * furnished to do so, subject to the following conditions:
       9             :  *
      10             :  * The above copyright notice and this permission notice shall be included in
      11             :  * all copies or substantial portions of the Software.
      12             :  *
      13             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      14             :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      15             :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      16             :  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      17             :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      18             :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
      19             :  * IN THE SOFTWARE.
      20             :  */
      21             : #include <assert.h>
      22             : #include <stddef.h>
      23             : #include "php_http_parser.h"
      24             : 
      25             : 
      26             : #ifndef MIN
      27             : # define MIN(a,b) ((a) < (b) ? (a) : (b))
      28             : #endif
      29             : 
      30             : 
      31             : #define CALLBACK2(FOR)                                               \
      32             : do {                                                                 \
      33             :   if (settings->on_##FOR) {                                          \
      34             :     if (0 != settings->on_##FOR(parser)) return (p - data);          \
      35             :   }                                                                  \
      36             : } while (0)
      37             : 
      38             : 
      39             : #define MARK(FOR)                                                    \
      40             : do {                                                                 \
      41             :   FOR##_mark = p;                                                    \
      42             : } while (0)
      43             : 
      44             : #define CALLBACK_NOCLEAR(FOR)                                        \
      45             : do {                                                                 \
      46             :   if (FOR##_mark) {                                                  \
      47             :     if (settings->on_##FOR) {                                        \
      48             :       if (0 != settings->on_##FOR(parser,                            \
      49             :                                  FOR##_mark,                         \
      50             :                                  p - FOR##_mark))                    \
      51             :       {                                                              \
      52             :         return (p - data);                                           \
      53             :       }                                                              \
      54             :     }                                                                \
      55             :   }                                                                  \
      56             : } while (0)
      57             : 
      58             : #ifdef PHP_WIN32
      59             : # undef CALLBACK
      60             : #endif
      61             : #define CALLBACK(FOR)                                                \
      62             : do {                                                                 \
      63             :   CALLBACK_NOCLEAR(FOR);                                             \
      64             :   FOR##_mark = NULL;                                                 \
      65             : } while (0)
      66             : 
      67             : 
      68             : #define PROXY_CONNECTION "proxy-connection"
      69             : #define CONNECTION "connection"
      70             : #define CONTENT_LENGTH "content-length"
      71             : #define TRANSFER_ENCODING "transfer-encoding"
      72             : #define UPGRADE "upgrade"
      73             : #define CHUNKED "chunked"
      74             : #define KEEP_ALIVE "keep-alive"
      75             : #define CLOSE "close"
      76             : 
      77             : 
      78             : static const char *method_strings[] =
      79             :   { "DELETE"
      80             :   , "GET"
      81             :   , "HEAD"
      82             :   , "POST"
      83             :   , "PUT"
      84             :   , "PATCH"
      85             :   , "CONNECT"
      86             :   , "OPTIONS"
      87             :   , "TRACE"
      88             :   , "COPY"
      89             :   , "LOCK"
      90             :   , "MKCOL"
      91             :   , "MOVE"
      92             :   , "PROPFIND"
      93             :   , "PROPPATCH"
      94             :   , "SEARCH"
      95             :   , "UNLOCK"
      96             :   , "REPORT"
      97             :   , "MKACTIVITY"
      98             :   , "CHECKOUT"
      99             :   , "MERGE"
     100             :   , "M-SEARCH"
     101             :   , "NOTIFY"
     102             :   , "SUBSCRIBE"
     103             :   , "UNSUBSCRIBE"
     104             :   , "NOTIMPLEMENTED"
     105             :   };
     106             : 
     107             : 
     108             : /* Tokens as defined by rfc 2616. Also lowercases them.
     109             :  *        token       = 1*<any CHAR except CTLs or separators>
     110             :  *     separators     = "(" | ")" | "<" | ">" | "@"
     111             :  *                    | "," | ";" | ":" | "\" | <">
     112             :  *                    | "/" | "[" | "]" | "?" | "="
     113             :  *                    | "{" | "}" | SP | HT
     114             :  */
     115             : static const char tokens[256] = {
     116             : /*   0 nul    1 soh    2 stx    3 etx    4 eot    5 enq    6 ack    7 bel  */
     117             :         0,       0,       0,       0,       0,       0,       0,       0,
     118             : /*   8 bs     9 ht    10 nl    11 vt    12 np    13 cr    14 so    15 si   */
     119             :         0,       0,       0,       0,       0,       0,       0,       0,
     120             : /*  16 dle   17 dc1   18 dc2   19 dc3   20 dc4   21 nak   22 syn   23 etb */
     121             :         0,       0,       0,       0,       0,       0,       0,       0,
     122             : /*  24 can   25 em    26 sub   27 esc   28 fs    29 gs    30 rs    31 us  */
     123             :         0,       0,       0,       0,       0,       0,       0,       0,
     124             : /*  32 sp    33  !    34  "    35  #    36  $    37  %    38  &    39  '  */
     125             :        ' ',      '!',     '"',     '#',     '$',     '%',     '&',    '\'',
     126             : /*  40  (    41  )    42  *    43  +    44  ,    45  -    46  .    47  /  */
     127             :         0,       0,      '*',     '+',      0,      '-',     '.',     '/',
     128             : /*  48  0    49  1    50  2    51  3    52  4    53  5    54  6    55  7  */
     129             :        '0',     '1',     '2',     '3',     '4',     '5',     '6',     '7',
     130             : /*  56  8    57  9    58  :    59  ;    60  <    61  =    62  >    63  ?  */
     131             :        '8',     '9',      0,       0,       0,       0,       0,       0,
     132             : /*  64  @    65  A    66  B    67  C    68  D    69  E    70  F    71  G  */
     133             :         0,      'a',     'b',     'c',     'd',     'e',     'f',     'g',
     134             : /*  72  H    73  I    74  J    75  K    76  L    77  M    78  N    79  O  */
     135             :        'h',     'i',     'j',     'k',     'l',     'm',     'n',     'o',
     136             : /*  80  P    81  Q    82  R    83  S    84  T    85  U    86  V    87  W  */
     137             :        'p',     'q',     'r',     's',     't',     'u',     'v',     'w',
     138             : /*  88  X    89  Y    90  Z    91  [    92  \    93  ]    94  ^    95  _  */
     139             :        'x',     'y',     'z',      0,       0,       0,      '^',     '_',
     140             : /*  96  `    97  a    98  b    99  c   100  d   101  e   102  f   103  g  */
     141             :        '`',     'a',     'b',     'c',     'd',     'e',     'f',     'g',
     142             : /* 104  h   105  i   106  j   107  k   108  l   109  m   110  n   111  o  */
     143             :        'h',     'i',     'j',     'k',     'l',     'm',     'n',     'o',
     144             : /* 112  p   113  q   114  r   115  s   116  t   117  u   118  v   119  w  */
     145             :        'p',     'q',     'r',     's',     't',     'u',     'v',     'w',
     146             : /* 120  x   121  y   122  z   123  {   124  |   125  }   126  ~   127 del */
     147             :        'x',     'y',     'z',      0,      '|',     '}',     '~',       0 };
     148             : 
     149             : 
     150             : static const int8_t unhex[256] =
     151             :   {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
     152             :   ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
     153             :   ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
     154             :   , 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1
     155             :   ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1
     156             :   ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
     157             :   ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1
     158             :   ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
     159             :   };
     160             : 
     161             : 
     162             : static const uint8_t normal_url_char[256] = {
     163             : /*   0 nul    1 soh    2 stx    3 etx    4 eot    5 enq    6 ack    7 bel  */
     164             :         0,       0,       0,       0,       0,       0,       0,       0,
     165             : /*   8 bs     9 ht    10 nl    11 vt    12 np    13 cr    14 so    15 si   */
     166             :         0,       0,       0,       0,       0,       0,       0,       0,
     167             : /*  16 dle   17 dc1   18 dc2   19 dc3   20 dc4   21 nak   22 syn   23 etb */
     168             :         0,       0,       0,       0,       0,       0,       0,       0,
     169             : /*  24 can   25 em    26 sub   27 esc   28 fs    29 gs    30 rs    31 us  */
     170             :         0,       0,       0,       0,       0,       0,       0,       0,
     171             : /*  32 sp    33  !    34  "    35  #    36  $    37  %    38  &    39  '  */
     172             :         0,       1,       1,       0,       1,       1,       1,       1,
     173             : /*  40  (    41  )    42  *    43  +    44  ,    45  -    46  .    47  /  */
     174             :         1,       1,       1,       1,       1,       1,       1,       1,
     175             : /*  48  0    49  1    50  2    51  3    52  4    53  5    54  6    55  7  */
     176             :         1,       1,       1,       1,       1,       1,       1,       1,
     177             : /*  56  8    57  9    58  :    59  ;    60  <    61  =    62  >    63  ?  */
     178             :         1,       1,       1,       1,       1,       1,       1,       0,
     179             : /*  64  @    65  A    66  B    67  C    68  D    69  E    70  F    71  G  */
     180             :         1,       1,       1,       1,       1,       1,       1,       1,
     181             : /*  72  H    73  I    74  J    75  K    76  L    77  M    78  N    79  O  */
     182             :         1,       1,       1,       1,       1,       1,       1,       1,
     183             : /*  80  P    81  Q    82  R    83  S    84  T    85  U    86  V    87  W  */
     184             :         1,       1,       1,       1,       1,       1,       1,       1,
     185             : /*  88  X    89  Y    90  Z    91  [    92  \    93  ]    94  ^    95  _  */
     186             :         1,       1,       1,       1,       1,       1,       1,       1,
     187             : /*  96  `    97  a    98  b    99  c   100  d   101  e   102  f   103  g  */
     188             :         1,       1,       1,       1,       1,       1,       1,       1,
     189             : /* 104  h   105  i   106  j   107  k   108  l   109  m   110  n   111  o  */
     190             :         1,       1,       1,       1,       1,       1,       1,       1,
     191             : /* 112  p   113  q   114  r   115  s   116  t   117  u   118  v   119  w  */
     192             :         1,       1,       1,       1,       1,       1,       1,       1,
     193             : /* 120  x   121  y   122  z   123  {   124  |   125  }   126  ~   127 del */
     194             :         1,       1,       1,       1,       1,       1,       1,       0 };
     195             : 
     196             : 
     197             : enum state
     198             :   { s_dead = 1 /* important that this is > 0 */
     199             : 
     200             :   , s_start_req_or_res
     201             :   , s_res_or_resp_H
     202             :   , s_start_res
     203             :   , s_res_H
     204             :   , s_res_HT
     205             :   , s_res_HTT
     206             :   , s_res_HTTP
     207             :   , s_res_first_http_major
     208             :   , s_res_http_major
     209             :   , s_res_first_http_minor
     210             :   , s_res_http_minor
     211             :   , s_res_first_status_code
     212             :   , s_res_status_code
     213             :   , s_res_status
     214             :   , s_res_line_almost_done
     215             : 
     216             :   , s_start_req
     217             : 
     218             :   , s_req_method
     219             :   , s_req_spaces_before_url
     220             :   , s_req_schema
     221             :   , s_req_schema_slash
     222             :   , s_req_schema_slash_slash
     223             :   , s_req_host
     224             :   , s_req_port
     225             :   , s_req_path
     226             :   , s_req_query_string_start
     227             :   , s_req_query_string
     228             :   , s_req_fragment_start
     229             :   , s_req_fragment
     230             :   , s_req_http_start
     231             :   , s_req_http_H
     232             :   , s_req_http_HT
     233             :   , s_req_http_HTT
     234             :   , s_req_http_HTTP
     235             :   , s_req_first_http_major
     236             :   , s_req_http_major
     237             :   , s_req_first_http_minor
     238             :   , s_req_http_minor
     239             :   , s_req_line_almost_done
     240             : 
     241             :   , s_header_field_start
     242             :   , s_header_field
     243             :   , s_header_value_start
     244             :   , s_header_value
     245             : 
     246             :   , s_header_almost_done
     247             : 
     248             :   , s_headers_almost_done
     249             :   /* Important: 's_headers_almost_done' must be the last 'header' state. All
     250             :    * states beyond this must be 'body' states. It is used for overflow
     251             :    * checking. See the PARSING_HEADER() macro.
     252             :    */
     253             :   , s_chunk_size_start
     254             :   , s_chunk_size
     255             :   , s_chunk_size_almost_done
     256             :   , s_chunk_parameters
     257             :   , s_chunk_data
     258             :   , s_chunk_data_almost_done
     259             :   , s_chunk_data_done
     260             : 
     261             :   , s_body_identity
     262             :   , s_body_identity_eof
     263             :   };
     264             : 
     265             : 
     266             : #define PARSING_HEADER(state) (state <= s_headers_almost_done && 0 == (parser->flags & F_TRAILING))
     267             : 
     268             : 
     269             : enum header_states
     270             :   { h_general = 0
     271             :   , h_C
     272             :   , h_CO
     273             :   , h_CON
     274             : 
     275             :   , h_matching_connection
     276             :   , h_matching_proxy_connection
     277             :   , h_matching_content_length
     278             :   , h_matching_transfer_encoding
     279             :   , h_matching_upgrade
     280             : 
     281             :   , h_connection
     282             :   , h_content_length
     283             :   , h_transfer_encoding
     284             :   , h_upgrade
     285             : 
     286             :   , h_matching_transfer_encoding_chunked
     287             :   , h_matching_connection_keep_alive
     288             :   , h_matching_connection_close
     289             : 
     290             :   , h_transfer_encoding_chunked
     291             :   , h_connection_keep_alive
     292             :   , h_connection_close
     293             :   };
     294             : 
     295             : 
     296             : enum flags
     297             :   { F_CHUNKED               = 1 << 0
     298             :   , F_CONNECTION_KEEP_ALIVE = 1 << 1
     299             :   , F_CONNECTION_CLOSE      = 1 << 2
     300             :   , F_TRAILING              = 1 << 3
     301             :   , F_UPGRADE               = 1 << 4
     302             :   , F_SKIPBODY              = 1 << 5
     303             :   };
     304             : 
     305             : 
     306             : #define CR '\r'
     307             : #define LF '\n'
     308             : #define LOWER(c) (unsigned char)(c | 0x20)
     309             : #define TOKEN(c) tokens[(unsigned char)c]
     310             : 
     311             : 
     312             : #define start_state (parser->type == PHP_HTTP_REQUEST ? s_start_req : s_start_res)
     313             : 
     314             : 
     315             : #if HTTP_PARSER_STRICT
     316             : # define STRICT_CHECK(cond) if (cond) goto error
     317             : # define NEW_MESSAGE() (http_should_keep_alive(parser) ? start_state : s_dead)
     318             : #else
     319             : # define STRICT_CHECK(cond)
     320             : # define NEW_MESSAGE() start_state
     321             : #endif
     322             : 
     323             : 
     324           0 : size_t php_http_parser_execute (php_http_parser *parser,
     325             :                             const php_http_parser_settings *settings,
     326             :                             const char *data,
     327             :                             size_t len)
     328             : {
     329             :   char c, ch;
     330           0 :   const char *p = data, *pe;
     331             :   size_t to_read;
     332             : 
     333           0 :   enum state state = (enum state) parser->state;
     334           0 :   enum header_states header_state = (enum header_states) parser->header_state;
     335           0 :   uint32_t index = parser->index;
     336           0 :   uint32_t nread = parser->nread;
     337             : 
     338             :   /* technically we could combine all of these (except for url_mark) into one
     339             :      variable, saving stack space, but it seems more clear to have them
     340             :      separated. */
     341           0 :   const char *header_field_mark = 0;
     342           0 :   const char *header_value_mark = 0;
     343           0 :   const char *fragment_mark = 0;
     344           0 :   const char *query_string_mark = 0;
     345           0 :   const char *path_mark = 0;
     346           0 :   const char *url_mark = 0;
     347             : 
     348           0 :   if (len == 0) {
     349           0 :     if (state == s_body_identity_eof) {
     350           0 :       CALLBACK2(message_complete);
     351             :     }
     352           0 :     return 0;
     353             :   }
     354             : 
     355           0 :   if (state == s_header_field)
     356           0 :     header_field_mark = data;
     357           0 :   if (state == s_header_value)
     358           0 :     header_value_mark = data;
     359           0 :   if (state == s_req_fragment)
     360           0 :     fragment_mark = data;
     361           0 :   if (state == s_req_query_string)
     362           0 :     query_string_mark = data;
     363           0 :   if (state == s_req_path)
     364           0 :     path_mark = data;
     365           0 :   if (state == s_req_path || state == s_req_schema || state == s_req_schema_slash
     366             :       || state == s_req_schema_slash_slash || state == s_req_port
     367             :       || state == s_req_query_string_start || state == s_req_query_string
     368             :       || state == s_req_host
     369             :       || state == s_req_fragment_start || state == s_req_fragment)
     370           0 :     url_mark = data;
     371             : 
     372           0 :   for (p=data, pe=data+len; p != pe; p++) {
     373           0 :     ch = *p;
     374             : 
     375           0 :     if (PARSING_HEADER(state)) {
     376           0 :       ++nread;
     377             :       /* Buffer overflow attack */
     378           0 :       if (nread > PHP_HTTP_MAX_HEADER_SIZE) goto error;
     379             :     }
     380             : 
     381           0 :     switch (state) {
     382             : 
     383             :       case s_dead:
     384             :         /* this state is used after a 'Connection: close' message
     385             :          * the parser will error out if it reads another message
     386             :          */
     387           0 :         goto error;
     388             : 
     389             :       case s_start_req_or_res:
     390             :       {
     391           0 :         if (ch == CR || ch == LF)
     392             :           break;
     393           0 :         parser->flags = 0;
     394           0 :         parser->content_length = -1;
     395             : 
     396           0 :         CALLBACK2(message_begin);
     397             : 
     398           0 :         if (ch == 'H')
     399           0 :           state = s_res_or_resp_H;
     400             :         else {
     401           0 :           parser->type = PHP_HTTP_REQUEST;
     402           0 :           goto start_req_method_assign;
     403             :         }
     404           0 :         break;
     405             :       }
     406             : 
     407             :       case s_res_or_resp_H:
     408           0 :         if (ch == 'T') {
     409           0 :           parser->type = PHP_HTTP_RESPONSE;
     410           0 :           state = s_res_HT;
     411             :         } else {
     412           0 :           if (ch != 'E') goto error;
     413           0 :           parser->type = PHP_HTTP_REQUEST;
     414           0 :           parser->method = PHP_HTTP_HEAD;
     415           0 :           index = 2;
     416           0 :           state = s_req_method;
     417             :         }
     418           0 :         break;
     419             : 
     420             :       case s_start_res:
     421             :       {
     422           0 :         parser->flags = 0;
     423           0 :         parser->content_length = -1;
     424             : 
     425           0 :         CALLBACK2(message_begin);
     426             : 
     427           0 :         switch (ch) {
     428             :           case 'H':
     429           0 :             state = s_res_H;
     430           0 :             break;
     431             : 
     432             :           case CR:
     433             :           case LF:
     434           0 :             break;
     435             : 
     436             :           default:
     437           0 :             goto error;
     438             :         }
     439           0 :         break;
     440             :       }
     441             : 
     442             :       case s_res_H:
     443             :         STRICT_CHECK(ch != 'T');
     444           0 :         state = s_res_HT;
     445           0 :         break;
     446             : 
     447             :       case s_res_HT:
     448             :         STRICT_CHECK(ch != 'T');
     449           0 :         state = s_res_HTT;
     450           0 :         break;
     451             : 
     452             :       case s_res_HTT:
     453             :         STRICT_CHECK(ch != 'P');
     454           0 :         state = s_res_HTTP;
     455           0 :         break;
     456             : 
     457             :       case s_res_HTTP:
     458             :         STRICT_CHECK(ch != '/');
     459           0 :         state = s_res_first_http_major;
     460           0 :         break;
     461             : 
     462             :       case s_res_first_http_major:
     463           0 :         if (ch < '1' || ch > '9') goto error;
     464           0 :         parser->http_major = ch - '0';
     465           0 :         state = s_res_http_major;
     466           0 :         break;
     467             : 
     468             :       /* major HTTP version or dot */
     469             :       case s_res_http_major:
     470             :       {
     471           0 :         if (ch == '.') {
     472           0 :           state = s_res_first_http_minor;
     473           0 :           break;
     474             :         }
     475             : 
     476           0 :         if (ch < '0' || ch > '9') goto error;
     477             : 
     478           0 :         parser->http_major *= 10;
     479           0 :         parser->http_major += ch - '0';
     480             : 
     481           0 :         if (parser->http_major > 999) goto error;
     482           0 :         break;
     483             :       }
     484             : 
     485             :       /* first digit of minor HTTP version */
     486             :       case s_res_first_http_minor:
     487           0 :         if (ch < '0' || ch > '9') goto error;
     488           0 :         parser->http_minor = ch - '0';
     489           0 :         state = s_res_http_minor;
     490           0 :         break;
     491             : 
     492             :       /* minor HTTP version or end of request line */
     493             :       case s_res_http_minor:
     494             :       {
     495           0 :         if (ch == ' ') {
     496           0 :           state = s_res_first_status_code;
     497           0 :           break;
     498             :         }
     499             : 
     500           0 :         if (ch < '0' || ch > '9') goto error;
     501             : 
     502           0 :         parser->http_minor *= 10;
     503           0 :         parser->http_minor += ch - '0';
     504             : 
     505           0 :         if (parser->http_minor > 999) goto error;
     506           0 :         break;
     507             :       }
     508             : 
     509             :       case s_res_first_status_code:
     510             :       {
     511           0 :         if (ch < '0' || ch > '9') {
     512           0 :           if (ch == ' ') {
     513           0 :             break;
     514             :           }
     515           0 :           goto error;
     516             :         }
     517           0 :         parser->status_code = ch - '0';
     518           0 :         state = s_res_status_code;
     519           0 :         break;
     520             :       }
     521             : 
     522             :       case s_res_status_code:
     523             :       {
     524           0 :         if (ch < '0' || ch > '9') {
     525           0 :           switch (ch) {
     526             :             case ' ':
     527           0 :               state = s_res_status;
     528           0 :               break;
     529             :             case CR:
     530           0 :               state = s_res_line_almost_done;
     531           0 :               break;
     532             :             case LF:
     533           0 :               state = s_header_field_start;
     534           0 :               break;
     535             :             default:
     536           0 :               goto error;
     537             :           }
     538           0 :           break;
     539             :         }
     540             : 
     541           0 :         parser->status_code *= 10;
     542           0 :         parser->status_code += ch - '0';
     543             : 
     544           0 :         if (parser->status_code > 999) goto error;
     545           0 :         break;
     546             :       }
     547             : 
     548             :       case s_res_status:
     549             :         /* the human readable status. e.g. "NOT FOUND"
     550             :          * we are not humans so just ignore this */
     551           0 :         if (ch == CR) {
     552           0 :           state = s_res_line_almost_done;
     553           0 :           break;
     554             :         }
     555             : 
     556           0 :         if (ch == LF) {
     557           0 :           state = s_header_field_start;
     558           0 :           break;
     559             :         }
     560           0 :         break;
     561             : 
     562             :       case s_res_line_almost_done:
     563             :         STRICT_CHECK(ch != LF);
     564           0 :         state = s_header_field_start;
     565           0 :         break;
     566             : 
     567             :       case s_start_req:
     568             :       {
     569           0 :         if (ch == CR || ch == LF)
     570             :           break;
     571           0 :         parser->flags = 0;
     572           0 :         parser->content_length = -1;
     573             : 
     574           0 :         CALLBACK2(message_begin);
     575             : 
     576           0 :         if (ch < 'A' || 'Z' < ch) goto error;
     577             : 
     578             :       start_req_method_assign:
     579           0 :         parser->method = (enum php_http_method) 0;
     580           0 :         index = 1;
     581           0 :         switch (ch) {
     582           0 :           case 'C': parser->method = PHP_HTTP_CONNECT; /* or COPY, CHECKOUT */ break;
     583           0 :           case 'D': parser->method = PHP_HTTP_DELETE; break;
     584           0 :           case 'G': parser->method = PHP_HTTP_GET; break;
     585           0 :           case 'H': parser->method = PHP_HTTP_HEAD; break;
     586           0 :           case 'L': parser->method = PHP_HTTP_LOCK; break;
     587           0 :           case 'M': parser->method = PHP_HTTP_MKCOL; /* or MOVE, MKACTIVITY, MERGE, M-SEARCH */ break;
     588           0 :           case 'N': parser->method = PHP_HTTP_NOTIFY; break;
     589           0 :           case 'O': parser->method = PHP_HTTP_OPTIONS; break;
     590           0 :           case 'P': parser->method = PHP_HTTP_POST; /* or PROPFIND or PROPPATCH or PUT */ break;
     591           0 :           case 'R': parser->method = PHP_HTTP_REPORT; break;
     592           0 :           case 'S': parser->method = PHP_HTTP_SUBSCRIBE; /* or SEARCH */ break;
     593           0 :           case 'T': parser->method = PHP_HTTP_TRACE; break;
     594           0 :           case 'U': parser->method = PHP_HTTP_UNLOCK; /* or UNSUBSCRIBE */ break;
     595           0 :           default: parser->method = PHP_HTTP_NOT_IMPLEMENTED; break;
     596             :         }
     597           0 :         state = s_req_method;
     598           0 :         break;
     599             :       }
     600             :       case s_req_method:
     601             :       {
     602             :         const char *matcher;
     603           0 :         if (ch == '\0')
     604           0 :           goto error;
     605             : 
     606           0 :         matcher = method_strings[parser->method];
     607           0 :         if (ch == ' ' && (matcher[index] == '\0' || parser->method == PHP_HTTP_NOT_IMPLEMENTED)) {
     608           0 :           state = s_req_spaces_before_url;
     609           0 :         } else if (ch == matcher[index]) {
     610             :           ; /* nada */
     611           0 :         } else if (parser->method == PHP_HTTP_CONNECT) {
     612           0 :           if (index == 1 && ch == 'H') {
     613           0 :             parser->method = PHP_HTTP_CHECKOUT;
     614           0 :           } else if (index == 2  && ch == 'P') {
     615           0 :             parser->method = PHP_HTTP_COPY;
     616             :           }
     617           0 :         } else if (parser->method == PHP_HTTP_MKCOL) {
     618           0 :           if (index == 1 && ch == 'O') {
     619           0 :             parser->method = PHP_HTTP_MOVE;
     620           0 :           } else if (index == 1 && ch == 'E') {
     621           0 :             parser->method = PHP_HTTP_MERGE;
     622           0 :           } else if (index == 1 && ch == '-') {
     623           0 :             parser->method = PHP_HTTP_MSEARCH;
     624           0 :           } else if (index == 2 && ch == 'A') {
     625           0 :             parser->method = PHP_HTTP_MKACTIVITY;
     626             :           }
     627           0 :         } else if (index == 1 && parser->method == PHP_HTTP_POST && ch == 'R') {
     628           0 :           parser->method = PHP_HTTP_PROPFIND; /* or HTTP_PROPPATCH */
     629           0 :         } else if (index == 1 && parser->method == PHP_HTTP_POST && ch == 'U') {
     630           0 :           parser->method = PHP_HTTP_PUT;
     631           0 :         } else if (index == 1 && parser->method == PHP_HTTP_POST && ch == 'A') {
     632           0 :           parser->method = PHP_HTTP_PATCH;
     633           0 :         } else if (index == 1 && parser->method == PHP_HTTP_SUBSCRIBE && ch == 'E') {
     634           0 :           parser->method = PHP_HTTP_SEARCH;
     635           0 :         } else if (index == 2 && parser->method == PHP_HTTP_UNLOCK && ch == 'S') {
     636           0 :           parser->method = PHP_HTTP_UNSUBSCRIBE;
     637           0 :         } else if (index == 4 && parser->method == PHP_HTTP_PROPFIND && ch == 'P') {
     638           0 :           parser->method = PHP_HTTP_PROPPATCH;
     639             :         } else {
     640           0 :           parser->method = PHP_HTTP_NOT_IMPLEMENTED;
     641             :         }
     642             : 
     643           0 :         ++index;
     644           0 :         break;
     645             :       }
     646             :       case s_req_spaces_before_url:
     647             :       {
     648           0 :         if (ch == ' ') break;
     649             : 
     650           0 :         if (ch == '/' || ch == '*') {
     651           0 :           MARK(url);
     652           0 :           MARK(path);
     653           0 :           state = s_req_path;
     654           0 :           break;
     655             :         }
     656             : 
     657           0 :         c = LOWER(ch);
     658             : 
     659           0 :         if (c >= 'a' && c <= 'z') {
     660           0 :           MARK(url);
     661           0 :           state = s_req_schema;
     662           0 :           break;
     663             :         }
     664             : 
     665           0 :         goto error;
     666             :       }
     667             : 
     668             :       case s_req_schema:
     669             :       {
     670           0 :         c = LOWER(ch);
     671             : 
     672           0 :         if (c >= 'a' && c <= 'z') break;
     673             : 
     674           0 :         if (ch == ':') {
     675           0 :           state = s_req_schema_slash;
     676           0 :           break;
     677           0 :         } else if (ch == '.') {
     678           0 :           state = s_req_host;
     679           0 :           break;
     680           0 :         } else if ('0' <= ch && ch <= '9') {
     681           0 :           state = s_req_host;
     682           0 :           break;
     683             :         }
     684             : 
     685           0 :         goto error;
     686             :       }
     687             : 
     688             :       case s_req_schema_slash:
     689             :         STRICT_CHECK(ch != '/');
     690           0 :         state = s_req_schema_slash_slash;
     691           0 :         break;
     692             : 
     693             :       case s_req_schema_slash_slash:
     694             :         STRICT_CHECK(ch != '/');
     695           0 :         state = s_req_host;
     696           0 :         break;
     697             : 
     698             :       case s_req_host:
     699             :       {
     700           0 :         c = LOWER(ch);
     701           0 :         if (c >= 'a' && c <= 'z') break;
     702           0 :         if ((ch >= '0' && ch <= '9') || ch == '.' || ch == '-') break;
     703           0 :         switch (ch) {
     704             :           case ':':
     705           0 :             state = s_req_port;
     706           0 :             break;
     707             :           case '/':
     708           0 :             MARK(path);
     709           0 :             state = s_req_path;
     710           0 :             break;
     711             :           case ' ':
     712             :             /* The request line looks like:
     713             :              *   "GET http://foo.bar.com HTTP/1.1"
     714             :              * That is, there is no path.
     715             :              */
     716           0 :             CALLBACK(url);
     717           0 :             state = s_req_http_start;
     718           0 :             break;
     719             :           default:
     720           0 :             goto error;
     721             :         }
     722           0 :         break;
     723             :       }
     724             : 
     725             :       case s_req_port:
     726             :       {
     727           0 :         if (ch >= '0' && ch <= '9') break;
     728           0 :         switch (ch) {
     729             :           case '/':
     730           0 :             MARK(path);
     731           0 :             state = s_req_path;
     732           0 :             break;
     733             :           case ' ':
     734             :             /* The request line looks like:
     735             :              *   "GET http://foo.bar.com:1234 HTTP/1.1"
     736             :              * That is, there is no path.
     737             :              */
     738           0 :             CALLBACK(url);
     739           0 :             state = s_req_http_start;
     740           0 :             break;
     741             :           default:
     742           0 :             goto error;
     743             :         }
     744           0 :         break;
     745             :       }
     746             : 
     747             :       case s_req_path:
     748             :       {
     749           0 :         if (normal_url_char[(unsigned char)ch]) break;
     750             : 
     751           0 :         switch (ch) {
     752             :           case ' ':
     753           0 :             CALLBACK(url);
     754           0 :             CALLBACK(path);
     755           0 :             state = s_req_http_start;
     756           0 :             break;
     757             :           case CR:
     758           0 :             CALLBACK(url);
     759           0 :             CALLBACK(path);
     760           0 :             parser->http_major = 0;
     761           0 :             parser->http_minor = 9;
     762           0 :             state = s_req_line_almost_done;
     763           0 :             break;
     764             :           case LF:
     765           0 :             CALLBACK(url);
     766           0 :             CALLBACK(path);
     767           0 :             parser->http_major = 0;
     768           0 :             parser->http_minor = 9;
     769           0 :             state = s_header_field_start;
     770           0 :             break;
     771             :           case '?':
     772           0 :             CALLBACK(path);
     773           0 :             state = s_req_query_string_start;
     774           0 :             break;
     775             :           case '#':
     776           0 :             CALLBACK(path);
     777           0 :             state = s_req_fragment_start;
     778           0 :             break;
     779             :           default:
     780           0 :             goto error;
     781             :         }
     782           0 :         break;
     783             :       }
     784             : 
     785             :       case s_req_query_string_start:
     786             :       {
     787           0 :         if (normal_url_char[(unsigned char)ch]) {
     788           0 :           MARK(query_string);
     789           0 :           state = s_req_query_string;
     790           0 :           break;
     791             :         }
     792             : 
     793           0 :         switch (ch) {
     794             :           case '?':
     795           0 :             break; /* XXX ignore extra '?' ... is this right? */
     796             :           case ' ':
     797           0 :             CALLBACK(url);
     798           0 :             state = s_req_http_start;
     799           0 :             break;
     800             :           case CR:
     801           0 :             CALLBACK(url);
     802           0 :             parser->http_major = 0;
     803           0 :             parser->http_minor = 9;
     804           0 :             state = s_req_line_almost_done;
     805           0 :             break;
     806             :           case LF:
     807           0 :             CALLBACK(url);
     808           0 :             parser->http_major = 0;
     809           0 :             parser->http_minor = 9;
     810           0 :             state = s_header_field_start;
     811           0 :             break;
     812             :           case '#':
     813           0 :             state = s_req_fragment_start;
     814           0 :             break;
     815             :           default:
     816           0 :             goto error;
     817             :         }
     818           0 :         break;
     819             :       }
     820             : 
     821             :       case s_req_query_string:
     822             :       {
     823           0 :         if (normal_url_char[(unsigned char)ch]) break;
     824             : 
     825           0 :         switch (ch) {
     826             :           case '?':
     827             :             /* allow extra '?' in query string */
     828           0 :             break;
     829             :           case ' ':
     830           0 :             CALLBACK(url);
     831           0 :             CALLBACK(query_string);
     832           0 :             state = s_req_http_start;
     833           0 :             break;
     834             :           case CR:
     835           0 :             CALLBACK(url);
     836           0 :             CALLBACK(query_string);
     837           0 :             parser->http_major = 0;
     838           0 :             parser->http_minor = 9;
     839           0 :             state = s_req_line_almost_done;
     840           0 :             break;
     841             :           case LF:
     842           0 :             CALLBACK(url);
     843           0 :             CALLBACK(query_string);
     844           0 :             parser->http_major = 0;
     845           0 :             parser->http_minor = 9;
     846           0 :             state = s_header_field_start;
     847           0 :             break;
     848             :           case '#':
     849           0 :             CALLBACK(query_string);
     850           0 :             state = s_req_fragment_start;
     851           0 :             break;
     852             :           default:
     853           0 :             goto error;
     854             :         }
     855           0 :         break;
     856             :       }
     857             : 
     858             :       case s_req_fragment_start:
     859             :       {
     860           0 :         if (normal_url_char[(unsigned char)ch]) {
     861           0 :           MARK(fragment);
     862           0 :           state = s_req_fragment;
     863           0 :           break;
     864             :         }
     865             : 
     866           0 :         switch (ch) {
     867             :           case ' ':
     868           0 :             CALLBACK(url);
     869           0 :             state = s_req_http_start;
     870           0 :             break;
     871             :           case CR:
     872           0 :             CALLBACK(url);
     873           0 :             parser->http_major = 0;
     874           0 :             parser->http_minor = 9;
     875           0 :             state = s_req_line_almost_done;
     876           0 :             break;
     877             :           case LF:
     878           0 :             CALLBACK(url);
     879           0 :             parser->http_major = 0;
     880           0 :             parser->http_minor = 9;
     881           0 :             state = s_header_field_start;
     882           0 :             break;
     883             :           case '?':
     884           0 :             MARK(fragment);
     885           0 :             state = s_req_fragment;
     886           0 :             break;
     887             :           case '#':
     888           0 :             break;
     889             :           default:
     890           0 :             goto error;
     891             :         }
     892           0 :         break;
     893             :       }
     894             : 
     895             :       case s_req_fragment:
     896             :       {
     897           0 :         if (normal_url_char[(unsigned char)ch]) break;
     898             : 
     899           0 :         switch (ch) {
     900             :           case ' ':
     901           0 :             CALLBACK(url);
     902           0 :             CALLBACK(fragment);
     903           0 :             state = s_req_http_start;
     904           0 :             break;
     905             :           case CR:
     906           0 :             CALLBACK(url);
     907           0 :             CALLBACK(fragment);
     908           0 :             parser->http_major = 0;
     909           0 :             parser->http_minor = 9;
     910           0 :             state = s_req_line_almost_done;
     911           0 :             break;
     912             :           case LF:
     913           0 :             CALLBACK(url);
     914           0 :             CALLBACK(fragment);
     915           0 :             parser->http_major = 0;
     916           0 :             parser->http_minor = 9;
     917           0 :             state = s_header_field_start;
     918           0 :             break;
     919             :           case '?':
     920             :           case '#':
     921           0 :             break;
     922             :           default:
     923           0 :             goto error;
     924             :         }
     925           0 :         break;
     926             :       }
     927             : 
     928             :       case s_req_http_start:
     929           0 :         switch (ch) {
     930             :           case 'H':
     931           0 :             state = s_req_http_H;
     932           0 :             break;
     933             :           case ' ':
     934           0 :             break;
     935             :           default:
     936           0 :             goto error;
     937             :         }
     938           0 :         break;
     939             : 
     940             :       case s_req_http_H:
     941             :         STRICT_CHECK(ch != 'T');
     942           0 :         state = s_req_http_HT;
     943           0 :         break;
     944             : 
     945             :       case s_req_http_HT:
     946             :         STRICT_CHECK(ch != 'T');
     947           0 :         state = s_req_http_HTT;
     948           0 :         break;
     949             : 
     950             :       case s_req_http_HTT:
     951             :         STRICT_CHECK(ch != 'P');
     952           0 :         state = s_req_http_HTTP;
     953           0 :         break;
     954             : 
     955             :       case s_req_http_HTTP:
     956             :         STRICT_CHECK(ch != '/');
     957           0 :         state = s_req_first_http_major;
     958           0 :         break;
     959             : 
     960             :       /* first digit of major HTTP version */
     961             :       case s_req_first_http_major:
     962           0 :         if (ch < '1' || ch > '9') goto error;
     963           0 :         parser->http_major = ch - '0';
     964           0 :         state = s_req_http_major;
     965           0 :         break;
     966             : 
     967             :       /* major HTTP version or dot */
     968             :       case s_req_http_major:
     969             :       {
     970           0 :         if (ch == '.') {
     971           0 :           state = s_req_first_http_minor;
     972           0 :           break;
     973             :         }
     974             : 
     975           0 :         if (ch < '0' || ch > '9') goto error;
     976             : 
     977           0 :         parser->http_major *= 10;
     978           0 :         parser->http_major += ch - '0';
     979             : 
     980           0 :         if (parser->http_major > 999) goto error;
     981           0 :         break;
     982             :       }
     983             : 
     984             :       /* first digit of minor HTTP version */
     985             :       case s_req_first_http_minor:
     986           0 :         if (ch < '0' || ch > '9') goto error;
     987           0 :         parser->http_minor = ch - '0';
     988           0 :         state = s_req_http_minor;
     989           0 :         break;
     990             : 
     991             :       /* minor HTTP version or end of request line */
     992             :       case s_req_http_minor:
     993             :       {
     994           0 :         if (ch == CR) {
     995           0 :           state = s_req_line_almost_done;
     996           0 :           break;
     997             :         }
     998             : 
     999           0 :         if (ch == LF) {
    1000           0 :           state = s_header_field_start;
    1001           0 :           break;
    1002             :         }
    1003             : 
    1004             :         /* XXX allow spaces after digit? */
    1005             : 
    1006           0 :         if (ch < '0' || ch > '9') goto error;
    1007             : 
    1008           0 :         parser->http_minor *= 10;
    1009           0 :         parser->http_minor += ch - '0';
    1010             : 
    1011           0 :         if (parser->http_minor > 999) goto error;
    1012           0 :         break;
    1013             :       }
    1014             : 
    1015             :       /* end of request line */
    1016             :       case s_req_line_almost_done:
    1017             :       {
    1018           0 :         if (ch != LF) goto error;
    1019           0 :         state = s_header_field_start;
    1020           0 :         break;
    1021             :       }
    1022             : 
    1023             :       case s_header_field_start:
    1024             :       {
    1025           0 :         if (ch == CR) {
    1026           0 :           state = s_headers_almost_done;
    1027           0 :           break;
    1028             :         }
    1029             : 
    1030           0 :         if (ch == LF) {
    1031             :           /* they might be just sending \n instead of \r\n so this would be
    1032             :            * the second \n to denote the end of headers*/
    1033           0 :           state = s_headers_almost_done;
    1034           0 :           goto headers_almost_done;
    1035             :         }
    1036             : 
    1037           0 :         c = TOKEN(ch);
    1038             : 
    1039           0 :         if (!c) goto error;
    1040             : 
    1041           0 :         MARK(header_field);
    1042             : 
    1043           0 :         index = 0;
    1044           0 :         state = s_header_field;
    1045             : 
    1046           0 :         switch (c) {
    1047             :           case 'c':
    1048           0 :             header_state = h_C;
    1049           0 :             break;
    1050             : 
    1051             :           case 'p':
    1052           0 :             header_state = h_matching_proxy_connection;
    1053           0 :             break;
    1054             : 
    1055             :           case 't':
    1056           0 :             header_state = h_matching_transfer_encoding;
    1057           0 :             break;
    1058             : 
    1059             :           case 'u':
    1060           0 :             header_state = h_matching_upgrade;
    1061           0 :             break;
    1062             : 
    1063             :           default:
    1064           0 :             header_state = h_general;
    1065             :             break;
    1066             :         }
    1067           0 :         break;
    1068             :       }
    1069             : 
    1070             :       case s_header_field:
    1071             :       {
    1072           0 :         c = TOKEN(ch);
    1073             : 
    1074           0 :         if (c) {
    1075           0 :           switch (header_state) {
    1076             :             case h_general:
    1077           0 :               break;
    1078             : 
    1079             :             case h_C:
    1080           0 :               index++;
    1081           0 :               header_state = (c == 'o' ? h_CO : h_general);
    1082           0 :               break;
    1083             : 
    1084             :             case h_CO:
    1085           0 :               index++;
    1086           0 :               header_state = (c == 'n' ? h_CON : h_general);
    1087           0 :               break;
    1088             : 
    1089             :             case h_CON:
    1090           0 :               index++;
    1091           0 :               switch (c) {
    1092             :                 case 'n':
    1093           0 :                   header_state = h_matching_connection;
    1094           0 :                   break;
    1095             :                 case 't':
    1096           0 :                   header_state = h_matching_content_length;
    1097           0 :                   break;
    1098             :                 default:
    1099           0 :                   header_state = h_general;
    1100             :                   break;
    1101             :               }
    1102           0 :               break;
    1103             : 
    1104             :             /* connection */
    1105             : 
    1106             :             case h_matching_connection:
    1107           0 :               index++;
    1108           0 :               if (index > sizeof(CONNECTION)-1
    1109           0 :                   || c != CONNECTION[index]) {
    1110           0 :                 header_state = h_general;
    1111           0 :               } else if (index == sizeof(CONNECTION)-2) {
    1112           0 :                 header_state = h_connection;
    1113             :               }
    1114           0 :               break;
    1115             : 
    1116             :             /* proxy-connection */
    1117             : 
    1118             :             case h_matching_proxy_connection:
    1119           0 :               index++;
    1120           0 :               if (index > sizeof(PROXY_CONNECTION)-1
    1121           0 :                   || c != PROXY_CONNECTION[index]) {
    1122           0 :                 header_state = h_general;
    1123           0 :               } else if (index == sizeof(PROXY_CONNECTION)-2) {
    1124           0 :                 header_state = h_connection;
    1125             :               }
    1126           0 :               break;
    1127             : 
    1128             :             /* content-length */
    1129             : 
    1130             :             case h_matching_content_length:
    1131           0 :               index++;
    1132           0 :               if (index > sizeof(CONTENT_LENGTH)-1
    1133           0 :                   || c != CONTENT_LENGTH[index]) {
    1134           0 :                 header_state = h_general;
    1135           0 :               } else if (index == sizeof(CONTENT_LENGTH)-2) {
    1136           0 :                 header_state = h_content_length;
    1137             :               }
    1138           0 :               break;
    1139             : 
    1140             :             /* transfer-encoding */
    1141             : 
    1142             :             case h_matching_transfer_encoding:
    1143           0 :               index++;
    1144           0 :               if (index > sizeof(TRANSFER_ENCODING)-1
    1145           0 :                   || c != TRANSFER_ENCODING[index]) {
    1146           0 :                 header_state = h_general;
    1147           0 :               } else if (index == sizeof(TRANSFER_ENCODING)-2) {
    1148           0 :                 header_state = h_transfer_encoding;
    1149             :               }
    1150           0 :               break;
    1151             : 
    1152             :             /* upgrade */
    1153             : 
    1154             :             case h_matching_upgrade:
    1155           0 :               index++;
    1156           0 :               if (index > sizeof(UPGRADE)-1
    1157           0 :                   || c != UPGRADE[index]) {
    1158           0 :                 header_state = h_general;
    1159           0 :               } else if (index == sizeof(UPGRADE)-2) {
    1160           0 :                 header_state = h_upgrade;
    1161             :               }
    1162           0 :               break;
    1163             : 
    1164             :             case h_connection:
    1165             :             case h_content_length:
    1166             :             case h_transfer_encoding:
    1167             :             case h_upgrade:
    1168           0 :               if (ch != ' ') header_state = h_general;
    1169           0 :               break;
    1170             : 
    1171             :             default:
    1172           0 :               assert(0 && "Unknown header_state");
    1173             :               break;
    1174             :           }
    1175           0 :           break;
    1176             :         }
    1177             : 
    1178           0 :         if (ch == ':') {
    1179           0 :           CALLBACK(header_field);
    1180           0 :           state = s_header_value_start;
    1181           0 :           break;
    1182             :         }
    1183             : 
    1184           0 :         if (ch == CR) {
    1185           0 :           state = s_header_almost_done;
    1186           0 :           CALLBACK(header_field);
    1187           0 :           break;
    1188             :         }
    1189             : 
    1190           0 :         if (ch == LF) {
    1191           0 :           CALLBACK(header_field);
    1192           0 :           state = s_header_field_start;
    1193           0 :           break;
    1194             :         }
    1195             : 
    1196           0 :         goto error;
    1197             :       }
    1198             : 
    1199             :       case s_header_value_start:
    1200             :       {
    1201           0 :         if (ch == ' ') break;
    1202             : 
    1203           0 :         MARK(header_value);
    1204             : 
    1205           0 :         state = s_header_value;
    1206           0 :         index = 0;
    1207             : 
    1208           0 :         c = LOWER(ch);
    1209             : 
    1210           0 :         if (ch == CR) {
    1211           0 :           CALLBACK(header_value);
    1212           0 :           header_state = h_general;
    1213           0 :           state = s_header_almost_done;
    1214           0 :           break;
    1215             :         }
    1216             : 
    1217           0 :         if (ch == LF) {
    1218           0 :           CALLBACK(header_value);
    1219           0 :           state = s_header_field_start;
    1220           0 :           break;
    1221             :         }
    1222             : 
    1223           0 :         switch (header_state) {
    1224             :           case h_upgrade:
    1225           0 :             parser->flags |= F_UPGRADE;
    1226           0 :             header_state = h_general;
    1227           0 :             break;
    1228             : 
    1229             :           case h_transfer_encoding:
    1230             :             /* looking for 'Transfer-Encoding: chunked' */
    1231           0 :             if ('c' == c) {
    1232           0 :               header_state = h_matching_transfer_encoding_chunked;
    1233             :             } else {
    1234           0 :               header_state = h_general;
    1235             :             }
    1236           0 :             break;
    1237             : 
    1238             :           case h_content_length:
    1239           0 :             if (ch < '0' || ch > '9') goto error;
    1240           0 :             parser->content_length = ch - '0';
    1241           0 :             break;
    1242             : 
    1243             :           case h_connection:
    1244             :             /* looking for 'Connection: keep-alive' */
    1245           0 :             if (c == 'k') {
    1246           0 :               header_state = h_matching_connection_keep_alive;
    1247             :             /* looking for 'Connection: close' */
    1248           0 :             } else if (c == 'c') {
    1249           0 :               header_state = h_matching_connection_close;
    1250             :             } else {
    1251           0 :               header_state = h_general;
    1252             :             }
    1253           0 :             break;
    1254             : 
    1255             :           default:
    1256           0 :             header_state = h_general;
    1257             :             break;
    1258             :         }
    1259           0 :         break;
    1260             :       }
    1261             : 
    1262             :       case s_header_value:
    1263             :       {
    1264           0 :         c = LOWER(ch);
    1265             : 
    1266           0 :         if (ch == CR) {
    1267           0 :           CALLBACK(header_value);
    1268           0 :           state = s_header_almost_done;
    1269           0 :           break;
    1270             :         }
    1271             : 
    1272           0 :         if (ch == LF) {
    1273           0 :           CALLBACK(header_value);
    1274           0 :           goto header_almost_done;
    1275             :         }
    1276             : 
    1277           0 :         switch (header_state) {
    1278             :           case h_general:
    1279           0 :             break;
    1280             : 
    1281             :           case h_connection:
    1282             :           case h_transfer_encoding:
    1283           0 :             assert(0 && "Shouldn't get here.");
    1284             :             break;
    1285             : 
    1286             :           case h_content_length:
    1287           0 :             if (ch == ' ') break;
    1288           0 :             if (ch < '0' || ch > '9') goto error;
    1289           0 :             parser->content_length *= 10;
    1290           0 :             parser->content_length += ch - '0';
    1291           0 :             break;
    1292             : 
    1293             :           /* Transfer-Encoding: chunked */
    1294             :           case h_matching_transfer_encoding_chunked:
    1295           0 :             index++;
    1296           0 :             if (index > sizeof(CHUNKED)-1
    1297           0 :                 || c != CHUNKED[index]) {
    1298           0 :               header_state = h_general;
    1299           0 :             } else if (index == sizeof(CHUNKED)-2) {
    1300           0 :               header_state = h_transfer_encoding_chunked;
    1301             :             }
    1302           0 :             break;
    1303             : 
    1304             :           /* looking for 'Connection: keep-alive' */
    1305             :           case h_matching_connection_keep_alive:
    1306           0 :             index++;
    1307           0 :             if (index > sizeof(KEEP_ALIVE)-1
    1308           0 :                 || c != KEEP_ALIVE[index]) {
    1309           0 :               header_state = h_general;
    1310           0 :             } else if (index == sizeof(KEEP_ALIVE)-2) {
    1311           0 :               header_state = h_connection_keep_alive;
    1312             :             }
    1313           0 :             break;
    1314             : 
    1315             :           /* looking for 'Connection: close' */
    1316             :           case h_matching_connection_close:
    1317           0 :             index++;
    1318           0 :             if (index > sizeof(CLOSE)-1 || c != CLOSE[index]) {
    1319           0 :               header_state = h_general;
    1320           0 :             } else if (index == sizeof(CLOSE)-2) {
    1321           0 :               header_state = h_connection_close;
    1322             :             }
    1323           0 :             break;
    1324             : 
    1325             :           case h_transfer_encoding_chunked:
    1326             :           case h_connection_keep_alive:
    1327             :           case h_connection_close:
    1328           0 :             if (ch != ' ') header_state = h_general;
    1329           0 :             break;
    1330             : 
    1331             :           default:
    1332           0 :             state = s_header_value;
    1333           0 :             header_state = h_general;
    1334             :             break;
    1335             :         }
    1336           0 :         break;
    1337             :       }
    1338             : 
    1339             :       case s_header_almost_done:
    1340             :       header_almost_done:
    1341             :       {
    1342             :         STRICT_CHECK(ch != LF);
    1343             : 
    1344           0 :         state = s_header_field_start;
    1345             : 
    1346           0 :         switch (header_state) {
    1347             :           case h_connection_keep_alive:
    1348           0 :             parser->flags |= F_CONNECTION_KEEP_ALIVE;
    1349           0 :             break;
    1350             :           case h_connection_close:
    1351           0 :             parser->flags |= F_CONNECTION_CLOSE;
    1352           0 :             break;
    1353             :           case h_transfer_encoding_chunked:
    1354           0 :             parser->flags |= F_CHUNKED;
    1355             :             break;
    1356             :           default:
    1357             :             break;
    1358             :         }
    1359           0 :         break;
    1360             :       }
    1361             : 
    1362             :       case s_headers_almost_done:
    1363             :       headers_almost_done:
    1364             :       {
    1365             :         STRICT_CHECK(ch != LF);
    1366             : 
    1367           0 :         if (parser->flags & F_TRAILING) {
    1368             :           /* End of a chunked request */
    1369           0 :           CALLBACK2(message_complete);
    1370           0 :           state = NEW_MESSAGE();
    1371           0 :           break;
    1372             :         }
    1373             : 
    1374           0 :         nread = 0;
    1375             : 
    1376           0 :         if (parser->flags & F_UPGRADE || parser->method == PHP_HTTP_CONNECT) {
    1377           0 :           parser->upgrade = 1;
    1378             :         }
    1379             : 
    1380             :         /* Here we call the headers_complete callback. This is somewhat
    1381             :          * different than other callbacks because if the user returns 1, we
    1382             :          * will interpret that as saying that this message has no body. This
    1383             :          * is needed for the annoying case of receiving a response to a HEAD
    1384             :          * request.
    1385             :          */
    1386           0 :         if (settings->on_headers_complete) {
    1387           0 :           switch (settings->on_headers_complete(parser)) {
    1388             :             case 0:
    1389           0 :               break;
    1390             : 
    1391             :             case 1:
    1392           0 :               parser->flags |= F_SKIPBODY;
    1393           0 :               break;
    1394             : 
    1395             :             default:
    1396           0 :               return p - data; /* Error */
    1397             :           }
    1398             :         }
    1399             : 
    1400             :         /* Exit, the rest of the connect is in a different protocol. */
    1401           0 :         if (parser->upgrade) {
    1402           0 :           CALLBACK2(message_complete);
    1403           0 :           return (p - data);
    1404             :         }
    1405             : 
    1406           0 :         if (parser->flags & F_SKIPBODY) {
    1407           0 :           CALLBACK2(message_complete);
    1408           0 :           state = NEW_MESSAGE();
    1409           0 :         } else if (parser->flags & F_CHUNKED) {
    1410             :           /* chunked encoding - ignore Content-Length header */
    1411           0 :           state = s_chunk_size_start;
    1412             :         } else {
    1413           0 :           if (parser->content_length == 0) {
    1414             :             /* Content-Length header given but zero: Content-Length: 0\r\n */
    1415           0 :             CALLBACK2(message_complete);
    1416           0 :             state = NEW_MESSAGE();
    1417           0 :           } else if (parser->content_length > 0) {
    1418             :             /* Content-Length header given and non-zero */
    1419           0 :             state = s_body_identity;
    1420             :           } else {
    1421           0 :             if (parser->type == PHP_HTTP_REQUEST || php_http_should_keep_alive(parser)) {
    1422             :               /* Assume content-length 0 - read the next */
    1423           0 :               CALLBACK2(message_complete);
    1424           0 :               state = NEW_MESSAGE();
    1425             :             } else {
    1426             :               /* Read body until EOF */
    1427           0 :               state = s_body_identity_eof;
    1428             :             }
    1429             :           }
    1430             :         }
    1431             : 
    1432           0 :         break;
    1433             :       }
    1434             : 
    1435             :       case s_body_identity:
    1436           0 :         assert(pe >= p);
    1437             : 
    1438           0 :         to_read = MIN((size_t)(pe - p), (size_t)parser->content_length);
    1439           0 :         if (to_read > 0) {
    1440           0 :           if (settings->on_body) settings->on_body(parser, p, to_read);
    1441           0 :           p += to_read - 1;
    1442           0 :           parser->content_length -= to_read;
    1443           0 :           if (parser->content_length == 0) {
    1444           0 :             CALLBACK2(message_complete);
    1445           0 :             state = NEW_MESSAGE();
    1446             :           }
    1447             :         }
    1448           0 :         break;
    1449             : 
    1450             :       /* read until EOF */
    1451             :       case s_body_identity_eof:
    1452           0 :         to_read = pe - p;
    1453           0 :         if (to_read > 0) {
    1454           0 :           if (settings->on_body) settings->on_body(parser, p, to_read);
    1455           0 :           p += to_read - 1;
    1456             :         }
    1457           0 :         break;
    1458             : 
    1459             :       case s_chunk_size_start:
    1460             :       {
    1461           0 :         assert(parser->flags & F_CHUNKED);
    1462             : 
    1463           0 :         c = unhex[(unsigned char)ch];
    1464           0 :         if (c == -1) goto error;
    1465           0 :         parser->content_length = c;
    1466           0 :         state = s_chunk_size;
    1467           0 :         break;
    1468             :       }
    1469             : 
    1470             :       case s_chunk_size:
    1471             :       {
    1472           0 :         assert(parser->flags & F_CHUNKED);
    1473             : 
    1474           0 :         if (ch == CR) {
    1475           0 :           state = s_chunk_size_almost_done;
    1476           0 :           break;
    1477             :         }
    1478             : 
    1479           0 :         c = unhex[(unsigned char)ch];
    1480             : 
    1481           0 :         if (c == -1) {
    1482           0 :           if (ch == ';' || ch == ' ') {
    1483           0 :             state = s_chunk_parameters;
    1484           0 :             break;
    1485             :           }
    1486           0 :           goto error;
    1487             :         }
    1488             : 
    1489           0 :         parser->content_length *= 16;
    1490           0 :         parser->content_length += c;
    1491           0 :         break;
    1492             :       }
    1493             : 
    1494             :       case s_chunk_parameters:
    1495             :       {
    1496           0 :         assert(parser->flags & F_CHUNKED);
    1497             :         /* just ignore this shit. TODO check for overflow */
    1498           0 :         if (ch == CR) {
    1499           0 :           state = s_chunk_size_almost_done;
    1500           0 :           break;
    1501             :         }
    1502           0 :         break;
    1503             :       }
    1504             : 
    1505             :       case s_chunk_size_almost_done:
    1506             :       {
    1507           0 :         assert(parser->flags & F_CHUNKED);
    1508             :         STRICT_CHECK(ch != LF);
    1509             : 
    1510           0 :         if (parser->content_length == 0) {
    1511           0 :           parser->flags |= F_TRAILING;
    1512           0 :           state = s_header_field_start;
    1513             :         } else {
    1514           0 :           state = s_chunk_data;
    1515             :         }
    1516           0 :         break;
    1517             :       }
    1518             : 
    1519             :       case s_chunk_data:
    1520             :       {
    1521           0 :         assert(parser->flags & F_CHUNKED);
    1522           0 :         assert(pe >= p);
    1523             : 
    1524           0 :         to_read = MIN((size_t)(pe - p), (size_t)(parser->content_length));
    1525             : 
    1526           0 :         if (to_read > 0) {
    1527           0 :           if (settings->on_body) settings->on_body(parser, p, to_read);
    1528           0 :           p += to_read - 1;
    1529             :         }
    1530             : 
    1531           0 :         if (to_read == parser->content_length) {
    1532           0 :           state = s_chunk_data_almost_done;
    1533             :         }
    1534             : 
    1535           0 :         parser->content_length -= to_read;
    1536           0 :         break;
    1537             :       }
    1538             : 
    1539             :       case s_chunk_data_almost_done:
    1540           0 :         assert(parser->flags & F_CHUNKED);
    1541             :         STRICT_CHECK(ch != CR);
    1542           0 :         state = s_chunk_data_done;
    1543           0 :         break;
    1544             : 
    1545             :       case s_chunk_data_done:
    1546           0 :         assert(parser->flags & F_CHUNKED);
    1547             :         STRICT_CHECK(ch != LF);
    1548           0 :         state = s_chunk_size_start;
    1549           0 :         break;
    1550             : 
    1551             :       default:
    1552           0 :         assert(0 && "unhandled state");
    1553             :         goto error;
    1554             :     }
    1555             :   }
    1556             : 
    1557           0 :   CALLBACK_NOCLEAR(header_field);
    1558           0 :   CALLBACK_NOCLEAR(header_value);
    1559           0 :   CALLBACK_NOCLEAR(fragment);
    1560           0 :   CALLBACK_NOCLEAR(query_string);
    1561           0 :   CALLBACK_NOCLEAR(path);
    1562           0 :   CALLBACK_NOCLEAR(url);
    1563             : 
    1564           0 :   parser->state = state;
    1565           0 :   parser->header_state = header_state;
    1566           0 :   parser->index = index;
    1567           0 :   parser->nread = nread;
    1568             : 
    1569           0 :   return len;
    1570             : 
    1571             : error:
    1572           0 :   parser->state = s_dead;
    1573           0 :   return (p - data);
    1574             : }
    1575             : 
    1576             : 
    1577             : int
    1578           0 : php_http_should_keep_alive (php_http_parser *parser)
    1579             : {
    1580           0 :   if (parser->http_major > 0 && parser->http_minor > 0) {
    1581             :     /* HTTP/1.1 */
    1582           0 :     if (parser->flags & F_CONNECTION_CLOSE) {
    1583           0 :       return 0;
    1584             :     } else {
    1585           0 :       return 1;
    1586             :     }
    1587             :   } else {
    1588             :     /* HTTP/1.0 or earlier */
    1589           0 :     if (parser->flags & F_CONNECTION_KEEP_ALIVE) {
    1590           0 :       return 1;
    1591             :     } else {
    1592           0 :       return 0;
    1593             :     }
    1594             :   }
    1595             : }
    1596             : 
    1597             : 
    1598           0 : const char * php_http_method_str (enum php_http_method m)
    1599             : {
    1600           0 :   return method_strings[m];
    1601             : }
    1602             : 
    1603             : 
    1604             : void
    1605           0 : php_http_parser_init (php_http_parser *parser, enum php_http_parser_type t)
    1606             : {
    1607           0 :   parser->type = t;
    1608           0 :   parser->state = (t == PHP_HTTP_REQUEST ? s_start_req : (t == PHP_HTTP_RESPONSE ? s_start_res : s_start_req_or_res));
    1609           0 :   parser->nread = 0;
    1610           0 :   parser->upgrade = 0;
    1611           0 :   parser->flags = 0;
    1612           0 :   parser->method = 0;
    1613           0 : }

Generated by: LCOV version 1.10

Generated at Fri, 31 Jul 2015 08:59:04 +0000 (14 hours ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.