php_http_parser.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. /* Maximium 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 arbitrarally
  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_UNLOCK
  83. /* subversion */
  84. , PHP_HTTP_REPORT
  85. , PHP_HTTP_MKACTIVITY
  86. , PHP_HTTP_CHECKOUT
  87. , PHP_HTTP_MERGE
  88. /* upnp */
  89. , PHP_HTTP_MSEARCH
  90. , PHP_HTTP_NOTIFY
  91. , PHP_HTTP_SUBSCRIBE
  92. , PHP_HTTP_UNSUBSCRIBE
  93. /* unknown, not implemented */
  94. , PHP_HTTP_NOT_IMPLEMENTED
  95. };
  96. enum php_http_parser_type { PHP_HTTP_REQUEST, PHP_HTTP_RESPONSE, PHP_HTTP_BOTH };
  97. struct php_http_parser {
  98. /** PRIVATE **/
  99. unsigned char type : 2;
  100. unsigned char flags : 6;
  101. unsigned char state;
  102. unsigned char header_state;
  103. unsigned char index;
  104. uint32_t nread;
  105. ssize_t content_length;
  106. /** READ-ONLY **/
  107. unsigned short http_major;
  108. unsigned short http_minor;
  109. unsigned short status_code; /* responses only */
  110. unsigned char method; /* requests only */
  111. /* 1 = Upgrade header was present and the parser has exited because of that.
  112. * 0 = No upgrade header present.
  113. * Should be checked when http_parser_execute() returns in addition to
  114. * error checking.
  115. */
  116. char upgrade;
  117. /** PUBLIC **/
  118. void *data; /* A pointer to get hook to the "connection" or "socket" object */
  119. };
  120. struct php_http_parser_settings {
  121. php_http_cb on_message_begin;
  122. php_http_data_cb on_path;
  123. php_http_data_cb on_query_string;
  124. php_http_data_cb on_url;
  125. php_http_data_cb on_fragment;
  126. php_http_data_cb on_header_field;
  127. php_http_data_cb on_header_value;
  128. php_http_cb on_headers_complete;
  129. php_http_data_cb on_body;
  130. php_http_cb on_message_complete;
  131. };
  132. void php_http_parser_init(php_http_parser *parser, enum php_http_parser_type type);
  133. size_t php_http_parser_execute(php_http_parser *parser,
  134. const php_http_parser_settings *settings,
  135. const char *data,
  136. size_t len);
  137. /* If php_http_should_keep_alive() in the on_headers_complete or
  138. * on_message_complete callback returns true, then this will be should be
  139. * the last message on the connection.
  140. * If you are the server, respond with the "Connection: close" header.
  141. * If you are the client, close the connection.
  142. */
  143. int php_http_should_keep_alive(php_http_parser *parser);
  144. /* Returns a string version of the HTTP method. */
  145. const char *php_http_method_str(enum php_http_method);
  146. #ifdef __cplusplus
  147. }
  148. #endif
  149. #endif