UPnPsdk 0.1
Universal Plug and Play +, Software Development Kit
 
Loading...
Searching...
No Matches
httpparser.hpp File Reference

Functions to parse UPnP messages like requests and responses. More...

#include <LinkedList.hpp>
#include <membuffer.hpp>
#include <upnp.hpp>
#include <uri.hpp>
#include <UPnPsdk/strintmap.hpp>
+ Include dependency graph for httpparser.hpp:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  scanner_t
 Structure of a scanner object. More...
 
struct  http_header_t
 Structure of an HTTP header object. More...
 
struct  http_message_t
 Structure of an HTTP message. More...
 
struct  http_parser_t
 Structure of an HTTP parser object. More...
 

Macros

Constants that define the read method.
#define ENTREAD_DETERMINE_READ_METHOD   1
 Defines read method.
 
#define ENTREAD_USING_CLEN   2
 Defines read method.
 
#define ENTREAD_USING_CHUNKED   3
 Defines read method.
 
#define ENTREAD_UNTIL_CLOSE   4
 Defines read method.
 
#define ENTREAD_CHUNKY_BODY   5
 Defines read method.
 
#define ENTREAD_CHUNKY_HEADERS   6
 Defines read method.
 
Different types of HTTP headers.
#define HDR_UNKNOWN   -1
 Type of a HTTP header.
 
#define HDR_CACHE_CONTROL   1
 Type of a HTTP header.
 
#define HDR_CALLBACK   2
 Type of a HTTP header.
 
#define HDR_CONTENT_LENGTH   3
 Type of a HTTP header.
 
#define HDR_CONTENT_TYPE   4
 Type of a HTTP header.
 
#define HDR_DATE   5
 Type of a HTTP header.
 
#define HDR_EXT   6
 Type of a HTTP header.
 
#define HDR_HOST   7
 Type of a HTTP header.
 
#define HDR_LOCATION   11
 Type of a HTTP header.
 
#define HDR_MAN   12
 Type of a HTTP header.
 
#define HDR_MX   13
 Type of a HTTP header.
 
#define HDR_NT   14
 Type of a HTTP header.
 
#define HDR_NTS   15
 Type of a HTTP header.
 
#define HDR_SERVER   16
 Type of a HTTP header.
 
#define HDR_SEQ   17
 Type of a HTTP header.
 
#define HDR_SID   18
 Type of a HTTP header.
 
#define HDR_SOAPACTION   19
 Type of a HTTP header.
 
#define HDR_ST   20
 Type of a HTTP header.
 
#define HDR_TIMEOUT   21
 Type of a HTTP header.
 
#define HDR_TRANSFER_ENCODING   22
 Type of a HTTP header.
 
#define HDR_USN   23
 Type of a HTTP header.
 
#define HDR_USER_AGENT   24
 Type of a HTTP header.
 
Adding new header definitions
#define HDR_ACCEPT   25
 Header definition.
 
#define HDR_ACCEPT_ENCODING   26
 Header definition.
 
#define HDR_ACCEPT_CHARSET   27
 Header definition.
 
#define HDR_ACCEPT_LANGUAGE   28
 Header definition.
 
#define HDR_ACCEPT_RANGE   29
 Header definition.
 
#define HDR_CONTENT_ENCODING   30
 Header definition.
 
#define HDR_CONTENT_LANGUAGE   31
 Header definition.
 
#define HDR_CONTENT_LOCATION   32
 Header definition.
 
#define HDR_CONTENT_RANGE   33
 Header definition.
 
#define HDR_IF_RANGE   34
 Header definition.
 
#define HDR_RANGE   35
 Header definition.
 
#define HDR_TE   36
 Header definition.
 

Enumerations

enum  parser_pos_t {
  POS_REQUEST_LINE , POS_RESPONSE_LINE , POS_HEADERS , POS_ENTITY ,
  POS_COMPLETE
}
 Type of a parser position. More...
 
enum  http_method_t {
  HTTPMETHOD_PUT = UPNP_HTTPMETHOD_PUT , HTTPMETHOD_DELETE = UPNP_HTTPMETHOD_DELETE , HTTPMETHOD_GET = UPNP_HTTPMETHOD_GET , HTTPMETHOD_HEAD = UPNP_HTTPMETHOD_HEAD ,
  HTTPMETHOD_POST = UPNP_HTTPMETHOD_POST , HTTPMETHOD_MPOST , HTTPMETHOD_SUBSCRIBE , HTTPMETHOD_UNSUBSCRIBE ,
  HTTPMETHOD_NOTIFY , HTTPMETHOD_MSEARCH , HTTPMETHOD_UNKNOWN , SOAPMETHOD_POST ,
  HTTPMETHOD_SIMPLEGET
}
 Method in a HTTP request. More...
 
