TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
http_parser.h
Go to the documentation of this file.
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 HTTPPARSER_H
22 #define HTTPPARSER_H
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Also update SONAME in the Makefile whenever you change these. */
28 #define HTTP_PARSER_VERSION_MAJOR 2
29 #define HTTP_PARSER_VERSION_MINOR 5
30 #define HTTP_PARSER_VERSION_PATCH 0
31 
32 #include <sys/types.h>
33 #if defined(_WIN32) && !defined(__MINGW32__) && (!defined(_MSC_VER) || _MSC_VER<1600)
34 #include <BaseTsd.h>
35 #include <stddef.h>
36 typedef __int8 int8_t;
37 typedef unsigned __int8 uint8_t;
38 typedef __int16 int16_t;
39 typedef unsigned __int16 uint16_t;
40 typedef __int32 int32_t;
41 typedef unsigned __int32 uint32_t;
42 typedef __int64 int64_t;
43 typedef unsigned __int64 uint64_t;
44 #else
45 #include <stdint.h>
46 #endif
47 
48 /* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run
49  * faster
50  */
51 #ifndef HTTP_PARSER_STRICT
52 # define HTTP_PARSER_STRICT 1
53 #endif
54 
55 /* Maximium header size allowed. If the macro is not defined
56  * before including this header then the default is used. To
57  * change the maximum header size, define the macro in the build
58  * environment (e.g. -DHTTP_MAX_HEADER_SIZE=<value>). To remove
59  * the effective limit on the size of the header, define the macro
60  * to a very large number (e.g. -DHTTP_MAX_HEADER_SIZE=0x7fffffff)
61  */
62 #ifndef HTTP_MAX_HEADER_SIZE
63 # define HTTP_MAX_HEADER_SIZE (80*1024)
64 #endif
65 
66 typedef struct http_parser http_parser;
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 called arbitrarily
80  * many times for each string. E.G. you might get 10 callbacks for "on_url"
81  * each providing just a few characters more data.
82  */
83 typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
84 typedef int (*http_cb) (http_parser*);
85 
86 
87 /* Request Methods */
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) \
120  /* CalDAV */ \
121  XX(26, MKCALENDAR, MKCALENDAR) \
122 
124  {
125 #define XX(num, name, string) HTTP_##name = num,
127 #undef XX
128  };
129 
130 
132 
133 
134 /* Flag values for http_parser.flags field */
135 enum flags
136  { F_CHUNKED = 1 << 0
140  , F_TRAILING = 1 << 4
141  , F_UPGRADE = 1 << 5
142  , F_SKIPBODY = 1 << 6
143  };
144 
145 
146 /* Map for errno-related constants
147  *
148  * The provided argument should be a macro that takes 2 arguments.
149  */
150 #define HTTP_ERRNO_MAP(XX) \
151  /* No error */ \
152  XX(OK, "success") \
153  \
154  /* Callback-related errors */ \
155  XX(CB_message_begin, "the on_message_begin callback failed") \
156  XX(CB_url, "the on_url callback failed") \
157  XX(CB_header_field, "the on_header_field callback failed") \
158  XX(CB_header_value, "the on_header_value callback failed") \
159  XX(CB_headers_complete, "the on_headers_complete callback failed") \
160  XX(CB_body, "the on_body callback failed") \
161  XX(CB_message_complete, "the on_message_complete callback failed") \
162  XX(CB_status, "the on_status callback failed") \
163  XX(CB_chunk_header, "the on_chunk_header callback failed") \
164  XX(CB_chunk_complete, "the on_chunk_complete callback failed") \
165  \
166  /* Parsing-related errors */ \
167  XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \
168  XX(HEADER_OVERFLOW, \
169  "too many header bytes seen; overflow detected") \
170  XX(CLOSED_CONNECTION, \
171  "data received after completed connection: close message") \
172  XX(INVALID_VERSION, "invalid HTTP version") \
173  XX(INVALID_STATUS, "invalid HTTP status code") \
174  XX(INVALID_METHOD, "invalid HTTP method") \
175  XX(INVALID_URL, "invalid URL") \
176  XX(INVALID_HOST, "invalid host") \
177  XX(INVALID_PORT, "invalid port") \
178  XX(INVALID_PATH, "invalid path") \
179  XX(INVALID_QUERY_STRING, "invalid query string") \
180  XX(INVALID_FRAGMENT, "invalid fragment") \
181  XX(LF_EXPECTED, "LF character expected") \
182  XX(INVALID_HEADER_TOKEN, "invalid character in header") \
183  XX(INVALID_CONTENT_LENGTH, \
184  "invalid character in content-length header") \
185  XX(INVALID_CHUNK_SIZE, \
186  "invalid character in chunk size header") \
187  XX(INVALID_CONSTANT, "invalid constant string") \
188  XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\
189  XX(STRICT, "strict mode assertion failed") \
190  XX(PAUSED, "parser is paused") \
191  XX(UNKNOWN, "an unknown error occurred")
192 
193 
194 /* Define HPE_* values for each errno value above */
195 #define HTTP_ERRNO_GEN(n, s) HPE_##n,
198 };
199 #undef HTTP_ERRNO_GEN
200 
201 
202 /* Get an http_errno value from an http_parser */
203 #define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno)
204 
205 
206 struct http_parser {
208  unsigned int type : 2; /* enum http_parser_type */
209  unsigned int flags : 7; /* F_* values from 'flags' enum; semi-public */
210  unsigned int state : 7; /* enum state from http_parser.c */
211  unsigned int header_state : 8; /* enum header_state from http_parser.c */
212  unsigned int index : 8; /* index into current matcher */
213 
214  uint32_t nread; /* # bytes read in various scenarios */
215  uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */
216 
218  unsigned short http_major;
219  unsigned short http_minor;
220  unsigned int status_code : 16; /* responses only */
221  unsigned int method : 8; /* requests only */
222  unsigned int http_errno : 7;
223 
224  /* 1 = Upgrade header was present and the parser has exited because of that.
225  * 0 = No upgrade header present.
226  * Should be checked when http_parser_execute() returns in addition to
227  * error checking.
228  */
229  unsigned int upgrade : 1;
230 
232  void *data; /* A pointer to get hook to the "connection" or "socket" object */
233 };
234 
235 
245  /* When on_chunk_header is called, the current chunk length is stored
246  * in parser->content_length.
247  */
250 };
251 
252 
254  { UF_SCHEMA = 0
255  , UF_HOST = 1
256  , UF_PORT = 2
257  , UF_PATH = 3
258  , UF_QUERY = 4
261  , UF_MAX = 7
262  };
263 
264 
265 /* Result structure for http_parser_parse_url().
266  *
267  * Callers should index into field_data[] with UF_* values iff field_set
268  * has the relevant (1 << UF_*) bit set. As a courtesy to clients (and
269  * because we probably have padding left over), we convert any port to
270  * a uint16_t.
271  */
273  uint16_t field_set; /* Bitmask of (1 << UF_*) values */
274  uint16_t port; /* Converted UF_PORT string */
275 
276  struct {
277  uint16_t off; /* Offset into buffer in which field starts */
278  uint16_t len; /* Length of run in buffer */
279  } field_data[UF_MAX];
280 };
281 
282 
283 /* Returns the library version. Bits 16-23 contain the major version number,
284  * bits 8-15 the minor version number and bits 0-7 the patch level.
285  * Usage example:
286  *
287  * unsigned long version = http_parser_version();
288  * unsigned major = (version >> 16) & 255;
289  * unsigned minor = (version >> 8) & 255;
290  * unsigned patch = version & 255;
291  * printf("http_parser v%u.%u.%u\n", major, minor, patch);
292  */
293 unsigned long http_parser_version(void);
294 
295 void http_parser_init(http_parser *parser, enum http_parser_type type);
296 
297 
298 /* Initialize http_parser_settings members to 0
299  */
301 
302 
303 /* Executes the parser. Returns number of parsed bytes. Sets
304  * `parser->http_errno` on error. */
305 size_t http_parser_execute(http_parser *parser,
306  const http_parser_settings *settings,
307  const char *data,
308  size_t len);
309 
310 
311 /* If http_should_keep_alive() in the on_headers_complete or
312  * on_message_complete callback returns 0, then this should be
313  * the last message on the connection.
314  * If you are the server, respond with the "Connection: close" header.
315  * If you are the client, close the connection.
316  */
317 int http_should_keep_alive(const http_parser *parser);
318 
319 /* Returns a string version of the HTTP method. */
320 const char *http_method_str(enum http_method m);
321 
322 /* Return a string name of the given error */
323 const char *http_errno_name(enum http_errno err);
324 
325 /* Return a string description of the given error */
326 const char *http_errno_description(enum http_errno err);
327 
328 /* Parse a URL; return nonzero on failure */
329 int http_parser_parse_url(const char *buf, size_t buflen,
330  int is_connect,
331  struct http_parser_url *u);
332 
333 /* Pause or un-pause the parser; a nonzero value pauses */
334 void http_parser_pause(http_parser *parser, int paused);
335 
336 /* Checks if this is the final chunk of the body. */
337 int http_body_is_final(const http_parser *parser);
338 
339 #ifdef __cplusplus
340 }
341 #endif
342 #endif
uint32_t nread
Definition: http_parser.h:214
int(* http_cb)(http_parser *)
Definition: http_parser.h:84
#define HTTP_ERRNO_MAP(XX)
Definition: http_parser.h:150
unsigned int method
Definition: http_parser.h:221
flags
Definition: http_parser.h:135
http_data_cb on_header_field
Definition: http_parser.h:240
http_cb on_headers_complete
Definition: http_parser.h:242
unsigned short http_minor
Definition: http_parser.h:219
const char * http_method_str(enum http_method m)
Definition: http_parser.c:2131
#define HTTP_METHOD_MAP(XX)
Definition: http_parser.h:88
uint16_t field_set
Definition: http_parser.h:273
void http_parser_pause(http_parser *parser, int paused)
Definition: http_parser.c:2407
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
http_data_cb on_body
Definition: http_parser.h:243
unsigned int status_code
Definition: http_parser.h:220
unsigned int header_state
Definition: http_parser.h:211
http_data_cb on_url
Definition: http_parser.h:238
voidpf void * buf
Definition: ioapi.h:136
unsigned int index
Definition: http_parser.h:212
#define XX(num, name, string)
Definition: http_parser.h:125
uint64_t content_length
Definition: http_parser.h:215
size_t http_parser_execute(http_parser *parser, const http_parser_settings *settings, const char *data, size_t len)
Definition: http_parser.c:626
http_data_cb on_status
Definition: http_parser.h:239
unsigned short http_major
READ-ONLY.
Definition: http_parser.h:218
int http_parser_parse_url(const char *buf, size_t buflen, int is_connect, struct http_parser_url *u)
Definition: http_parser.c:2307
http_method
Definition: http_parser.h:123
http_parser_type
Definition: http_parser.h:131
http_data_cb on_header_value
Definition: http_parser.h:241
void http_parser_settings_init(http_parser_settings *settings)
Definition: http_parser.c:2149
#define HTTP_ERRNO_GEN(n, s)
Definition: http_parser.h:195
void * data
PUBLIC.
Definition: http_parser.h:232
http_errno
Definition: http_parser.h:196
int(* http_data_cb)(http_parser *, const char *at, size_t length)
Definition: http_parser.h:83
unsigned int upgrade
Definition: http_parser.h:229
const char * http_errno_name(enum http_errno err)
Definition: http_parser.c:2155
int http_should_keep_alive(const http_parser *parser)
Definition: http_parser.c:2112
const char * http_errno_description(enum http_errno err)
Definition: http_parser.c:2162
int http_body_is_final(const http_parser *parser)
struct http_parser_url::@1 field_data[UF_MAX]
unsigned int type
PRIVATE.
Definition: http_parser.h:208
http_parser_url_fields
Definition: http_parser.h:253
state
Definition: http_parser.c:279
http_cb on_message_complete
Definition: http_parser.h:244
void http_parser_init(http_parser *parser, enum http_parser_type type)
Definition: http_parser.c:2138
unsigned long http_parser_version(void)
Definition: http_parser.c:2426