forked from FreeRTOS/coreHTTP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore_http_client_private.h
More file actions
316 lines (281 loc) · 13.4 KB
/
core_http_client_private.h
File metadata and controls
316 lines (281 loc) · 13.4 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* coreHTTP
* Copyright (C) 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* @file core_http_client_private.h
* @brief Internal definitions to the HTTP Client library.
*/
#ifndef CORE_HTTP_CLIENT_PRIVATE_H_
#define CORE_HTTP_CLIENT_PRIVATE_H_
/**
* @cond DOXYGEN_IGNORE
* http-parser defaults this to 1, llhttp to 0.
*/
#ifndef LLHTTP_STRICT_MODE
#define LLHTTP_STRICT_MODE 0
#endif
/** @endcond */
/* Third-party llhttp include. */
#include "llhttp.h"
/* *INDENT-OFF* */
#ifdef __cplusplus
extern "C" {
#endif
/* *INDENT-ON* */
/**
* @brief The HTTP protocol version of this library is HTTP/1.1.
*/
#define HTTP_PROTOCOL_VERSION "HTTP/1.1"
#define HTTP_PROTOCOL_VERSION_LEN ( sizeof( HTTP_PROTOCOL_VERSION ) - 1U ) /**< The length of #HTTP_PROTOCOL_VERSION. */
/**
* @brief Default value when pRequestInfo->pPath == NULL.
*/
#define HTTP_EMPTY_PATH "/"
#define HTTP_EMPTY_PATH_LEN ( sizeof( HTTP_EMPTY_PATH ) - 1U ) /**< The length of #HTTP_EMPTY_PATH. */
/* Constants for HTTP header formatting. */
#define HTTP_HEADER_LINE_SEPARATOR "\r\n" /**< HTTP header field lines are separated by `\r\n`. */
#define HTTP_HEADER_LINE_SEPARATOR_LEN ( sizeof( HTTP_HEADER_LINE_SEPARATOR ) - 1U ) /**< The length of #HTTP_HEADER_LINE_SEPARATOR. */
#define HTTP_HEADER_END_INDICATOR "\r\n\r\n" /**< The HTTP header is complete when `\r\n\r\n` is found. */
#define HTTP_HEADER_END_INDICATOR_LEN ( sizeof( HTTP_HEADER_END_INDICATOR ) - 1U ) /**< The length of #HTTP_HEADER_END_INDICATOR. */
#define HTTP_HEADER_FIELD_SEPARATOR ": " /**< HTTP header field and values are separated by ": ". */
#define HTTP_HEADER_FIELD_SEPARATOR_LEN ( sizeof( HTTP_HEADER_FIELD_SEPARATOR ) - 1U ) /**< The length of #HTTP_HEADER_FIELD_SEPARATOR. */
#define SPACE_CHARACTER ' ' /**< A space character macro to help with serializing a request. */
#define SPACE_CHARACTER_LEN ( 1U ) /**< The length of #SPACE_CHARACTER. */
#define DASH_CHARACTER '-' /**< A dash character macro to help with serializing a request. */
#define DASH_CHARACTER_LEN ( 1U ) /**< The length of #DASH_CHARACTER. */
/* Constants for HTTP header copy checks. */
#define CARRIAGE_RETURN_CHARACTER '\r' /**< A carriage return character to help with header validation. */
#define LINEFEED_CHARACTER '\n' /**< A linefeed character to help with header validation. */
#define COLON_CHARACTER ':' /**< A colon character to help with header validation. */
/**
* @brief Indicator for function #httpHeaderStrncpy that the pSrc parameter is a
* header value.
*/
#define HTTP_HEADER_STRNCPY_IS_VALUE 0U
/**
* @brief Indicator for function #httpHeaderStrncpy that the pSrc parameter is a
* header field.
*/
#define HTTP_HEADER_STRNCPY_IS_FIELD 1U
/* Constants for header fields added automatically during the request
* initialization. */
#define HTTP_USER_AGENT_FIELD "User-Agent" /**< HTTP header field "User-Agent". */
#define HTTP_USER_AGENT_FIELD_LEN ( sizeof( HTTP_USER_AGENT_FIELD ) - 1U ) /**< The length of #HTTP_USER_AGENT_FIELD. */
#define HTTP_HOST_FIELD "Host" /**< HTTP header field "Host". */
#define HTTP_HOST_FIELD_LEN ( sizeof( HTTP_HOST_FIELD ) - 1U ) /**< The length of #HTTP_HOST_FIELD. */
#define HTTP_USER_AGENT_VALUE_LEN ( sizeof( HTTP_USER_AGENT_VALUE ) - 1U ) /**< The length of #HTTP_USER_AGENT_VALUE. */
/* Constants for header fields added based on flags. */
#define HTTP_CONNECTION_FIELD "Connection" /**< HTTP header field "Connection". */
#define HTTP_CONNECTION_FIELD_LEN ( sizeof( HTTP_CONNECTION_FIELD ) - 1U ) /**< The length of #HTTP_CONNECTION_FIELD. */
#define HTTP_CONTENT_LENGTH_FIELD "Content-Length" /**< HTTP header field "Content-Length". */
#define HTTP_CONTENT_LENGTH_FIELD_LEN ( sizeof( HTTP_CONTENT_LENGTH_FIELD ) - 1U ) /**< The length of #HTTP_CONTENT_LENGTH_FIELD. */
/* Constants for header values added based on flags. */
/* MISRA Ref 5.4.1 [Macro identifiers] */
/* More details at: https://github.com/FreeRTOS/coreHTTP/blob/main/MISRA.md#rule-54 */
/* coverity[other_declaration] */
#define HTTP_CONNECTION_KEEP_ALIVE_VALUE "keep-alive" /**< HTTP header value "keep-alive" for the "Connection" header field. */
/* MISRA Ref 5.4.2 [Macro identifiers] */
/* More details at: https://github.com/FreeRTOS/coreHTTP/blob/main/MISRA.md#rule-54 */
/* coverity[misra_c_2012_rule_5_4_violation] */
#define HTTP_CONNECTION_KEEP_ALIVE_VALUE_LEN ( sizeof( HTTP_CONNECTION_KEEP_ALIVE_VALUE ) - 1U ) /**< The length of #HTTP_CONNECTION_KEEP_ALIVE_VALUE. */
/* Constants relating to Range Requests. */
/* MISRA Ref 5.4.3 [Macro identifiers] */
/* More details at: https://github.com/FreeRTOS/coreHTTP/blob/main/MISRA.md#rule-54 */
/* coverity[other_declaration] */
#define HTTP_RANGE_REQUEST_HEADER_FIELD "Range" /**< HTTP header field "Range". */
/* MISRA Ref 5.4.4 [Macro identifiers] */
/* More details at: https://github.com/FreeRTOS/coreHTTP/blob/main/MISRA.md#rule-54 */
/* coverity[misra_c_2012_rule_5_4_violation] */
#define HTTP_RANGE_REQUEST_HEADER_FIELD_LEN ( sizeof( HTTP_RANGE_REQUEST_HEADER_FIELD ) - 1U ) /**< The length of #HTTP_RANGE_REQUEST_HEADER_FIELD. */
/* MISRA Ref 5.4.5 [Macro identifiers] */
/* More details at: https://github.com/FreeRTOS/coreHTTP/blob/main/MISRA.md#rule-54 */
/* coverity[other_declaration] */
#define HTTP_RANGE_REQUEST_HEADER_VALUE_PREFIX "bytes=" /**< HTTP required header value prefix when specifying a byte range for partial content. */
/* MISRA Ref 5.4.6 [Macro identifiers] */
/* More details at: https://github.com/FreeRTOS/coreHTTP/blob/main/MISRA.md#rule-54 */
/* coverity[misra_c_2012_rule_5_4_violation] */
#define HTTP_RANGE_REQUEST_HEADER_VALUE_PREFIX_LEN ( sizeof( HTTP_RANGE_REQUEST_HEADER_VALUE_PREFIX ) - 1U ) /**< The length of #HTTP_RANGE_REQUEST_HEADER_VALUE_PREFIX. */
/**
* @brief Maximum value of a 32 bit signed integer is 2,147,483,647.
*
* Used for calculating buffer space for ASCII representation of range values.
*/
#define MAX_INT32_NO_OF_DECIMAL_DIGITS 10U
/**
* @brief Maximum buffer space for storing a Range Request Value.
*
* The largest Range Request value is of the form:
* "bytes=<Max-Integer-Value>-<Max-Integer-Value>"
*/
#define HTTP_MAX_RANGE_REQUEST_VALUE_LEN \
( HTTP_RANGE_REQUEST_HEADER_VALUE_PREFIX_LEN + MAX_INT32_NO_OF_DECIMAL_DIGITS + \
1U /* Dash character '-' */ + MAX_INT32_NO_OF_DECIMAL_DIGITS )
/**
* @brief Return value for llhttp registered callback to signal
* continuation of HTTP response parsing. Equal to HPE_OK.
*/
#define LLHTTP_CONTINUE_PARSING 0
/**
* @brief Return value for llhttp registered callback to signal halting
* further execution.
*/
#define LLHTTP_STOP_PARSING HPE_USER
/**
* @brief Return value for llhttp registered callback to signal to pause
* further execution.
*/
#define LLHTTP_PAUSE_PARSING HPE_PAUSED
/**
* @brief Return value for llhttp_t.on_headers_complete to signal
* that the HTTP response has no body and to halt further execution.
*/
#define LLHTTP_STOP_PARSING_NO_BODY 1
/**
* @brief Return value for llhttp_t.on_headers_complete to signal
* halting further execution. This is the same return value that
* indicates the HTTP response has no body, but unlike the -1 error
* code, gives consistent return values for llhttp_execute in both
* strict and non-strict modes.
*/
#define LLHTTP_STOP_PARSING_NO_HEADER 1
/**
* @brief The minimum request-line in the headers has a possible one character
* custom method and a single forward / or asterisk * for the path:
*
* @code
* <1 character custom method> <1 character / or *> HTTP/1.x\r\n\r\n
* @endcode
*
* Therefore the minimum length is 16. If this minimum request-line is not
* satisfied, then the request headers to send are invalid.
*
* Note that custom methods are allowed per:
* https://tools.ietf.org/html/rfc2616#section-5.1.1.
*/
#define HTTP_MINIMUM_REQUEST_LINE_LENGTH 16u
/**
* @brief The state of the response message parsed after function
* #parseHttpResponse returns.
*/
typedef enum HTTPParsingState
{
HTTP_PARSING_NONE = 0, /**< The parser has not started reading any response. */
HTTP_PARSING_INCOMPLETE, /**< The parser found a partial response. */
HTTP_PARSING_COMPLETE /**< The parser found the entire response. */
} HTTPParsingState_t;
/**
* @brief An aggregator that represents the user-provided parameters to the
* #HTTPClient_ReadHeader API function. This will be used as context parameter
* for the parsing callbacks used by the API function.
*/
typedef struct findHeaderContext
{
const char * pField; /**< The field that is being searched for. */
size_t fieldLen; /**< The length of pField. */
const char ** pValueLoc; /**< The location of the value found in the buffer. */
size_t * pValueLen; /**< the length of the value found. */
uint8_t fieldFound; /**< Indicates that the header field was found during parsing. */
uint8_t valueFound; /**< Indicates that the header value was found during parsing. */
} findHeaderContext_t;
/**
* @brief The HTTP response parsing context for a response fresh from the
* server. This context is passed into the http-parser registered callbacks.
* The registered callbacks are private functions of the form
* httpParserXXXXCallbacks().
*
* The transitions of the httpParserXXXXCallback() functions are shown below.
* The XXXX is replaced by the strings in the state boxes:
*
* +---------------------+
* |onMessageBegin |
* +--------+------------+
* |
* |
* |
* v
* +--------+------------+
* |onStatus |
* +--------+------------+
* |
* |
* |
* v
* +--------+------------+
* |onStatusComplete |
* +--------+------------+
* |
* |
* |
* v
* +--------+------------+
* |onHeaderField +<---+
* +--------+------------+ |
* | |
* | |(More headers)
* | |
* v |
* +--------+------------+ |
* |onHeaderValue +----^
* +--------+------------+
* |
* |
* |
* v
* +--------+------------+
* |onHeadersComplete |
* +---------------------+
* |
* |
* |
* v
* +--------+------------+
* |onBody +<---+
* +--------+--------+---+ |
* | | |(Transfer-encoding chunked body)
* | | |
* | +--------+
* |
* v
* +--------+------------+
* |onMessageComplete |
* +---------------------+
*/
typedef struct HTTPParsingContext
{
llhttp_t llhttpParser; /**< Third-party llhttp context. */
llhttp_settings_t llhttpSettings; /**< Third-party parser settings. */
HTTPParsingState_t state; /**< The current state of the HTTP response parsed. */
HTTPResponse_t * pResponse; /**< HTTP response associated with this parsing context. */
uint8_t isHeadResponse; /**< HTTP response is for a HEAD request. */
const char * pBufferCur; /**< The current location of the parser in the response buffer. */
const char * pLastHeaderField; /**< Holds the last part of the header field parsed. */
size_t lastHeaderFieldLen; /**< The length of the last header field parsed. */
const char * pLastHeaderValue; /**< Holds the last part of the header value parsed. */
size_t lastHeaderValueLen; /**< The length of the last value field parsed. */
} HTTPParsingContext_t;
/* *INDENT-OFF* */
#ifdef __cplusplus
}
#endif
/* *INDENT-ON* */
#endif /* ifndef CORE_HTTP_CLIENT_PRIVATE_H_ */