php_http_parser.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. /* modified by Moriyoshi Koizumi <moriyoshi@php.net> to make it fit to PHP source tree. */
  22. #ifndef php_http_parser_h
  23. #define php_http_parser_h
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. #include <sys/types.h>
  28. #if defined(_WIN32) && !defined(__MINGW32__)
  29. # include <windows.h>
  30. # include "config.w32.h"
  31. #else
  32. # include "php_config.h"
  33. #endif
  34. #include "php_stdint.h"
  35. /* Compile with -DPHP_HTTP_PARSER_STRICT=0 to make less checks, but run
  36. * faster
  37. */
  38. #ifndef PHP_HTTP_PARSER_STRICT
  39. # define PHP_HTTP_PARSER_STRICT 1
  40. #else
  41. # define PHP_HTTP_PARSER_STRICT 0
  42. #endif
  43. /* Maximum header size allowed */
  44. #define PHP_HTTP_MAX_HEADER_SIZE (80*1024)
  45. typedef struct php_http_parser php_http_parser;
  46. typedef struct php_http_parser_settings php_http_parser_settings;
  47. /* Callbacks should return non-zero to indicate an error. The parser will
  48. * then halt execution.
  49. *
  50. * The one exception is on_headers_complete. In a PHP_HTTP_RESPONSE parser
  51. * returning '1' from on_headers_complete will tell the parser that it
  52. * should not expect a body. This is used when receiving a response to a
  53. * HEAD request which may contain 'Content-Length' or 'Transfer-Encoding:
  54. * chunked' headers that indicate the presence of a body.
  55. *
  56. * http_data_cb does not return data chunks. It will be call arbitrarily
  57. * many times for each string. E.G. you might get 10 callbacks for "on_path"
  58. * each providing just a few characters more data.
  59. */
  60. typedef int (*php_http_data_cb) (php_http_parser*, const char *at, size_t length);
  61. typedef int (*php_http_cb) (php_http_parser*);
  62. /* Request Methods */
  63. enum php_http_method
  64. { PHP_HTTP_DELETE = 0
  65. , PHP_HTTP_GET
  66. , PHP_HTTP_HEAD
  67. , PHP_HTTP_POST
  68. , PHP_HTTP_PUT
  69. , PHP_HTTP_PATCH
  70. /* pathological */
  71. , PHP_HTTP_CONNECT
  72. , PHP_HTTP_OPTIONS
  73. , PHP_HTTP_TRACE
  74. /* webdav */
  75. , PHP_HTTP_COPY
  76. , PHP_HTTP_LOCK
  77. , PHP_HTTP_MKCOL
  78. , PHP_HTTP_MOVE
  79. , PHP_HTTP_MKCALENDAR
  80. , PHP_HTTP_PROPFIND
  81. , PHP_HTTP_PROPPATCH
  82. , PHP_HTTP_SEARCH
  83. , PHP_HTTP_UNLOCK
  84. /* subversion */
  85. , PHP_HTTP_REPORT
  86. , PHP_HTTP_MKACTIVITY
  87. , PHP_HTTP_CHECKOUT
  88. , PHP_HTTP_MERGE
  89. /* upnp */
  90. , PHP_HTTP_MSEARCH
  91. , PHP_HTTP_NOTIFY
  92. , PHP_HTTP_SUBSCRIBE
  93. , PHP_HTTP_UNSUBSCRIBE
  94. /* unknown, not implemented */
  95. , PHP_HTTP_NOT_IMPLEMENTED
  96. };
  97. enum php_http_parser_type { PHP_HTTP_REQUEST, PHP_HTTP_RESPONSE, PHP_HTTP_BOTH };
  98. enum state
  99. { s_dead = 1 /* important that this is > 0 */
  100. , s_start_req_or_res
  101. , s_res_or_resp_H
  102. , s_start_res
  103. , s_res_H
  104. , s_res_HT
  105. , s_res_HTT
  106. , s_res_HTTP
  107. , s_res_first_http_major
  108. , s_res_http_major
  109. , s_res_first_http_minor
  110. , s_res_http_minor
  111. , s_res_first_status_code
  112. , s_res_status_code
  113. , s_res_status
  114. , s_res_line_almost_done
  115. , s_start_req
  116. , s_req_method
  117. , s_req_spaces_before_url
  118. , s_req_schema
  119. , s_req_schema_slash
  120. , s_req_schema_slash_slash
  121. , s_req_host
  122. , s_req_port
  123. , s_req_path
  124. , s_req_query_string_start
  125. , s_req_query_string
  126. , s_req_fragment_start
  127. , s_req_fragment
  128. , s_req_http_start
  129. , s_req_http_H
  130. , s_req_http_HT
  131. , s_req_http_HTT
  132. , s_req_http_HTTP
  133. , s_req_first_http_major
  134. , s_req_http_major
  135. , s_req_first_http_minor
  136. , s_req_http_minor
  137. , s_req_line_almost_done
  138. , s_header_field_start
  139. , s_header_field
  140. , s_header_value_start
  141. , s_header_value
  142. , s_header_almost_done
  143. , s_headers_almost_done
  144. /* Important: 's_headers_almost_done' must be the last 'header' state. All
  145. * states beyond this must be 'body' states. It is used for overflow
  146. * checking. See the PARSING_HEADER() macro.
  147. */
  148. , s_chunk_size_start
  149. , s_chunk_size
  150. , s_chunk_size_almost_done
  151. , s_chunk_parameters
  152. , s_chunk_data
  153. , s_chunk_data_almost_done
  154. , s_chunk_data_done
  155. , s_body_identity
  156. , s_body_identity_eof
  157. };
  158. struct php_http_parser {
  159. /** PRIVATE **/
  160. unsigned char type : 2;
  161. unsigned char flags : 6;
  162. unsigned char state;
  163. unsigned char header_state;
  164. unsigned char index;
  165. uint32_t nread;
  166. ssize_t content_length;
  167. /** READ-ONLY **/
  168. unsigned short http_major;
  169. unsigned short http_minor;
  170. unsigned short status_code; /* responses only */
  171. unsigned char method; /* requests only */
  172. /* 1 = Upgrade header was present and the parser has exited because of that.
  173. * 0 = No upgrade header present.
  174. * Should be checked when http_parser_execute() returns in addition to
  175. * error checking.
  176. */
  177. char upgrade;
  178. /** PUBLIC **/
  179. void *data; /* A pointer to get hook to the "connection" or "socket" object */
  180. };
  181. struct php_http_parser_settings {
  182. php_http_cb on_message_begin;
  183. php_http_data_cb on_path;
  184. php_http_data_cb on_query_string;
  185. php_http_data_cb on_url;
  186. php_http_data_cb on_fragment;
  187. php_http_data_cb on_header_field;
  188. php_http_data_cb on_header_value;
  189. php_http_cb on_headers_complete;
  190. php_http_data_cb on_body;
  191. php_http_cb on_message_complete;
  192. };
  193. void php_http_parser_init(php_http_parser *parser, enum php_http_parser_type type);
  194. size_t php_http_parser_execute(php_http_parser *parser,
  195. const php_http_parser_settings *settings,
  196. const char *data,
  197. size_t len);
  198. /* If php_http_should_keep_alive() in the on_headers_complete or
  199. * on_message_complete callback returns true, then this will be should be
  200. * the last message on the connection.
  201. * If you are the server, respond with the "Connection: close" header.
  202. * If you are the client, close the connection.
  203. */
  204. int php_http_should_keep_alive(php_http_parser *parser);
  205. /* Returns a string version of the HTTP method. */
  206. const char *php_http_method_str(enum php_http_method);
  207. #ifdef __cplusplus
  208. }
  209. #endif
  210. #endif