Mstdlib-1.24.0
HTTP Simple Reader

Typedefs

typedef struct M_http_simple_read M_http_simple_read_t
 

Enumerations

enum  M_http_simple_read_flags_t {
  M_HTTP_SIMPLE_READ_NONE = 0 ,
  M_HTTP_SIMPLE_READ_NODECODE_BODY ,
  M_HTTP_SIMPLE_READ_LEN_REQUIRED ,
  M_HTTP_SIMPLE_READ_FAIL_EXTENSION ,
  M_HTTP_SIMPLE_READ_FAIL_TRAILERS
}
 

Functions

M_http_error_t M_http_simple_read (M_http_simple_read_t **simple, const unsigned char *data, size_t data_len, M_uint32 flags, size_t *len_read)
 
M_http_error_t M_http_simple_read_parser (M_http_simple_read_t **simple, M_parser_t *parser, M_uint32 flags)
 
void M_http_simple_read_destroy (M_http_simple_read_t *http)
 
M_http_message_type_t M_http_simple_read_message_type (const M_http_simple_read_t *simple)
 
M_http_version_t M_http_simple_read_version (const M_http_simple_read_t *simple)
 
M_uint32 M_http_simple_read_status_code (const M_http_simple_read_t *simple)
 
const char * M_http_simple_read_reason_phrase (const M_http_simple_read_t *simple)
 
M_http_method_t M_http_simple_read_method (const M_http_simple_read_t *simple)
 
const char * M_http_simple_read_uri (const M_http_simple_read_t *simple)
 
const char * M_http_simple_read_path (const M_http_simple_read_t *simple)
 
const char * M_http_simple_read_query_string (const M_http_simple_read_t *simple)
 
const M_hash_dict_tM_http_simple_read_query_args (const M_http_simple_read_t *simple)
 
const char * M_http_simple_read_host (const M_http_simple_read_t *simple)
 
M_bool M_http_simple_read_port (const M_http_simple_read_t *simple, M_uint16 *port)
 
M_hash_dict_tM_http_simple_read_headers_dict (const M_http_simple_read_t *simple)
 
M_list_str_tM_http_simple_read_headers (const M_http_simple_read_t *simple)
 
char * M_http_simple_read_header (const M_http_simple_read_t *simple, const char *key)
 
const M_list_str_tM_http_simple_read_get_set_cookie (const M_http_simple_read_t *simple)
 
M_bool M_http_simple_read_is_body_form_data (const M_http_simple_read_t *simple)
 
const unsigned char * M_http_simple_read_body (const M_http_simple_read_t *simple, size_t *len)
 
const M_hash_dict_tM_http_simple_read_body_form_data (const M_http_simple_read_t *simple)
 
const char * M_http_simple_read_content_type (const M_http_simple_read_t *simple)
 
M_textcodec_codec_t M_http_simple_read_codec (const M_http_simple_read_t *simple)
 
const char * M_http_simple_read_charset (const M_http_simple_read_t *simple)
 

Detailed Description

Reads a full HTTP message. Useful for small messages. All data is contained within on object for easy processing.

Will attempt to decode body data based on the detected charset unless M_HTTP_SIMPLE_READ_NODECODE_BODY is set.

Use M_http_simple_read_is_body_form_data() to determine which accessors to use. M_http_simple_read_codec will dictated the current encoding of the body data. If form encoded this is the encoding of the key value pairs not the raw body data. If not form data this is the encoding of the body data. M_http_simple_read_is_body will always return body data. If from encoded the data will still be form encoded.

Codec will be set to the codec of the body data. Unless form encoded, in which case it is the codec for the key value pairs from M_http_simple_read_is_body_form_data().

If the encoding could not be detected or is not supported the codec will be unknown. Use M_http_simple_read_charset() to determine the charset of the data.

Charset and codec will only be read from the Content-Type header. Content type and encoding set in an HTML tag is not supported.

Typedef Documentation

◆ M_http_simple_read_t

Enumeration Type Documentation

◆ M_http_simple_read_flags_t

Enumerator
M_HTTP_SIMPLE_READ_NONE 
M_HTTP_SIMPLE_READ_NODECODE_BODY 

Do not attempt to decode the body data (from detected charset).

M_HTTP_SIMPLE_READ_LEN_REQUIRED 

Require content-length, cannot be chunked data.

M_HTTP_SIMPLE_READ_FAIL_EXTENSION 

Fail if chunked extensions are specified. Otherwise, Ignore.

M_HTTP_SIMPLE_READ_FAIL_TRAILERS 

