http_parser.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  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. #ifndef http_parser_h
  22. #define http_parser_h
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. #define HTTP_PARSER_VERSION_MAJOR 1
  27. #define HTTP_PARSER_VERSION_MINOR 0
  28. #include <sys/types.h>
  29. #if defined(_WIN32) && !defined(__MINGW32__)
  30. typedef __int8 int8_t;
  31. typedef unsigned __int8 uint8_t;
  32. typedef __int16 int16_t;
  33. typedef unsigned __int16 uint16_t;
  34. typedef __int32 int32_t;
  35. typedef unsigned __int32 uint32_t;
  36. typedef __int64 int64_t;
  37. typedef unsigned __int64 uint64_t;
  38. typedef unsigned int size_t;
  39. typedef int ssize_t;
  40. #else
  41. #include <stdint.h>
  42. #endif
  43. /* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run
  44. * faster
  45. */
  46. #ifndef HTTP_PARSER_STRICT
  47. # define HTTP_PARSER_STRICT 1
  48. #else
  49. # define HTTP_PARSER_STRICT 0
  50. #endif
  51. /* Maximium header size allowed */
  52. #define HTTP_MAX_HEADER_SIZE (80*1024)
  53. typedef struct http_parser http_parser;
  54. typedef struct http_parser_settings http_parser_settings;
  55. /* Callbacks should return non-zero to indicate an error. The parser will
  56. * then halt execution.
  57. *
  58. * The one exception is on_headers_complete. In a HTTP_RESPONSE parser
  59. * returning '1' from on_headers_complete will tell the parser that it
  60. * should not expect a body. This is used when receiving a response to a
  61. * HEAD request which may contain 'Content-Length' or 'Transfer-Encoding:
  62. * chunked' headers that indicate the presence of a body.
  63. *
  64. * http_data_cb does not return data chunks. It will be call arbitrarally
  65. * many times for each string. E.G. you might get 10 callbacks for "on_path"
  66. * each providing just a few characters more data.
  67. */
  68. typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
  69. typedef int (*http_cb) (http_parser*);
  70. /* Request Methods */
  71. enum http_method
  72. { HTTP_DELETE = 0
  73. , HTTP_GET
  74. , HTTP_HEAD
  75. , HTTP_POST
  76. , HTTP_PUT
  77. /* pathological */
  78. , HTTP_CONNECT
  79. , HTTP_OPTIONS
  80. , HTTP_TRACE
  81. /* webdav */
  82. , HTTP_COPY
  83. , HTTP_LOCK
  84. , HTTP_MKCOL
  85. , HTTP_MOVE
  86. , HTTP_PROPFIND
  87. , HTTP_PROPPATCH
  88. , HTTP_UNLOCK
  89. /* subversion */
  90. , HTTP_REPORT
  91. , HTTP_MKACTIVITY
  92. , HTTP_CHECKOUT
  93. , HTTP_MERGE
  94. /* upnp */
  95. , HTTP_MSEARCH
  96. , HTTP_NOTIFY
  97. , HTTP_SUBSCRIBE
  98. , HTTP_UNSUBSCRIBE
  99. };
  100. enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
  101. struct http_parser {
  102. /** PRIVATE **/
  103. unsigned char type : 2;
  104. unsigned char flags : 6;
  105. unsigned char state;
  106. unsigned char header_state;
  107. unsigned char index;
  108. uint32_t nread;
  109. int64_t content_length;
  110. /** READ-ONLY **/
  111. unsigned short http_major;
  112. unsigned short http_minor;
  113. unsigned short status_code; /* responses only */
  114. unsigned char method; /* requests only */
  115. /* 1 = Upgrade header was present and the parser has exited because of that.
  116. * 0 = No upgrade header present.
  117. * Should be checked when http_parser_execute() returns in addition to
  118. * error checking.
  119. */
  120. char upgrade;
  121. /** PUBLIC **/
  122. void *data; /* A pointer to get hook to the "connection" or "socket" object */
  123. };
  124. struct http_parser_settings {
  125. http_cb on_message_begin;
  126. http_data_cb on_path;
  127. http_data_cb on_query_string;
  128. http_data_cb on_url;
  129. http_data_cb on_fragment;
  130. http_data_cb on_header_field;
  131. http_data_cb on_header_value;
  132. http_cb on_headers_complete;
  133. http_data_cb on_body;
  134. http_cb on_message_complete;
  135. };
  136. void http_parser_init(http_parser *parser, enum http_parser_type type);
  137. size_t http_parser_execute(http_parser *parser,
  138. const http_parser_settings *settings,
  139. const char *data,
  140. size_t len);
  141. /* If http_should_keep_alive() in the on_headers_complete or
  142. * on_message_complete callback returns true, then this will be should be
  143. * the last message on the connection.
  144. * If you are the server, respond with the "Connection: close" header.
  145. * If you are the client, close the connection.
  146. */
  147. int http_should_keep_alive(http_parser *parser);
  148. /* Returns a string version of the HTTP method. */
  149. const char *http_method_str(enum http_method);
  150. #ifdef __cplusplus
  151. }
  152. #endif
  153. #endif