1 #ifndef HTTP_MESSAGE_PARSER_H 2 #define HTTP_MESSAGE_PARSER_H 3 4 #include "http-response.h" 5 #include "http-transfer.h" 6 7 #include "http-header.h" 8 9 enum http_message_parse_error { 10 HTTP_MESSAGE_PARSE_ERROR_NONE = 0, /* no error */ 11 HTTP_MESSAGE_PARSE_ERROR_BROKEN_STREAM, /* stream error */ 12 HTTP_MESSAGE_PARSE_ERROR_BROKEN_MESSAGE, /* unrecoverable generic error */ 13 HTTP_MESSAGE_PARSE_ERROR_BAD_MESSAGE, /* recoverable generic error */ 14 HTTP_MESSAGE_PARSE_ERROR_NOT_IMPLEMENTED, /* used unimplemented feature 15 (recoverable) */ 16 HTTP_MESSAGE_PARSE_ERROR_PAYLOAD_TOO_LARGE /* message payload is too large 17 (fatal) */ 18 }; 19 20 enum http_message_parse_flags { 21 /* Strictly adhere to the HTTP protocol specification */ 22 HTTP_MESSAGE_PARSE_FLAG_STRICT = BIT(0) 23 }; 24 25 struct http_message { 26 pool_t pool; 27 28 unsigned int version_major; 29 unsigned int version_minor; 30 31 struct http_header *header; 32 33 time_t date; 34 uoff_t content_length; 35 const char *location; 36 ARRAY_TYPE(http_transfer_coding) transfer_encoding; 37 ARRAY_TYPE(const_string) connection_options; 38 39 bool connection_close:1; 40 bool have_content_length:1; 41 }; 42 43 struct http_message_parser { 44 struct istream *input; 45 46 struct http_header_limits header_limits; 47 uoff_t max_payload_size; 48 enum http_message_parse_flags flags; 49 50 const unsigned char *begin, *cur, *end; 51 52 const char *error; 53 enum http_message_parse_error error_code; 54 55 struct http_header_parser *header_parser; 56 struct istream *payload; 57 58 pool_t msg_pool; 59 struct http_message msg; 60 }; 61 62 void http_message_parser_init(struct http_message_parser *parser, 63 struct istream *input, const struct http_header_limits *hdr_limits, 64 uoff_t max_payload_size, enum http_message_parse_flags flags) 65 ATTR_NULL(3); 66 void http_message_parser_deinit(struct http_message_parser *parser); 67 void http_message_parser_restart(struct http_message_parser *parser, 68 pool_t pool); 69 70 pool_t http_message_parser_get_pool(struct http_message_parser *parser); 71 72 int http_message_parse_finish_payload(struct http_message_parser *parser); 73 int http_message_parse_version(struct http_message_parser *parser); 74 int http_message_parse_headers(struct http_message_parser *parser); 75 int http_message_parse_body(struct http_message_parser *parser, bool request); 76 77 #endif 78