Fail if trailers sent. Otherwise, they are ignored.

Function Documentation

◆ M_http_simple_read()

M_http_error_t M_http_simple_read ( M_http_simple_read_t **  simple,
const unsigned char *  data,
size_t  data_len,
M_uint32  flags,
size_t *  len_read 
)

Read the next HTTP message from the given buffer, store results in a new M_http_simple_read_t object.

Will return M_HTTP_ERROR_MOREDATA if we need to wait for more data to get a complete message.

Parameters
[out]simplePlace to store new M_http_simple_read_t object. Can be NULL to check for valid message. Will only be set on M_HTTP_ERROR_SUCCESS, and M_HTTP_ERROR_SUCCESS_MORE_POSSIBLE.
[in]dataBuffer containing HTTP messages to read.
[in]data_lenLength of data.
[in]flagsRead options (OR'd combo of M_http_simple_read_flags_t).
[in]len_readNum bytes consumed from data (may be NULL, if caller doesn't need this info). Will be set on error indicating the location in the message that generated the error.
Returns
Response code.
See also
M_http_reader_read
M_http_simple_read_parser
M_http_simple_read_destroy

◆ M_http_simple_read_parser()

M_http_error_t M_http_simple_read_parser ( M_http_simple_read_t **  simple,
M_parser_t parser,
M_uint32  flags 
)

Read the next HTTP message from the given parser.

Will return M_HTTP_ERROR_MOREDATA if we need to wait for more data to get a complete message. No data will be dropped from the parser, in this case.

On all other return values the parser will advance and data consumed. On a hard ERROR condition the parser will start at the point of the error. If this is undesirable, the parser should be marked and rewound after this function is called.

Parameters
[out]simplePlace to store new M_http_simple_read_t object. Can be NULL to check for valid message.
[in]parserBuffer containing HTTP messages to read.
[in]flagsRead options (OR'd combo of M_http_simple_read_flags_t).
Returns
Response code.
See also
M_http_reader_read
M_http_simple_read
M_http_simple_destroy

◆ M_http_simple_read_destroy()

void M_http_simple_read_destroy ( M_http_simple_read_t http)

Destroy the given M_http_simple_read_t object.

Parameters
[in]httpobject to destroy
See also
M_http_simple_read
M_http_simple_read_parser

◆ M_http_simple_read_message_type()

M_http_message_type_t M_http_simple_read_message_type ( const M_http_simple_read_t simple)

Return the type of the parsed message.

Parameters
[in]simpleParsed HTTP message.
Returns
Type of message (REQUEST or RESPONSE, usually).

◆ M_http_simple_read_version()

M_http_version_t M_http_simple_read_version ( const M_http_simple_read_t simple)

Return the HTTP protocol version of the parsed message.

Parameters
[in]simpleParsed HTTP message.
Returns
HTTP protocol version (1.0, 1.1).

◆ M_http_simple_read_status_code()

M_uint32 M_http_simple_read_status_code ( const M_http_simple_read_t simple)

Return the HTTP status code of the parsed message.

The status code is only set for response messages (type == M_HTTP_MESSAGE_TYPE_RESPONSE). If the parsed message wasn't a response, the returned status code will be 0.

See also
M_http_simple_read_reason_phrase
Parameters
[in]simpleParsed HTTP message.
Returns
HTTP status code (200, 404, etc.), or 0 if this isn't a response.

◆ M_http_simple_read_reason_phrase()

const char * M_http_simple_read_reason_phrase ( const M_http_simple_read_t simple)

Return the human-readable status of the parsed message.

This is the text that goes with the HTTP status code in the message.

The reason phrase is only set for response messages (type == M_HTTP_MESSAGE_TYPE_RESPONSE). If the parsed message wasn't a response, the returned string will be NULL.

Parameters
[in]simpleParsed HTTP message.
Returns
String describing reason for message's status code, or NULL if this isn't a response.
See also
M_http_simple_read_status_code

◆ M_http_simple_read_method()

M_http_method_t M_http_simple_read_method ( const M_http_simple_read_t simple)

Return the HTTP method (GET, POST, etc) of the parsed message.

The method is only set for request messages (type == M_HTTP_MESSAGE_TYPE_REQUEST). If the parsed message wasn't a request, M_HTTP_METHOD_UNKNOWN will be returned.

Parameters
[in]simpleParsed HTTP message.
Returns
HTTP verb used by the parsed message, or M_HTTP_METHOD_UNKNOWN if this isn't a request.

◆ M_http_simple_read_uri()

const char * M_http_simple_read_uri ( const M_http_simple_read_t simple)

Return the full URI (port, path and query) of the parsed message.

Only request messages have a URI. If the parsed message wasn't a request, the returned string will be NULL.

Parameters
[in]simpleParsed HTTP message.
Returns
URI of the parsed message, or NULL if this isn't a request.
See also
M_http_simple_read_port
M_http_simple_read_path
M_http_simple_read_query_string
M_http_simple_read_query_args

◆ M_http_simple_read_path()

const char * M_http_simple_read_path ( const M_http_simple_read_t simple)

Return the path component of the URI from the parsed message.

Only request messages have a URI. If the parsed message wasn't a request, the function will return NULL.

The path may be relative or absolute.

Parameters
[in]simpleParsed HTTP message.
Returns
Path part of URI, or NULL if this isn't a request.
See also
M_http_simple_read_uri

◆ M_http_simple_read_query_string()

const char * M_http_simple_read_query_string ( const M_http_simple_read_t simple)

Return the query component of the URI from the parsed message.

The returned query string hasn't been processed in any way. Call M_http_simple_read_query_args() instead to process the query and return its contents as a set of key-value pairs.

Only request messages have a URI. If the parsed message wasn't a request, the function will return NULL.

Not all requests have a query string embedded in the URI. This is normally seen in GET requests, but it's not always present even there.

Parameters
[in]simpleParsed HTTP message.
Returns
Query string from URI, or NULL if not present.
See also
M_http_simple_read_query_args
M_http_simple_read_uri

◆ M_http_simple_read_query_args()

const M_hash_dict_t * M_http_simple_read_query_args ( const M_http_simple_read_t simple)

Parse arguments from query component of URI as key-value pairs.

Processes the query string (if any), then returns a key->value mapping of all the values present in the string.

Warning
Any keys in the query string that don't have values (no '='), or whose values are empty ('key=') will not be present in the returned mapping. To parse empty keys, you have to process the query string returned by M_http_simple_read_query_string() yourself.
Parameters
[in]simpleParsed HTTP message.
Returns
Dictionary containing key-value mappings from query string, or NULL if there were none.
See also
M_http_simple_read_query_string
M_http_simple_read_uri

◆ M_http_simple_read_host()

const char * M_http_simple_read_host ( const M_http_simple_read_t simple)

Return the host.

Only request messages have a host. If the parsed message wasn't a request, the function will return NULL.

The host will be read from the URI and fall back to the Host header. If neither are present the host will be NULL.

Parameters
[in]simpleParsed HTTP message.
Returns
Host.

◆ M_http_simple_read_port()

M_bool M_http_simple_read_port ( const M_http_simple_read_t simple,
M_uint16 *  port 
)

Return the port number.

Only request messages have a port. If the parsed message wasn't a request, the function will return M_FALSE and set port to 0.

The port will be read from the URI and fall back to the Host header. If neither are present the port will be returned as 0. In this case it should be assumed to be 80.

Parameters
[in]simpleParsed HTTP message.
[out]portPlace to store port number. May be NULL, if you're just checking to see if a port is present.
Returns
M_TRUE if a port was set, M_FALSE if there was no port in the message.

◆ M_http_simple_read_headers_dict()

M_hash_dict_t * M_http_simple_read_headers_dict ( const M_http_simple_read_t simple)

Get headers from parsed message as key-multivalue pairs.

Note that some headers may contain a list of multiple values, so the returned M_hash_dict_t is a multimap (one key may map to a list of values). The first entry in the list is the first item in the value list if there are multiple values.

Modfiers that are part of the value are included with the value.

Header names are not case-sensitive, when doing lookups into the returned dictionary.

Warning
M_http_simple_read_header should be used in order to get a full header. This is a parsed list and is provided to make searching for specific values easier. Use M_http_simple_read_headers to get a list of headers.
The returned dictionary does not include "Set-Cookie" headers, because they can be sent multiple times with different attributes, and their values cannot be merged into a list.
Parameters
[in]simpleParsed HTTP message.
Returns
Multimap of header names and values.
See also
M_http_simple_read_header
M_http_simple_read_headers
M_http_simple_read_get_set_cookie

◆ M_http_simple_read_headers()

M_list_str_t * M_http_simple_read_headers ( const M_http_simple_read_t simple)

Get a list of headers.

Header names are not case-sensitive.

Warning
The returned list does not include "Set-Cookie" headers, because they can be sent multiple times with different attributes, and their values cannot be merged into a list.
Parameters
[in]simpleParsed HTTP message.
Returns
List of headers
See also
M_http_simple_read_header
M_http_simple_read_get_set_cookie

◆ M_http_simple_read_header()

char * M_http_simple_read_header ( const M_http_simple_read_t simple,
const char *  key 
)

Get value of the named header from the parsed message.

The key is not case-sensitive - it will match header names that only differ because of capitalization.

Note that some headers may contain a list of multiple values. For these headers, this function will return a comma-delimited list of values. Some extra white space may be added in addition to the commas.

Warning
Attempts to retrieve "Set-Cookie" header values with this function will fail, because those headers may be sent multiple times with different attributes, and their values cannot be merged into a list.
Parameters
[in]simpleParsed HTTP message.
[in]keyName of header to retrieve values from.
Returns
Delimited list of values (if necessary) for this header, or NULL if no data found.
See also
M_http_simple_read_headers
M_http_simple_read_get_set_cookie

◆ M_http_simple_read_get_set_cookie()

const M_list_str_t * M_http_simple_read_get_set_cookie ( const M_http_simple_read_t simple)

Return list of values from all Set-Cookie headers in the parsed message.

Note
This does not set anything, it is an accessor to get the "Set-Cookie" header field. The header is called "Set-Cookie" and can be set multiple times with different values.

The returned list of values is stable-sorted alphabetically.

Parameters
[in]simpleParsed HTTP message.
Returns
Sorted list of all cookies in the message (may be empty).
See also
M_http_simple_read_header
M_http_simple_read_headers

◆ M_http_simple_read_is_body_form_data()

M_bool M_http_simple_read_is_body_form_data ( const M_http_simple_read_t simple)

Whether the decoded body data is form encoded and can be accessed using M_http_simple_read_body_form_data(). If the text codec is set, the codec represents the codec of the form data key value pairs. While M_http_simple_read_body() will still be encoded as form data.

Parameters
[in]simpleParsed HTTP message.
Returns
Bytes from body of message.

◆ M_http_simple_read_body()

const unsigned char * M_http_simple_read_body ( const M_http_simple_read_t simple,
size_t *  len 
)

Return the body of the parsed message (if any).

If the body is application/x-www-form-urlencoded this will return the raw form data encoded body. Form data decodes to key value pairs so accessing the data will need to be used through M_http_simple_read_body_form_data().

When not form encoded. The body data will be in be represented by the codec from M_http_simple_read_codec().

See also
M_http_simple_read_body_form_data()
Parameters
[in]simpleParsed HTTP message.
[out]lenPlace to store length of body (may be NULL).
Returns
Bytes from body of message.

◆ M_http_simple_read_body_form_data()

const M_hash_dict_t * M_http_simple_read_body_form_data ( const M_http_simple_read_t simple)

Return the body of the parsed message (if any) if it was form data encoded.

This will only be filled when the content-type is application/x-www-form-urlencoded and there is key value pair body data. The data within the key value pairs will have be represented by the codec from M_http_simple_read_codec().

Parameters
[in]simpleParsed HTTP message.
Returns
Bytes from body of message.

◆ M_http_simple_read_content_type()

const char * M_http_simple_read_content_type ( const M_http_simple_read_t simple)

Get the content type.

May be empty if the content type was not set. Or if it was application/x-www-form-urlencoded and the body was auto decoded.

application/x-www-form-urlencoded does not allows allow the encoding to be known. Preventing auto deciding from taking place. Leaving this as application/x-www-form-urlencoded and the codec as unknown.

Parameters
[in]simpleParsed HTTP message.
Returns
The content type.

◆ M_http_simple_read_codec()

M_textcodec_codec_t M_http_simple_read_codec ( const M_http_simple_read_t simple)

Get the codec of the data.

Codec is detected by charset from the Content-Type header. May be unknown if charset was not present or if the charset is not supported by mstdlib's text encoding functionality.

If body decoding takes place and the data will have been be decoded to utf-8. The codec will have been updated to utf-8 and the content length will have been updated to match the new body length.

Parameters
[in]simpleParsed HTTP message.
Returns
Encoded data codec.
See also
M_http_simple_read_charset

◆ M_http_simple_read_charset()

const char * M_http_simple_read_charset ( const M_http_simple_read_t simple)

Get the text charset of the data.

The charater set as defined in the Content-Type header. This should correspond to codec returned by M_http_simple_read_codec. However, if codec is unknown due to not being supported by mstdlib's text codec, this is the charset used by the data. Body decoding will not take place if codec is unknown. It is up to the application to determine how to decoded the body data in this situation.

Parameters
[in]simpleParsed HTTP message.
Returns
Character set.
See also
M_http_simple_read_codec