summaryrefslogtreecommitdiffstats
path: root/src/lib-http/http-message-parser.h
blob: 17c17cb2b5b5e7b4cb401e4ef1f21cb5ce8e0bf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifndef HTTP_MESSAGE_PARSER_H
#define HTTP_MESSAGE_PARSER_H

#include "http-response.h"
#include "http-transfer.h"

#include "http-header.h"

enum http_message_parse_error {
	HTTP_MESSAGE_PARSE_ERROR_NONE = 0,          /* no error */
	HTTP_MESSAGE_PARSE_ERROR_BROKEN_STREAM,     /* stream error */
	HTTP_MESSAGE_PARSE_ERROR_BROKEN_MESSAGE,    /* unrecoverable generic error */
	HTTP_MESSAGE_PARSE_ERROR_BAD_MESSAGE,       /* recoverable generic error */
	HTTP_MESSAGE_PARSE_ERROR_NOT_IMPLEMENTED,   /* used unimplemented feature
	                                              (recoverable) */
	HTTP_MESSAGE_PARSE_ERROR_PAYLOAD_TOO_LARGE  /* message payload is too large
	                                              (fatal) */
};

enum http_message_parse_flags {
	/* Strictly adhere to the HTTP protocol specification */
	HTTP_MESSAGE_PARSE_FLAG_STRICT = BIT(0)
};

struct http_message {
	pool_t pool;

	unsigned int version_major;
	unsigned int version_minor;

	struct http_header *header;

	time_t date;
	uoff_t content_length;
	const char *location;
	ARRAY_TYPE(http_transfer_coding) transfer_encoding;
	ARRAY_TYPE(const_string) connection_options;

	bool connection_close:1;
	bool have_content_length:1;
};

struct http_message_parser {
	struct istream *input;

	struct http_header_limits header_limits;
	uoff_t max_payload_size;
	enum http_message_parse_flags flags;

	const unsigned char *begin, *cur, *end;

	const char *error;
	enum http_message_parse_error error_code;

	struct http_header_parser *header_parser;
	struct istream *payload;

	pool_t msg_pool;
	struct http_message msg;
};

void http_message_parser_init(struct http_message_parser *parser,
	struct istream *input, const struct http_header_limits *hdr_limits,
	uoff_t max_payload_size, enum http_message_parse_flags flags)
	ATTR_NULL(3);
void http_message_parser_deinit(struct http_message_parser *parser);
void http_message_parser_restart(struct http_message_parser *parser,
	pool_t pool);

pool_t http_message_parser_get_pool(struct http_message_parser *parser);

int http_message_parse_finish_payload(struct http_message_parser *parser);
int http_message_parse_version(struct http_message_parser *parser);
int http_message_parse_headers(struct http_message_parser *parser);
int http_message_parse_body(struct http_message_parser *parser, bool request);

#endif