Zephyr API Documentation 4.0.0-rc3
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
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
51
52#ifdef __cplusplus
53extern "C" {
54#endif
55
56/* Maximum header size allowed. If the macro is not defined
57 * before including this header then the default is used. To
58 * change the maximum header size, define the macro in the build
59 * environment (e.g. -DHTTP_MAX_HEADER_SIZE=<value>). To remove
60 * the effective limit on the size of the header, define the macro
61 * to a very large number (e.g. -DHTTP_MAX_HEADER_SIZE=0x7fffffff)
62 */
63#ifndef HTTP_MAX_HEADER_SIZE
64# define HTTP_MAX_HEADER_SIZE (80 * 1024)
65#endif
66
67struct http_parser;
69
70
71/* Callbacks should return non-zero to indicate an error. The parser will
72 * then halt execution.
73 *
74 * The one exception is on_headers_complete. In a HTTP_RESPONSE parser
75 * returning '1' from on_headers_complete will tell the parser that it
76 * should not expect a body. This is used when receiving a response to a
77 * HEAD request which may contain 'Content-Length' or 'Transfer-Encoding:
78 * chunked' headers that indicate the presence of a body.
79 *
80 * Returning `2` from on_headers_complete will tell parser that it should not
81 * expect neither a body nor any further responses on this connection. This is
82 * useful for handling responses to a CONNECT request which may not contain
83 * `Upgrade` or `Connection: upgrade` headers.
84 *
85 * http_data_cb does not return data chunks. It will be called arbitrarily
86 * many times for each string. E.G. you might get 10 callbacks for "on_url"
87 * each providing just a few characters more data.
88 */
89typedef int (*http_data_cb)(struct http_parser *, const char *at,
90 size_t length);
91typedef int (*http_cb)(struct http_parser *);
92
94
95/* Flag values for http_parser.flags field */
96enum flags {
97 F_CHUNKED = 1 << 0,
101 F_TRAILING = 1 << 4,
102 F_UPGRADE = 1 << 5,
103 F_SKIPBODY = 1 << 6,
104 F_CONTENTLENGTH = 1 << 7
106
142
143/* Get an http_errno value from an http_parser */
144#define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno)
145
146
149 unsigned int type : 2; /* enum http_parser_type */
150 unsigned int flags : 8; /* F_xxx values from 'flags' enum;
151 * semi-public
152 */
153 unsigned int state : 7; /* enum state from http_parser.c */
154 unsigned int header_state : 7; /* enum header_state from http_parser.c
155 */
156 unsigned int index : 7; /* index into current matcher */
157 unsigned int lenient_http_headers : 1;
158
159 uint32_t nread; /* # bytes read in various scenarios */
160 uint64_t content_length; /* # bytes in body (0 if no Content-Length
161 * header)
162 */
164 unsigned short http_major;
165 unsigned short http_minor;
166 unsigned int status_code : 16; /* responses only */
167 unsigned int method : 8; /* requests only */
168 unsigned int http_errno : 7;
169
170 /* 1 = Upgrade header was present and the parser has exited because of
171 * that.
172 * 0 = No upgrade header present.
173 * Should be checked when http_parser_execute() returns in addition to
174 * error checking.
175 */
176 unsigned int upgrade : 1;
177
179 void *data; /* A pointer to get hook to the "connection" or "socket"
180 * object
181 */
182
183 /* Remote socket address of http connection, where parser can initiate
184 * replies if necessary.
185 */
186 const struct sockaddr *addr;
187};
188
189
205
206
207/* Returns the library version. Bits 16-23 contain the major version number,
208 * bits 8-15 the minor version number and bits 0-7 the patch level.
209 * Usage example:
210 *
211 * unsigned long version = http_parser_version();
212 * unsigned major = (version >> 16) & 255;
213 * unsigned minor = (version >> 8) & 255;
214 * unsigned patch = version & 255;
215 * printf("http_parser v%u.%u.%u\n", major, minor, patch);
216 */
217unsigned long http_parser_version(void);
218
219void http_parser_init(struct http_parser *parser, enum http_parser_type type);
220
221
222/* Initialize http_parser_settings members to 0
223 */
225
226
227/* Executes the parser. Returns number of parsed bytes. Sets
228 * `parser->http_errno` on error.
229 */
230
231size_t http_parser_execute(struct http_parser *parser,
232 const struct http_parser_settings *settings,
233 const char *data, size_t len);
234
235/* If http_should_keep_alive() in the on_headers_complete or
236 * on_message_complete callback returns 0, then this should be
237 * the last message on the connection.
238 * If you are the server, respond with the "Connection: close" header.
239 * If you are the client, close the connection.
240 */
241int http_should_keep_alive(const struct http_parser *parser);
242
243/* Returns a string version of the HTTP method. */
244const char *http_method_str(enum http_method m);
245
246/* Return a string name of the given error */
247const char *http_errno_name(enum http_errno err);
248
249/* Return a string description of the given error */
250const char *http_errno_description(enum http_errno err);
251
252/* Pause or un-pause the parser; a nonzero value pauses */
253void http_parser_pause(struct http_parser *parser, int paused);
254
255/* Checks if this is the final chunk of the body. */
256int http_body_is_final(const struct http_parser *parser);
257
258#ifdef __cplusplus
259}
260#endif
261#endif
http_method
HTTP Request Methods.
Definition method.h:28
HTTP request methods.
http_errno
Definition parser.h:107
@ HPE_CB_chunk_complete
Definition parser.h:118
@ HPE_INVALID_CONTENT_LENGTH
Definition parser.h:133
@ HPE_INVALID_CONSTANT
Definition parser.h:136
@ HPE_UNEXPECTED_CONTENT_LENGTH
Definition parser.h:134
@ HPE_INVALID_CHUNK_SIZE
Definition parser.h:135
@ HPE_UNKNOWN
Definition parser.h:140
@ HPE_INVALID_QUERY_STRING
Definition parser.h:129
@ HPE_INVALID_PORT
Definition parser.h:127
@ HPE_INVALID_EOF_STATE
Definition parser.h:119
@ HPE_HEADER_OVERFLOW
Definition parser.h:120
@ HPE_INVALID_VERSION
Definition parser.h:122
@ HPE_CB_message_begin
Definition parser.h:109
@ HPE_CB_headers_complete
Definition parser.h:113
@ HPE_CB_body
Definition parser.h:114
@ HPE_INVALID_HOST
Definition parser.h:126
@ HPE_CB_chunk_header
Definition parser.h:117
@ HPE_INVALID_PATH
Definition parser.h:128
@ HPE_STRICT
Definition parser.h:138
@ HPE_CLOSED_CONNECTION
Definition parser.h:121
@ HPE_CB_status
Definition parser.h:116
@ HPE_INVALID_STATUS
Definition parser.h:123
@ HPE_INVALID_INTERNAL_STATE
Definition parser.h:137
@ HPE_INVALID_METHOD
Definition parser.h:124
@ HPE_INVALID_HEADER_TOKEN
Definition parser.h:132
@ HPE_CB_header_value
Definition parser.h:112
@ HPE_INVALID_URL
Definition parser.h:125
@ HPE_CB_header_field
Definition parser.h:111
@ HPE_OK
Definition parser.h:108
@ HPE_CB_message_complete
Definition parser.h:115
@ HPE_CB_url
Definition parser.h:110
@ HPE_PAUSED
Definition parser.h:139
@ HPE_LF_EXPECTED
Definition parser.h:131
@ HPE_INVALID_FRAGMENT
Definition parser.h:130
int(* http_data_cb)(struct http_parser *, const char *at, size_t length)
Definition parser.h:89
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 parser.h:91
void http_parser_pause(struct http_parser *parser, int paused)
flags
Definition parser.h:96
@ F_CHUNKED
Definition parser.h:97
@ F_CONNECTION_CLOSE
Definition parser.h:99
@ F_CONNECTION_KEEP_ALIVE
Definition parser.h:98
@ F_UPGRADE
Definition parser.h:102
@ F_CONTENTLENGTH
Definition parser.h:104
@ F_TRAILING
Definition parser.h:101
@ F_SKIPBODY
Definition parser.h:103
@ F_CONNECTION_UPGRADE
Definition parser.h:100
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 parser.h:93
@ HTTP_RESPONSE
Definition parser.h:93
@ HTTP_REQUEST
Definition parser.h:93
@ HTTP_BOTH
Definition parser.h:93
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__INT32_TYPE__ int32_t
Definition stdint.h:74
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
__INT64_TYPE__ int64_t
Definition stdint.h:75
__INT8_TYPE__ int8_t
Definition stdint.h:72
__INT16_TYPE__ int16_t
Definition stdint.h:73
Definition parser.h:190
http_data_cb on_header_value
Definition parser.h:195
http_cb on_chunk_header
Definition parser.h:202
http_data_cb on_status
Definition parser.h:193
http_cb on_headers_complete
Definition parser.h:196
http_data_cb on_url
Definition parser.h:192
http_data_cb on_body
Definition parser.h:197
http_cb on_chunk_complete
Definition parser.h:203
http_cb on_message_begin
Definition parser.h:191
http_data_cb on_header_field
Definition parser.h:194
http_cb on_message_complete
Definition parser.h:198
Definition parser.h:147
unsigned int flags
Definition parser.h:150
unsigned int state
Definition parser.h:153
unsigned int index
Definition parser.h:156
unsigned int upgrade
Definition parser.h:176
uint32_t nread
Definition parser.h:159
unsigned int method
Definition parser.h:167
void * data
PUBLIC.
Definition parser.h:179
uint64_t content_length
Definition parser.h:160
unsigned int status_code
Definition parser.h:166
unsigned int http_errno
Definition parser.h:168
unsigned int header_state
Definition parser.h:154
unsigned int type
PRIVATE.
Definition parser.h:149
unsigned short http_major
READ-ONLY.
Definition parser.h:164
unsigned int lenient_http_headers
Definition parser.h:157
unsigned short http_minor
Definition parser.h:165
const struct sockaddr * addr
Definition parser.h:186
Generic sockaddr struct.
Definition net_ip.h:388