UPnPsdk 0.1
Universal Plug and Play +, Software Development Kit
 
Loading...
Searching...
No Matches
httpparser.hpp
Go to the documentation of this file.
1#ifndef COMPA_GENLIB_NET_HTTP_HTTPPARSER_HPP
2#define COMPA_GENLIB_NET_HTTP_HTTPPARSER_HPP
3/*******************************************************************************
4 *
5 * Copyright (c) 2000-2003 Intel Corporation
6 * All rights reserved.
7 * Copyright (c) 2012 France Telecom All rights reserved.
8 * Copyright (C) 2021+ GPL 3 and higher by Ingo Höft, <Ingo@Hoeft-online.de>
9 * Redistribution only with this Copyright remark. Last modified: 2025-06-12
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 *
14 * - Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * - Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * - Neither name of Intel Corporation nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
31 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 ******************************************************************************/
36// Last verify with this project pupnp source file on 2023-07-20, ver 1.14.17
42#include <LinkedList.hpp>
43#include <membuffer.hpp>
44#include <upnp.hpp>
45#include <uri.hpp>
46
47#include <UPnPsdk/strintmap.hpp>
48
50#include <array>
52
53
58#define ENTREAD_DETERMINE_READ_METHOD 1
59#define ENTREAD_USING_CLEN 2
60#define ENTREAD_USING_CHUNKED 3
61#define ENTREAD_UNTIL_CLOSE 4
62#define ENTREAD_CHUNKY_BODY 5
63#define ENTREAD_CHUNKY_HEADERS 6
65
74
87
89// The enum values of the standard HTTP method should match those of
90// Upnp_HttpMethod enum defined in Compa/inc/API.hpp
92 HTTPMETHOD_PUT = UPNP_HTTPMETHOD_PUT,
93 HTTPMETHOD_DELETE = UPNP_HTTPMETHOD_DELETE,
94 HTTPMETHOD_GET = UPNP_HTTPMETHOD_GET,
95 HTTPMETHOD_HEAD = UPNP_HTTPMETHOD_HEAD,
96 HTTPMETHOD_POST = UPNP_HTTPMETHOD_POST,
97 HTTPMETHOD_MPOST,
98 HTTPMETHOD_SUBSCRIBE,
99 HTTPMETHOD_UNSUBSCRIBE,
100 HTTPMETHOD_NOTIFY,
101 HTTPMETHOD_MSEARCH,
102 HTTPMETHOD_UNKNOWN,
103 SOAPMETHOD_POST,
104 HTTPMETHOD_SIMPLEGET
105};
106
111#define HDR_UNKNOWN -1
112#define HDR_CACHE_CONTROL 1
113#define HDR_CALLBACK 2
114#define HDR_CONTENT_LENGTH 3
115#define HDR_CONTENT_TYPE 4
116#define HDR_DATE 5
117#define HDR_EXT 6
118#define HDR_HOST 7
119/*define HDR_IF_MODIFIED_SINCE 8 */
120/*define HDR_IF_UNMODIFIED_SINCE 9 */
121/*define HDR_LAST_MODIFIED 10 */
122#define HDR_LOCATION 11
123#define HDR_MAN 12
124#define HDR_MX 13
125#define HDR_NT 14
126#define HDR_NTS 15
127#define HDR_SERVER 16
128#define HDR_SEQ 17
129#define HDR_SID 18
130#define HDR_SOAPACTION 19
131#define HDR_ST 20
132#define HDR_TIMEOUT 21
133#define HDR_TRANSFER_ENCODING 22
134#define HDR_USN 23
135#define HDR_USER_AGENT 24
137
141#define HDR_ACCEPT 25
142#define HDR_ACCEPT_ENCODING 26
143#define HDR_ACCEPT_CHARSET 27
144#define HDR_ACCEPT_LANGUAGE 28
145#define HDR_ACCEPT_RANGE 29
146#define HDR_CONTENT_ENCODING 30
147#define HDR_CONTENT_LANGUAGE 31
148#define HDR_CONTENT_LOCATION 32
149#define HDR_CONTENT_RANGE 33
150#define HDR_IF_RANGE 34
151#define HDR_RANGE 35
152#define HDR_TE 36
154
156// This is prepared for binary search and must be sorted by header-name.
157inline constexpr std::array<const UPnPsdk::str_int_entry, 33> Http_Header_Names{
158 {{"ACCEPT", HDR_ACCEPT},
159 {"ACCEPT-CHARSET", HDR_ACCEPT_CHARSET},
160 {"ACCEPT-ENCODING", HDR_ACCEPT_ENCODING},
161 {"ACCEPT-LANGUAGE", HDR_ACCEPT_LANGUAGE},
162 {"ACCEPT-RANGES", HDR_ACCEPT_RANGE},
163 {"CACHE-CONTROL", HDR_CACHE_CONTROL},
164 {"CALLBACK", HDR_CALLBACK},
165 {"CONTENT-ENCODING", HDR_CONTENT_ENCODING},
166 {"CONTENT-LANGUAGE", HDR_CONTENT_LANGUAGE},
167 {"CONTENT-LENGTH", HDR_CONTENT_LENGTH},
168 {"CONTENT-LOCATION", HDR_CONTENT_LOCATION},
169 {"CONTENT-RANGE", HDR_CONTENT_RANGE},
170 {"CONTENT-TYPE", HDR_CONTENT_TYPE},
171 {"DATE", HDR_DATE},
172 {"EXT", HDR_EXT},
173 {"HOST", HDR_HOST},
174 {"IF-RANGE", HDR_IF_RANGE},
175 {"LOCATION", HDR_LOCATION},
176 {"MAN", HDR_MAN},
177 {"MX", HDR_MX},
178 {"NT", HDR_NT},
179 {"NTS", HDR_NTS},
180 {"RANGE", HDR_RANGE},
181 {"SEQ", HDR_SEQ},
182 {"SERVER", HDR_SERVER},
183 {"SID", HDR_SID},
184 {"SOAPACTION", HDR_SOAPACTION},
185 {"ST", HDR_ST},
186 {"TE", HDR_TE},
187 {"TIMEOUT", HDR_TIMEOUT},
188 {"TRANSFER-ENCODING", HDR_TRANSFER_ENCODING},
189 {"USER-AGENT", HDR_USER_AGENT},
190 {"USN", HDR_USN}}};
191
209
221
270
297
301void httpmsg_destroy( //
302 http_message_t* msg
303);
304
314 http_message_t* msg,
315 const char* header_name
316);
317
326 http_message_t* msg,
327 int header_name_id,
328 memptr* value
329);
330
334void parser_request_init( //
335 http_parser_t* parser
336);
337
342 http_parser_t* parser,
343 http_method_t request_method
344);
345
361 http_parser_t* parser
362);
363
364/* **********************************************************************
365 * Function: matchstr
366 *
367 * Parameters:
368 * IN char *str ; String to be matched
369 * IN size_t slen ; Length of the string
370 * IN const char* fmt ; Pattern format
371 * ...
372 *
373 * Description: Matches a variable parameter list with a string
374 * and takes actions based on the data type specified.
375 *
376 * Returns:
377 * PARSE_OK
378 * PARSE_NO_MATCH -- failure to match pattern 'fmt'
379 * PARSE_FAILURE -- 'str' is bad input
380 * PARSE_INCOMPLETE
381 ************************************************************************/
394 http_parser_t* parser
395);
396
397/* **********************************************************************
398 * Function: parser_parse_headers
399 *
400 * Parameters:
401 * INOUT http_parser_t* parser ; HTTP Parser object
402 *
403 * Description: Read HTTP header fields.
404 *
405 * Returns:
406 * PARSE_OK
407 * PARSE_SUCCESS
408 * PARSE_FAILURE
409 * PARSE_INCOMPLETE
410 * PARSE_NO_MATCH
411 ************************************************************************/
425);
426
427/* !
428 * \brief Read HTTP entity body.
429 *
430 * \returns
431 * On success: PARSE_SUCCESS - no more reading to do.\n
432 * On error:
433 * - PARSE_FAILURE
434 * - PARSE_NO_MATCH
435 * - PARSE_INCOMPLETE
436 * - PARSE_INCOMPLETE_ENTITY
437 */
451);
452
463 http_parser_t* parser
464);
465
479 const char* buf,
480 size_t buf_length
481);
482
493 char* str,
494 size_t slen,
495 const char* fmt,
496 ...
497);
498
504int raw_to_int( //
505 memptr* raw_value,
506 int base
507);
508
516int raw_find_str( //
517 memptr* raw_value,
518 const char* str
519);
520
528const char* method_to_str( //
529 http_method_t method
530);
531
537 std::string_view log_msg,
538 http_message_t* hmsg
539);
540
541#endif /* COMPA_GENLIB_NET_HTTP_HTTPPARSER_HPP */
parse_status_t parser_parse_headers(http_parser_t *parser)
Get HTTP Method, URL location and version information.
http_message_t msg
entire raw message
#define HDR_CONTENT_ENCODING
Header definition.
uri_type uri
Type of a uri, e.g. absolute, relative, etc.
#define HDR_CONTENT_LOCATION
Header definition.
constexpr std::array< const UPnPsdk::str_int_entry, 33 > Http_Header_Names
Assigns header-name id to its text representation.
http_header_t * httpmsg_find_hdr_str(http_message_t *msg, const char *header_name)
Compares the header name with the header names stored in the linked list of messages.
http_method_t method
Http method of an outgoing request.
int is_request
If 1, msg is a request, else response.
http_method_t
Method in a HTTP request.
memptr name
Header name as a string.
#define HDR_ACCEPT_ENCODING
Header definition.
#define HDR_CONTENT_LANGUAGE
Header definition.
int major_version
Http major version.
parse_status_t parser_parse(http_parser_t *parser)
The parser function.
membuffer status_msg
???
int entire_msg_loaded
Completeness of 'msg'.
#define HDR_TRANSFER_ENCODING
Type of a HTTP header.
memptr entity
message body(entity).
#define HDR_USN
Type of a HTTP header.
char * urlbuf
storage for url string.
#define HDR_ACCEPT_RANGE
Header definition.
void parser_request_init(http_parser_t *parser)
Initializes parser object for a request.
#define HDR_DATE
Type of a HTTP header.
#define HDR_NT
Type of a HTTP header.
#define HDR_MX
Type of a HTTP header.
#define HDR_NTS
Type of a HTTP header.
#define HDR_ACCEPT_LANGUAGE
Header definition.
#define HDR_EXT
Type of a HTTP header.
membuffer value
Raw-value; could be multi-lined; min-length = 0.
size_t amount_discarded
The amount of data that's been read by the user, that's no longer in the raw message buffer.
int raw_find_str(memptr *raw_value, const char *str)
Find a substring from raw character string buffer.
#define HDR_ACCEPT
Header definition.
#define HDR_SERVER
Type of a HTTP header.
membuffer msg
entire raw message.
int http_error_code
read-only; in case of parse error, this contains the HTTP error code (4XX or 5XX).
membuffer name_buf
(Private use – don't touch.)
http_header_t * httpmsg_find_hdr(http_message_t *msg, int header_name_id, memptr *value)
Finds header from a list, with the given 'name_id'.
#define HDR_ST
Type of a HTTP header.
#define HDR_HOST
Type of a HTTP header.
parse_status_t parser_parse_entity(http_parser_t *parser)
Determines method to read entity.
http_method_t request_method
Http method of an incoming response.
#define HDR_ACCEPT_CHARSET
Header definition.
scanner_t scanner
???
#define HDR_CACHE_CONTROL
Type of a HTTP header.
size_t chunk_size
Private data – don't touch.
#define HDR_CALLBACK
Type of a HTTP header.
parser_pos_t position
Private data – don't touch.
void parser_response_init(http_parser_t *parser, http_method_t request_method)
Initializes parser object for a response.
#define HDR_SEQ
Type of a HTTP header.
membuffer * msg
Raw http message.
#define HDR_LOCATION
Type of a HTTP header.
#define HDR_MAN
Type of a HTTP header.
parse_status_t parser_parse_responseline(http_parser_t *parser)
Get HTTP Method, URL location and version information.
parser_pos_t
Type of a parser position.
@ POS_COMPLETE
Position complete.
@ POS_RESPONSE_LINE
Position response line.
@ POS_ENTITY
Position entity.
@ POS_REQUEST_LINE
Position request line.
@ POS_HEADERS
Position headers.
#define HDR_USER_AGENT
Type of a HTTP header.
void httpmsg_destroy(http_message_t *msg)
Free memory allocated for the http message.
parse_status_t parser_get_entity_read_method(http_parser_t *parser)
Determines method to read entity.
parse_status_t
Status of parsing.
@ PARSE_INCOMPLETE
@ PARSE_NO_MATCH
@ PARSE_FAILURE
@ PARSE_INCOMPLETE_ENTITY
@ PARSE_SUCCESS
@ PARSE_OK
@ PARSE_CONTINUE_1
#define HDR_IF_RANGE
Header definition.
int initialized
Indicates if the object is initialized.
unsigned int content_length
Private data – don't touch.
int raw_to_int(memptr *raw_value, int base)
Converts raw character data to integer value.
int valid_ssdp_notify_hack
read-only; this is set to 1 if a NOTIFY request has no content-length. used to read valid sspd notify...
size_t cursor
Current position in buffer.
#define HDR_SOAPACTION
Type of a HTTP header.
#define HDR_CONTENT_TYPE
Type of a HTTP header.
int name_id
Header name id (for a selective group of headers only).
int ent_position
Private data – don't touch.
LinkedList headers
List of headers.
int minor_version
Http minor version.
parse_status_t parser_append(http_parser_t *parser, const char *buf, size_t buf_length)
Append date to HTTP parser, and do the parsing.
parse_status_t matchstr(char *str, size_t slen, const char *fmt,...)
Matches a variable parameter list with a string and takes actions based on the data type specified.
const char * method_to_str(http_method_t method)
A wrapper function that maps a method id to a method.
void print_http_headers(std::string_view log_msg, http_message_t *hmsg)
Print the HTTP headers.
size_t entity_start_position
Offset in the raw message buffer, which contains the message body. preceding this are the headers of ...
#define HDR_TE
Header definition.
#define HDR_TIMEOUT
Type of a HTTP header.
#define HDR_RANGE
Header definition.
#define HDR_CONTENT_LENGTH
Type of a HTTP header.
#define HDR_SID
Type of a HTTP header.
#define HDR_CONTENT_RANGE
Header definition.
Structure of an HTTP parser object.
Structure of a scanner object.
Structure of an HTTP message.
Structure of an HTTP header object.
Represents a URI used in parse_uri and elsewhere.
Definition uri.hpp:92
Manage a linked list (for internal use only).
Linked list (no protection).
C++ interface for the Uri module.
@ UPNP_HTTPMETHOD_DELETE
DELETE.
Definition API.hpp:1947
@ UPNP_HTTPMETHOD_GET
GET.
Definition API.hpp:1948
@ UPNP_HTTPMETHOD_HEAD
HEAD.
Definition API.hpp:1949
@ UPNP_HTTPMETHOD_PUT
PUT.
Definition API.hpp:1946
@ UPNP_HTTPMETHOD_POST
POST.
Definition API.hpp:1950
Manage blocks of dynamically allocated memory.
pointer to a chunk of memory.
Definition membuffer.hpp:50
Maintains a block of dynamically allocated memory.
Definition membuffer.hpp:61
String to integer and integer to string conversion functions.