Patch the new http_parser to support RTSP requests again
[deb_shairplay.git] / src / lib / http_parser.h
CommitLineData
2340bcd3
JVH
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
24extern "C" {
25#endif
26
462c72aa 27#define HTTP_PARSER_VERSION_MAJOR 2
2340bcd3
JVH
28#define HTTP_PARSER_VERSION_MINOR 0
29
30#include <sys/types.h>
462c72aa
JVH
31#if defined(_WIN32) && !defined(__MINGW32__) && (!defined(_MSC_VER) || _MSC_VER<1600)
32#include <BaseTsd.h>
2340bcd3
JVH
33typedef __int8 int8_t;
34typedef unsigned __int8 uint8_t;
35typedef __int16 int16_t;
36typedef unsigned __int16 uint16_t;
37typedef __int32 int32_t;
38typedef unsigned __int32 uint32_t;
39typedef __int64 int64_t;
40typedef unsigned __int64 uint64_t;
462c72aa
JVH
41typedef SIZE_T size_t;
42typedef SSIZE_T ssize_t;
2340bcd3
JVH
43#else
44#include <stdint.h>
45#endif
46
47/* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run
48 * faster
49 */
50#ifndef HTTP_PARSER_STRICT
efc9c140 51# define HTTP_PARSER_STRICT 0
2340bcd3
JVH
52#endif
53
54/* Compile with -DHTTP_PARSER_DEBUG=1 to add extra debugging information to
55 * the error reporting facility.
56 */
57#ifndef HTTP_PARSER_DEBUG
58# define HTTP_PARSER_DEBUG 0
59#endif
60
61
62/* Maximium header size allowed */
63#define HTTP_MAX_HEADER_SIZE (80*1024)
64
65
66typedef struct http_parser http_parser;
67typedef struct http_parser_settings http_parser_settings;
2340bcd3
JVH
68
69
70/* Callbacks should return non-zero to indicate an error. The parser will
71 * then halt execution.
72 *
73 * The one exception is on_headers_complete. In a HTTP_RESPONSE parser
74 * returning '1' from on_headers_complete will tell the parser that it
75 * should not expect a body. This is used when receiving a response to a
76 * HEAD request which may contain 'Content-Length' or 'Transfer-Encoding:
77 * chunked' headers that indicate the presence of a body.
78 *
79 * http_data_cb does not return data chunks. It will be call arbitrarally
80 * many times for each string. E.G. you might get 10 callbacks for "on_path"
81 * each providing just a few characters more data.
82 */
83typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
84typedef int (*http_cb) (http_parser*);
85
86
87/* Request Methods */
462c72aa
JVH
88#define HTTP_METHOD_MAP(XX) \
89 XX(0, DELETE, DELETE) \
90 XX(1, GET, GET) \
91 XX(2, HEAD, HEAD) \
92 XX(3, POST, POST) \
93 XX(4, PUT, PUT) \
94 /* pathological */ \
95 XX(5, CONNECT, CONNECT) \
96 XX(6, OPTIONS, OPTIONS) \
97 XX(7, TRACE, TRACE) \
98 /* webdav */ \
99 XX(8, COPY, COPY) \
100 XX(9, LOCK, LOCK) \
101 XX(10, MKCOL, MKCOL) \
102 XX(11, MOVE, MOVE) \
103 XX(12, PROPFIND, PROPFIND) \
104 XX(13, PROPPATCH, PROPPATCH) \
105 XX(14, SEARCH, SEARCH) \
106 XX(15, UNLOCK, UNLOCK) \
107 /* subversion */ \
108 XX(16, REPORT, REPORT) \
109 XX(17, MKACTIVITY, MKACTIVITY) \
110 XX(18, CHECKOUT, CHECKOUT) \
111 XX(19, MERGE, MERGE) \
112 /* upnp */ \
113 XX(20, MSEARCH, M-SEARCH) \
114 XX(21, NOTIFY, NOTIFY) \
115 XX(22, SUBSCRIBE, SUBSCRIBE) \
116 XX(23, UNSUBSCRIBE, UNSUBSCRIBE) \
117 /* RFC-5789 */ \
118 XX(24, PATCH, PATCH) \
119 XX(25, PURGE, PURGE) \
efc9c140
JVH
120 /* RFC-2326 (RTSP) */ \
121 XX(26, DESCRIBE, DESCRIBE) \
122 XX(27, ANNOUNCE, ANNOUNCE) \
123 XX(28, SETUP, SETUP) \
124 XX(29, PLAY, PLAY) \
125 XX(30, PAUSE, PAUSE) \
126 XX(31, TEARDOWN, TEARDOWN) \
127 XX(32, GET_PARAMETER, GET_PARAMETER) \
128 XX(33, SET_PARAMETER, SET_PARAMETER) \
129 XX(34, REDIRECT, REDIRECT) \
130 XX(35, RECORD, RECORD) \
131 /* RAOP */ \
132 XX(36, FLUSH, FLUSH) \
462c72aa 133
2340bcd3 134enum http_method
462c72aa
JVH
135 {
136#define XX(num, name, string) HTTP_##name = num,
137 HTTP_METHOD_MAP(XX)
138#undef XX
2340bcd3
JVH
139 };
140
141
142enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
143
144
145/* Flag values for http_parser.flags field */
146enum flags
147 { F_CHUNKED = 1 << 0
148 , F_CONNECTION_KEEP_ALIVE = 1 << 1
149 , F_CONNECTION_CLOSE = 1 << 2
150 , F_TRAILING = 1 << 3
151 , F_UPGRADE = 1 << 4
152 , F_SKIPBODY = 1 << 5
153 };
154
155
156/* Map for errno-related constants
157 *
158 * The provided argument should be a macro that takes 2 arguments.
159 */
160#define HTTP_ERRNO_MAP(XX) \
161 /* No error */ \
162 XX(OK, "success") \
163 \
164 /* Callback-related errors */ \
165 XX(CB_message_begin, "the on_message_begin callback failed") \
2340bcd3 166 XX(CB_url, "the on_url callback failed") \
2340bcd3
JVH
167 XX(CB_header_field, "the on_header_field callback failed") \
168 XX(CB_header_value, "the on_header_value callback failed") \
169 XX(CB_headers_complete, "the on_headers_complete callback failed") \
170 XX(CB_body, "the on_body callback failed") \
171 XX(CB_message_complete, "the on_message_complete callback failed") \
172 \
173 /* Parsing-related errors */ \
174 XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \
175 XX(HEADER_OVERFLOW, \
176 "too many header bytes seen; overflow detected") \
177 XX(CLOSED_CONNECTION, \
178 "data received after completed connection: close message") \
179 XX(INVALID_VERSION, "invalid HTTP version") \
180 XX(INVALID_STATUS, "invalid HTTP status code") \
181 XX(INVALID_METHOD, "invalid HTTP method") \
182 XX(INVALID_URL, "invalid URL") \
183 XX(INVALID_HOST, "invalid host") \
184 XX(INVALID_PORT, "invalid port") \
185 XX(INVALID_PATH, "invalid path") \
186 XX(INVALID_QUERY_STRING, "invalid query string") \
187 XX(INVALID_FRAGMENT, "invalid fragment") \
188 XX(LF_EXPECTED, "LF character expected") \
189 XX(INVALID_HEADER_TOKEN, "invalid character in header") \
190 XX(INVALID_CONTENT_LENGTH, \
191 "invalid character in content-length header") \
192 XX(INVALID_CHUNK_SIZE, \
193 "invalid character in chunk size header") \
194 XX(INVALID_CONSTANT, "invalid constant string") \
195 XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\
196 XX(STRICT, "strict mode assertion failed") \
462c72aa 197 XX(PAUSED, "parser is paused") \
2340bcd3
JVH
198 XX(UNKNOWN, "an unknown error occurred")
199
200
201/* Define HPE_* values for each errno value above */
202#define HTTP_ERRNO_GEN(n, s) HPE_##n,
203enum http_errno {
204 HTTP_ERRNO_MAP(HTTP_ERRNO_GEN)
205};
206#undef HTTP_ERRNO_GEN
207
208
209/* Get an http_errno value from an http_parser */
210#define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno)
211
212/* Get the line number that generated the current error */
213#if HTTP_PARSER_DEBUG
214#define HTTP_PARSER_ERRNO_LINE(p) ((p)->error_lineno)
215#else
216#define HTTP_PARSER_ERRNO_LINE(p) 0
217#endif
218
219
220struct http_parser {
221 /** PRIVATE **/
462c72aa
JVH
222 unsigned char type : 2; /* enum http_parser_type */
223 unsigned char flags : 6; /* F_* values from 'flags' enum; semi-public */
224 unsigned char state; /* enum state from http_parser.c */
225 unsigned char header_state; /* enum header_state from http_parser.c */
226 unsigned char index; /* index into current matcher */
2340bcd3 227
462c72aa
JVH
228 uint32_t nread; /* # bytes read in various scenarios */
229 uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */
2340bcd3
JVH
230
231 /** READ-ONLY **/
232 unsigned short http_major;
233 unsigned short http_minor;
234 unsigned short status_code; /* responses only */
462c72aa 235 unsigned char method; /* requests only */
2340bcd3
JVH
236 unsigned char http_errno : 7;
237
238 /* 1 = Upgrade header was present and the parser has exited because of that.
239 * 0 = No upgrade header present.
240 * Should be checked when http_parser_execute() returns in addition to
241 * error checking.
242 */
243 unsigned char upgrade : 1;
244
245#if HTTP_PARSER_DEBUG
246 uint32_t error_lineno;
247#endif
248
249 /** PUBLIC **/
250 void *data; /* A pointer to get hook to the "connection" or "socket" object */
251};
252
253
254struct http_parser_settings {
255 http_cb on_message_begin;
256 http_data_cb on_url;
257 http_data_cb on_header_field;
258 http_data_cb on_header_value;
259 http_cb on_headers_complete;
260 http_data_cb on_body;
261 http_cb on_message_complete;
262};
263
264
462c72aa
JVH
265enum http_parser_url_fields
266 { UF_SCHEMA = 0
267 , UF_HOST = 1
268 , UF_PORT = 2
269 , UF_PATH = 3
270 , UF_QUERY = 4
271 , UF_FRAGMENT = 5
272 , UF_USERINFO = 6
273 , UF_MAX = 7
274 };
275
276
277/* Result structure for http_parser_parse_url().
278 *
279 * Callers should index into field_data[] with UF_* values iff field_set
280 * has the relevant (1 << UF_*) bit set. As a courtesy to clients (and
281 * because we probably have padding left over), we convert any port to
282 * a uint16_t.
283 */
284struct http_parser_url {
285 uint16_t field_set; /* Bitmask of (1 << UF_*) values */
286 uint16_t port; /* Converted UF_PORT string */
287
288 struct {
289 uint16_t off; /* Offset into buffer in which field starts */
290 uint16_t len; /* Length of run in buffer */
291 } field_data[UF_MAX];
292};
293
294
295void http_parser_init(http_parser *parser, enum http_parser_type type);
2340bcd3
JVH
296
297
298size_t http_parser_execute(http_parser *parser,
299 const http_parser_settings *settings,
300 const char *data,
301 size_t len);
302
303
304/* If http_should_keep_alive() in the on_headers_complete or
462c72aa 305 * on_message_complete callback returns 0, then this should be
2340bcd3
JVH
306 * the last message on the connection.
307 * If you are the server, respond with the "Connection: close" header.
308 * If you are the client, close the connection.
309 */
462c72aa 310int http_should_keep_alive(const http_parser *parser);
2340bcd3
JVH
311
312/* Returns a string version of the HTTP method. */
313const char *http_method_str(enum http_method m);
314
315/* Return a string name of the given error */
316const char *http_errno_name(enum http_errno err);
317
318/* Return a string description of the given error */
319const char *http_errno_description(enum http_errno err);
320
462c72aa
JVH
321/* Parse a URL; return nonzero on failure */
322int http_parser_parse_url(const char *buf, size_t buflen,
323 int is_connect,
324 struct http_parser_url *u);
325
326/* Pause or un-pause the parser; a nonzero value pauses */
327void http_parser_pause(http_parser *parser, int paused);
328
329/* Checks if this is the final chunk of the body. */
330int http_body_is_final(const http_parser *parser);
331
2340bcd3
JVH
332#ifdef __cplusplus
333}
334#endif
335#endif