enum  parse_status_t {
  PARSE_SUCCESS = 0 , PARSE_INCOMPLETE , PARSE_INCOMPLETE_ENTITY , PARSE_FAILURE ,
  PARSE_OK , PARSE_NO_MATCH , PARSE_CONTINUE_1
}
 Status of parsing. More...
 

Functions

void httpmsg_destroy (http_message_t *msg)
 Free memory allocated for the http message.
 
http_header_thttpmsg_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_header_thttpmsg_find_hdr (http_message_t *msg, int header_name_id, memptr *value)
 Finds header from a list, with the given 'name_id'.
 
void parser_request_init (http_parser_t *parser)
 Initializes parser object for a request.
 
void parser_response_init (http_parser_t *parser, http_method_t request_method)
 Initializes parser object for a response.
 
parse_status_t parser_parse (http_parser_t *parser)
 The parser function.
 
parse_status_t parser_parse_responseline (http_parser_t *parser)
 Get HTTP Method, URL location and version information.
 
parse_status_t parser_parse_headers (http_parser_t *parser)
 Get HTTP Method, URL location and version information.
 
parse_status_t parser_parse_entity (http_parser_t *parser)
 Determines method to read entity.
 
parse_status_t parser_get_entity_read_method (http_parser_t *parser)
 Determines method to read entity.
 
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.
 
int raw_to_int (memptr *raw_value, int base)
 Converts raw character data to integer value.
 
int raw_find_str (memptr *raw_value, const char *str)
 Find a substring from raw character string buffer.
 
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.
 

Variables

constexpr std::array< const UPnPsdk::str_int_entry, 33 > Http_Header_Names
 Assigns header-name id to its text representation.
 

Detailed Description

Functions to parse UPnP messages like requests and responses.

Definition in file httpparser.hpp.


Class Documentation

◆ scanner_t

struct scanner_t

Structure of a scanner object.

Definition at line 76 of file httpparser.hpp.

+ Collaboration diagram for scanner_t:
Class Members
membuffer * msg Raw http message.
size_t cursor Current position in buffer.
int entire_msg_loaded Completeness of 'msg'.
  • Set to 1 if the entire message is loaded in 'msg'.
  • Set to 0 if only a partial message is in 'msg'.
  • Default is 0.

◆ http_header_t

struct http_header_t

Structure of an HTTP header object.

Definition at line 211 of file httpparser.hpp.

