Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
http_parser.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: MIT */
2
3/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23#ifndef ZEPHYR_INCLUDE_NET_HTTP_PARSER_H_
24#define ZEPHYR_INCLUDE_NET_HTTP_PARSER_H_
25
26/* Also update SONAME in the Makefile whenever you change these. */
27#define HTTP_PARSER_VERSION_MAJOR 2
28#define HTTP_PARSER_VERSION_MINOR 7
29#define HTTP_PARSER_VERSION_PATCH 1
30
31#include <sys/types.h>
32#if defined(_WIN32) && !defined(__MINGW32__) && \
33 (!defined(_MSC_VER) || _MSC_VER < 1600) && !defined(__WINE__)
34#include <BaseTsd.h>
35#include <stddef.h>
36typedef __int8 int8_t;
37typedef unsigned __int8 uint8_t;
38typedef __int16 int16_t;
39typedef unsigned __int16 uint16_t;
40typedef __int32 int32_t;
41typedef unsigned __int32 uint32_t;
42typedef __int64 int64_t;
43typedef unsigned __int64 uint64_t;
44#else
45#include <zephyr/types.h>
46#include <stddef.h>
47#endif
49#include <net/http_parser_url.h>
50
51#ifdef __cplusplus
52extern "C" {
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
66struct 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 * Returning `2` from on_headers_complete will tell parser that it should not
80 * expect neither a body nor any further responses on this connection. This is
81 * useful for handling responses to a CONNECT request which may not contain
82 * `Upgrade` or `Connection: upgrade` headers.
83 *
84 * http_data_cb does not return data chunks. It will be called arbitrarily
85 * many times for each string. E.G. you might get 10 callbacks for "on_url"
86 * each providing just a few characters more data.
87 */
88typedef int (*http_data_cb)(struct http_parser *, const char *at,
89 size_t length);
90typedef int (*http_cb)(struct http_parser *);
91
125 HTTP_UNLINK = 32
127
129
130/* Flag values for http_parser.flags field */
131enum flags {
132 F_CHUNKED = 1 << 0,
136 F_TRAILING = 1 << 4,
137 F_UPGRADE = 1 << 5,
138 F_SKIPBODY = 1 << 6,
139 F_CONTENTLENGTH = 1 << 7
141
177
178/* Get an http_errno value from an http_parser */
179#define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno)
180
181
184 unsigned int type : 2; /* enum http_parser_type */
185 unsigned int flags : 8; /* F_xxx values from 'flags' enum;
186 * semi-public
187 */
188 unsigned int state : 7; /* enum state from http_parser.c */
189 unsigned int header_state : 7; /* enum header_state from http_parser.c
190 */
191 unsigned int index : 7; /* index into current matcher */
192 unsigned int lenient_http_headers : 1;
193
194 uint32_t nread; /* # bytes read in various scenarios */
195 uint64_t content_length; /* # bytes in body (0 if no Content-Length
196 * header)
197 */
199 unsigned short http_major;
200 unsigned short http_minor;
201 unsigned int status_code : 16; /* responses only */
202 unsigned int method : 8; /* requests only */
203 unsigned int http_errno : 7;
204
205 /* 1 = Upgrade header was present and the parser has exited because of
206 * that.
207 * 0 = No upgrade header present.
208 * Should be checked when http_parser_execute() returns in addition to
209 * error checking.
210 */
211 unsigned int upgrade : 1;
212
214 void *data; /* A pointer to get hook to the "connection" or "socket"
215 * object
216 */
217
218 /* Remote socket address of http connection, where parser can initiate
219 * replies if necessary.
220 */
221 const struct sockaddr *addr;
222};
223
224
234 /* When on_chunk_header is called, the current chunk length is stored
235 * in parser->content_length.
236 */
239};
240
241
242/* Returns the library version. Bits 16-23 contain the major version number,
243 * bits 8-15 the minor version number and bits 0-7 the patch level.
244 * Usage example:
245 *
246 * unsigned long version = http_parser_version();
247 * unsigned major = (version >> 16) & 255;
248 * unsigned minor = (version >> 8) & 255;
249 * unsigned patch = version & 255;
250 * printf("http_parser v%u.%u.%u\n", major, minor, patch);
251 */
252unsigned long http_parser_version(void);
253
254void http_parser_init(struct http_parser *parser, enum http_parser_type type);
255
256
257/* Initialize http_parser_settings members to 0
258 */
260
261
262/* Executes the parser. Returns number of parsed bytes. Sets
263 * `parser->http_errno` on error.
264 */
265
266size_t http_parser_execute(struct http_parser *parser,
267 const struct http_parser_settings *settings,
268 const char *data, size_t len);
269
270/* If http_should_keep_alive() in the on_headers_complete or
271 * on_message_complete callback returns 0, then this should be
272 * the last message on the connection.
273 * If you are the server, respond with the "Connection: close" header.
274 * If you are the client, close the connection.
275 */
276int http_should_keep_alive(const struct http_parser *parser);
277
278/* Returns a string version of the HTTP method. */
279const char *http_method_str(enum http_method m);
280
281/* Return a string name of the given error */
282const char *http_errno_name(enum http_errno err);
283
284/* Return a string description of the given error */
285const char *http_errno_description(enum http_errno err);
286
287/* Pause or un-pause the parser; a nonzero value pauses */
288void http_parser_pause(struct http_parser *parser, int paused);
289
290/* Checks if this is the final chunk of the body. */
291int http_body_is_final(const struct http_parser *parser);
292
293#ifdef __cplusplus
294}
295#endif
296#endif
http_errno
Definition: http_parser.h:142
@ HPE_CB_chunk_complete
Definition: http_parser.h:153
@ HPE_INVALID_CONTENT_LENGTH
Definition: http_parser.h:168
@ HPE_INVALID_CONSTANT
Definition: http_parser.h:171
@ HPE_UNEXPECTED_CONTENT_LENGTH
Definition: http_parser.h:169
@ HPE_INVALID_CHUNK_SIZE
Definition: http_parser.h:170
@ HPE_UNKNOWN
Definition: http_parser.h:175
@ HPE_INVALID_QUERY_STRING
Definition: http_parser.h:164
@ HPE_INVALID_PORT
Definition: http_parser.h:162
@ HPE_INVALID_EOF_STATE
Definition: http_parser.h:154
@ HPE_HEADER_OVERFLOW
Definition: http_parser.h:155
@ HPE_INVALID_VERSION
Definition: http_parser.h:157
@ HPE_CB_message_begin
Definition: http_parser.h:144
@ HPE_CB_headers_complete
Definition: http_parser.h:148
@ HPE_CB_body
Definition: http_parser.h:149
@ HPE_INVALID_HOST
Definition: http_parser.h:161
@ HPE_CB_chunk_header
Definition: http_parser.h:152
@ HPE_INVALID_PATH
Definition: http_parser.h:163
@ HPE_STRICT
Definition: http_parser.h:173
@ HPE_CLOSED_CONNECTION
Definition: http_parser.h:156
@ HPE_CB_status
Definition: http_parser.h:151
@ HPE_INVALID_STATUS
Definition: http_parser.h:158
@ HPE_INVALID_INTERNAL_STATE
Definition: http_parser.h:172
@ HPE_INVALID_METHOD
Definition: http_parser.h:159
@ HPE_INVALID_HEADER_TOKEN
Definition: http_parser.h:167
@ HPE_CB_header_value
Definition: http_parser.h:147
@ HPE_INVALID_URL
Definition: http_parser.h:160
@ HPE_CB_header_field
Definition: http_parser.h:146
@ HPE_OK
Definition: http_parser.h:143
@ HPE_CB_message_complete
Definition: http_parser.h:150
@ HPE_CB_url
Definition: http_parser.h:145
@ HPE_PAUSED
Definition: http_parser.h:174
@ HPE_LF_EXPECTED
Definition: http_parser.h:166
@ HPE_INVALID_FRAGMENT
Definition: http_parser.h:165
int(* http_data_cb)(struct http_parser *, const char *at, size_t length)
Definition: http_parser.h:88
unsigned long http_parser_version(void)
int http_body_is_final(const struct http_parser *parser)
size_t http_parser_execute(struct http_parser *parser, const struct http_parser_settings *settings, const char *data, size_t len)
void http_parser_settings_init(struct http_parser_settings *settings)
const char * http_errno_description(enum http_errno err)
int http_should_keep_alive(const struct http_parser *parser)
int(* http_cb)(struct http_parser *)
Definition: http_parser.h:90
void http_parser_pause(struct http_parser *parser, int paused)
http_method
Definition: http_parser.h:92
@ HTTP_PROPPATCH
Definition: http_parser.h:106
@ HTTP_MSEARCH
Definition: http_parser.h:117
@ HTTP_PUT
Definition: http_parser.h:97
@ HTTP_MKACTIVITY
Definition: http_parser.h:114
@ HTTP_MKCALENDAR
Definition: http_parser.h:123
@ HTTP_BIND
Definition: http_parser.h:109
@ HTTP_PURGE
Definition: http_parser.h:122
@ HTTP_NOTIFY
Definition: http_parser.h:118
@ HTTP_PROPFIND
Definition: http_parser.h:105
@ HTTP_DELETE
Definition: http_parser.h:93
@ HTTP_MERGE
Definition: http_parser.h:116
@ HTTP_GET
Definition: http_parser.h:94
@ HTTP_REPORT
Definition: http_parser.h:113
@ HTTP_SUBSCRIBE
Definition: http_parser.h:119
@ HTTP_UNBIND
Definition: http_parser.h:111
@ HTTP_CONNECT
Definition: http_parser.h:98
@ HTTP_UNLOCK
Definition: http_parser.h:108
@ HTTP_MKCOL
Definition: http_parser.h:103
@ HTTP_OPTIONS
Definition: http_parser.h:99
@ HTTP_LINK
Definition: http_parser.h:124
@ HTTP_UNSUBSCRIBE
Definition: http_parser.h:120
@ HTTP_REBIND
Definition: http_parser.h:110
@ HTTP_LOCK
Definition: http_parser.h:102
@ HTTP_POST
Definition: http_parser.h:96
@ HTTP_HEAD
Definition: http_parser.h:95
@ HTTP_SEARCH
Definition: http_parser.h:107
@ HTTP_UNLINK
Definition: http_parser.h:125
@ HTTP_ACL
Definition: http_parser.h:112
@ HTTP_CHECKOUT
Definition: http_parser.h:115
@ HTTP_MOVE
Definition: http_parser.h:104
@ HTTP_COPY
Definition: http_parser.h:101
@ HTTP_PATCH
Definition: http_parser.h:121
@ HTTP_TRACE
Definition: http_parser.h:100
flags
Definition: http_parser.h:131
@ F_CHUNKED
Definition: http_parser.h:132
@ F_CONNECTION_CLOSE
Definition: http_parser.h:134
@ F_CONNECTION_KEEP_ALIVE
Definition: http_parser.h:133
@ F_UPGRADE
Definition: http_parser.h:137
@ F_CONTENTLENGTH
Definition: http_parser.h:139
@ F_TRAILING
Definition: http_parser.h:136
@ F_SKIPBODY
Definition: http_parser.h:138
@ F_CONNECTION_UPGRADE
Definition: http_parser.h:135
void http_parser_init(struct http_parser *parser, enum http_parser_type type)
const char * http_method_str(enum http_method m)
const char * http_errno_name(enum http_errno err)
http_parser_type
Definition: http_parser.h:128
@ HTTP_RESPONSE
Definition: http_parser.h:128
@ HTTP_REQUEST
Definition: http_parser.h:128
@ HTTP_BOTH
Definition: http_parser.h:128
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
__INT32_TYPE__ int32_t
Definition: stdint.h:44
__UINT64_TYPE__ uint64_t
Definition: stdint.h:61
__UINT8_TYPE__ uint8_t
Definition: stdint.h:58
__UINT16_TYPE__ uint16_t
Definition: stdint.h:59
__INT64_TYPE__ int64_t
Definition: stdint.h:45
__INT8_TYPE__ int8_t
Definition: stdint.h:42
__INT16_TYPE__ int16_t
Definition: stdint.h:43
Definition: http_parser.h:225
http_data_cb on_header_value
Definition: http_parser.h:230
http_cb on_chunk_header
Definition: http_parser.h:237
http_data_cb on_status
Definition: http_parser.h:228
http_cb on_headers_complete
Definition: http_parser.h:231
http_data_cb on_url
Definition: http_parser.h:227
http_data_cb on_body
Definition: http_parser.h:232
http_cb on_chunk_complete
Definition: http_parser.h:238
http_cb on_message_begin
Definition: http_parser.h:226
http_data_cb on_header_field
Definition: http_parser.h:229
http_cb on_message_complete
Definition: http_parser.h:233
Definition: http_parser.h:182
unsigned int flags
Definition: http_parser.h:185
unsigned int state
Definition: http_parser.h:188
unsigned int index
Definition: http_parser.h:191
unsigned int upgrade
Definition: http_parser.h:211
uint32_t nread
Definition: http_parser.h:194
unsigned int method
Definition: http_parser.h:202
void * data
Definition: http_parser.h:214
uint64_t content_length
Definition: http_parser.h:195
unsigned int status_code
Definition: http_parser.h:201
unsigned int http_errno
Definition: http_parser.h:203
unsigned int header_state
Definition: http_parser.h:189
unsigned int type
Definition: http_parser.h:184
unsigned short http_major
Definition: http_parser.h:199
unsigned int lenient_http_headers
Definition: http_parser.h:192
unsigned short http_minor
Definition: http_parser.h:200
const struct sockaddr * addr
Definition: http_parser.h:221
Definition: net_ip.h:335
static fdata_t data[2]
Definition: test_fifo_contexts.c:15