+ Collaboration diagram for http_header_t:
Class Members
memptr name Header name as a string.
int name_id Header name id (for a selective group of headers only).
membuffer value Raw-value; could be multi-lined; min-length = 0.
membuffer name_buf (Private use – don't touch.)

◆ http_message_t

struct http_message_t

Structure of an HTTP message.

Definition at line 223 of file httpparser.hpp.

+ Collaboration diagram for http_message_t:
Class Members
int initialized Indicates if the object is initialized.
http_method_t method Http method of an outgoing request.
uri_type uri Type of a uri, e.g. absolute, relative, etc.
http_method_t request_method Http method of an incoming response.
int status_code ???
membuffer status_msg ???
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 is_request If 1, msg is a request, else response.
int major_version Http major version.
int minor_version Http minor version.
LinkedList headers List of headers.
memptr entity message body(entity).
membuffer msg entire raw message.
char * urlbuf storage for url string.

◆ http_parser_t

struct http_parser_t

Structure of an HTTP parser object.

Definition at line 272 of file httpparser.hpp.

+ Collaboration diagram for http_parser_t:
Class Members
http_message_t msg entire raw message

This contains the complete HTTP message with preceding header and message body. There is also the http_parser_t::entity_start_position available that points to the message body in the message.

int http_error_code read-only; in case of parse error, this contains the HTTP error code (4XX or 5XX).
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 msg.
parser_pos_t position Private data – don't touch.
int ent_position Private data – don't touch.
unsigned int content_length Private data – don't touch.
size_t chunk_size Private data – don't touch.
size_t entity_start_position Offset in the raw message buffer, which contains the message body. preceding this are the headers of the message.
scanner_t scanner ???

Macro Definition Documentation

◆ ENTREAD_DETERMINE_READ_METHOD

#define ENTREAD_DETERMINE_READ_METHOD   1

Defines read method.

Definition at line 58 of file httpparser.hpp.

◆ ENTREAD_USING_CLEN

#define ENTREAD_USING_CLEN   2

Defines read method.

Definition at line 59 of file httpparser.hpp.

◆ ENTREAD_USING_CHUNKED

#define ENTREAD_USING_CHUNKED   3

Defines read method.

Definition at line 60 of file httpparser.hpp.

◆ ENTREAD_UNTIL_CLOSE

#define ENTREAD_UNTIL_CLOSE   4

Defines read method.

Definition at line 61 of file httpparser.hpp.

◆ ENTREAD_CHUNKY_BODY

#define ENTREAD_CHUNKY_BODY   5

Defines read method.

Definition at line 62 of file httpparser.hpp.

◆ ENTREAD_CHUNKY_HEADERS

#define ENTREAD_CHUNKY_HEADERS   6

Defines read method.

Definition at line 63 of file httpparser.hpp.

◆ HDR_UNKNOWN

#define HDR_UNKNOWN   -1

Type of a HTTP header.

Definition at line 111 of file httpparser.hpp.

◆ HDR_CACHE_CONTROL

#define HDR_CACHE_CONTROL   1

Type of a HTTP header.

Definition at line 112 of file httpparser.hpp.

◆ HDR_CALLBACK

#define HDR_CALLBACK   2

Type of a HTTP header.

Definition at line 113 of file httpparser.hpp.

◆ HDR_CONTENT_LENGTH

#define HDR_CONTENT_LENGTH   3

Type of a HTTP header.

Definition at line 114 of file httpparser.hpp.

◆ HDR_CONTENT_TYPE

#define HDR_CONTENT_TYPE   4

Type of a HTTP header.

Definition at line 115 of file httpparser.hpp.

◆ HDR_DATE

#define HDR_DATE   5

Type of a HTTP header.

Definition at line 116 of file httpparser.hpp.

◆ HDR_EXT

#define HDR_EXT   6

Type of a HTTP header.

Definition at line 117 of file httpparser.hpp.

◆ HDR_HOST

#define HDR_HOST   7

Type of a HTTP header.

Definition at line 118 of file httpparser.hpp.

◆ HDR_LOCATION

#define HDR_LOCATION   11

Type of a HTTP header.

Definition at line 122 of file httpparser.hpp.

◆ HDR_MAN

#define HDR_MAN   12

Type of a HTTP header.

Definition at line 123 of file httpparser.hpp.

◆ HDR_MX

#define HDR_MX   13

Type of a HTTP header.

Definition at line 124 of file httpparser.hpp.

◆ HDR_NT

#define HDR_NT   14

Type of a HTTP header.

Definition at line 125 of file httpparser.hpp.

◆ HDR_NTS

#define HDR_NTS   15

Type of a HTTP header.

Definition at line 126 of file httpparser.hpp.

◆ HDR_SERVER

#define HDR_SERVER   16

Type of a HTTP header.

Definition at line 127 of file httpparser.hpp.

◆ HDR_SEQ

#define HDR_SEQ   17

Type of a HTTP header.

Definition at line 128 of file httpparser.hpp.

◆ HDR_SID

#define HDR_SID   18

Type of a HTTP header.

Definition at line 129 of file httpparser.hpp.

◆ HDR_SOAPACTION

#define HDR_SOAPACTION   19

Type of a HTTP header.

Definition at line 130 of file httpparser.hpp.

◆ HDR_ST

#define HDR_ST   20

Type of a HTTP header.

Definition at line 131 of file httpparser.hpp.

◆ HDR_TIMEOUT

#define HDR_TIMEOUT   21

Type of a HTTP header.

Definition at line 132 of file httpparser.hpp.

◆ HDR_TRANSFER_ENCODING

#define HDR_TRANSFER_ENCODING   22

Type of a HTTP header.

Definition at line 133 of file httpparser.hpp.

◆ HDR_USN

#define HDR_USN   23

Type of a HTTP header.

Definition at line 134 of file httpparser.hpp.

◆ HDR_USER_AGENT

#define HDR_USER_AGENT   24

Type of a HTTP header.

Definition at line 135 of file httpparser.hpp.

◆ HDR_ACCEPT

#define HDR_ACCEPT   25

Header definition.

Definition at line 141 of file httpparser.hpp.

◆ HDR_ACCEPT_ENCODING

#define HDR_ACCEPT_ENCODING   26

Header definition.

Definition at line 142 of file httpparser.hpp.

◆ HDR_ACCEPT_CHARSET

#define HDR_ACCEPT_CHARSET   27

Header definition.

Definition at line 143 of file httpparser.hpp.

◆ HDR_ACCEPT_LANGUAGE

#define HDR_ACCEPT_LANGUAGE   28

Header definition.

Definition at line 144 of file httpparser.hpp.

◆ HDR_ACCEPT_RANGE

#define HDR_ACCEPT_RANGE   29

Header definition.

Definition at line 145 of file httpparser.hpp.

◆ HDR_CONTENT_ENCODING

#define HDR_CONTENT_ENCODING   30

Header definition.

Definition at line 146 of file httpparser.hpp.

◆ HDR_CONTENT_LANGUAGE

#define HDR_CONTENT_LANGUAGE   31

Header definition.

Definition at line 147 of file httpparser.hpp.

◆ HDR_CONTENT_LOCATION

#define HDR_CONTENT_LOCATION   32

Header definition.

Definition at line 148 of file httpparser.hpp.

◆ HDR_CONTENT_RANGE

#define HDR_CONTENT_RANGE   33

Header definition.

Definition at line 149 of file httpparser.hpp.

◆ HDR_IF_RANGE

#define HDR_IF_RANGE   34

Header definition.

Definition at line 150 of file httpparser.hpp.

◆ HDR_RANGE

#define HDR_RANGE   35

Header definition.

Definition at line 151 of file httpparser.hpp.

◆ HDR_TE

#define HDR_TE   36

Header definition.

Definition at line 152 of file httpparser.hpp.

Enumeration Type Documentation

◆ parser_pos_t

Type of a parser position.

Enumerator
POS_REQUEST_LINE 

Position request line.

POS_RESPONSE_LINE 

Position response line.

POS_HEADERS 

Position headers.

POS_ENTITY 

Position entity.

POS_COMPLETE 

Position complete.

Definition at line 67 of file httpparser.hpp.

◆ http_method_t

Method in a HTTP request.

Definition at line 91 of file httpparser.hpp.

◆ parse_status_t

Status of parsing.

Enumerator
PARSE_SUCCESS 

msg was parsed successfully.

PARSE_INCOMPLETE 

need more data to continue.

PARSE_INCOMPLETE_ENTITY 

for responses that don't have length specified.

PARSE_FAILURE 

parse failed; check status code for details.

PARSE_OK 

done partial.

PARSE_NO_MATCH 

token not matched.

PARSE_CONTINUE_1 

private.

Definition at line 193 of file httpparser.hpp.

Function Documentation

◆ httpmsg_destroy()

void httpmsg_destroy ( http_message_t msg)

Free memory allocated for the http message.

Parameters
[in,out]msgHTTP Message Object.

Definition at line 1273 of file httpparser.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ httpmsg_find_hdr_str()

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.

Returns
  • Pointer to a header on success
  • nullptr on failure
Parameters
[in]msgHTTP Message Object.
[in]header_nameHeader name to be compared with.

Definition at line 1286 of file httpparser.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ httpmsg_find_hdr()

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'.

Returns
  • Pointer to a header on success
  • nullptr on failure
Parameters
[in]msgHTTP Message Object.
[in]header_name_idHeader Name ID to be compared with.
[out]valueBuffer to get the ouput to.

Definition at line 1307 of file httpparser.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parser_request_init()

void parser_request_init ( http_parser_t parser)

Initializes parser object for a request.

Parameters
[out]parserHTTP Parser Object.

Definition at line 1679 of file httpparser.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parser_response_init()

void parser_response_init ( http_parser_t parser,
http_method_t  request_method 
)

Initializes parser object for a response.

Parameters
[out]parserHTTP Parser object.
[in]request_methodRequest method.

Definition at line 1686 of file httpparser.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parser_parse()

parse_status_t parser_parse ( http_parser_t parser)

The parser function.

Depending on the position of the parser object the actual parsing function is invoked.

Returns
On success: PARSE_SUCCESS
On error:
  • PARSE_FAILURE
  • PARSE_INCOMPLETE
  • PARSE_INCOMPLETE_ENTITY
  • PARSE_NO_MATCH
Parameters
[in,out]parserHTTP Parser Object.

Definition at line 1695 of file httpparser.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parser_parse_responseline()

parse_status_t parser_parse_responseline ( http_parser_t parser)

Get HTTP Method, URL location and version information.

Returns
  • PARSE_OK
  • PARSE_SUCCESS
  • PARSE_FAILURE
Todo:
Check what function description is the right one. There was another found.
Parameters
[in,out]parserHTTP Parser object.

Definition at line 1362 of file httpparser.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parser_parse_headers()

parse_status_t parser_parse_headers ( http_parser_t parser)

Get HTTP Method, URL location and version information.

Returns
  • PARSE_OK
  • PARSE_SUCCESS
  • PARSE_FAILURE
Todo:
Check what function description is the right one. There was another found.
Parameters
[in,out]parserHTTP Parser object.

Definition at line 1429 of file httpparser.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parser_parse_entity()

parse_status_t parser_parse_entity ( http_parser_t parser)

Determines method to read entity.

Returns
  • PARSE_OK
  • PARSE_FAILURE
  • PARSE_COMPLETE – no more reading to do
Todo:
Check what function description is the right one. There was another found.
Parameters
[in,out]parserHTTP Parser object.

Definition at line 1637 of file httpparser.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parser_get_entity_read_method()

parse_status_t parser_get_entity_read_method ( http_parser_t parser)

Determines method to read entity.

Returns
On success: PARSE_CONTINUE_1
On error:
  • PARSE_FAILURE
  • PARSE_SUCCESS – no more reading to do
Parameters
[in,out]parserHTTP Parser object.

Definition at line 1552 of file httpparser.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parser_append()

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.

Returns
On success: PARSE_SUCCESS
On error:
  • PARSE_FAILURE
  • PARSE_INCOMPLETE
  • PARSE_INCOMPLETE_ENTITY
  • PARSE_NO_MATCH
Parameters
[in,out]parserHTTP Parser object.
[in]bufbuffer to be appended to the parser.
[in]buf_lengthSize of the buffer.

Definition at line 1736 of file httpparser.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ matchstr()

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.

Returns
  • PARSE_OK
  • PARSE_NO_MATCH – failure to match pattern 'fmt'
  • PARSE_FAILURE – 'str' is bad input
Parameters
[in]strString to be matched.
[in]slenLength of the string.
[in]fmtPattern format for arguments (like printf()).
[in]...Variable list of arguments (like printf()).

Definition at line 1328 of file httpparser.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ raw_to_int()

int raw_to_int ( memptr raw_value,
int  base 
)

Converts raw character data to integer value.

Returns
integer
Parameters
[in]raw_valueBuffer to be converted.
baseBase to use for conversion.

Definition at line 1753 of file httpparser.cpp.

+ Here is the caller graph for this function:

◆ raw_find_str()

int raw_find_str ( memptr raw_value,
const char *  str 
)

Find a substring from raw character string buffer.

Side effects: raw_value is transformed to lowercase.

Returns
integer - index at which the substring is found.
Parameters
[in]raw_valueBuffer containg the string.
[in]strSubstring to be found.

Definition at line 1771 of file httpparser.cpp.

+ Here is the caller graph for this function:

◆ method_to_str()

const char * method_to_str ( http_method_t  method)

A wrapper function that maps a method id to a method.

nameConverts a http_method id stored in the HTTP Method.

Returns
Pointer to the HTTP Method.
Parameters
[in]methodHTTP method.

Definition at line 1801 of file httpparser.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ print_http_headers()

void print_http_headers ( std::string_view  log_msg,
http_message_t hmsg 
)

Print the HTTP headers.

Function is mainly used for debugging.

Parameters
[in]log_msglog message number ("MSGxxxx").
[in]hmsgHTTP Message object.

Definition at line 1815 of file httpparser.cpp.

+ Here is the call graph for this function:

Variable Documentation

◆ Http_Header_Names

constexpr std::array<const UPnPsdk::str_int_entry, 33> Http_Header_Names
inlineconstexpr

Assigns header-name id to its text representation.

Definition at line 157 of file httpparser.hpp.