Mstdlib-1.24.0

Modules

 HTTP Stream Reader
 
 HTTP Simple
 

Enumerations

enum  M_http_error_t {
  M_HTTP_ERROR_SUCCESS = 0 ,
  M_HTTP_ERROR_SUCCESS_MORE_POSSIBLE ,
  M_HTTP_ERROR_MOREDATA ,
  M_HTTP_ERROR_STOP ,
  M_HTTP_ERROR_INVALIDUSE ,
  M_HTTP_ERROR_LENGTH_REQUIRED ,
  M_HTTP_ERROR_CHUNK_EXTENSION_NOTALLOWED ,
  M_HTTP_ERROR_TRAILER_NOTALLOWED ,
  M_HTTP_ERROR_URI ,
  M_HTTP_ERROR_STARTLINE_LENGTH ,
  M_HTTP_ERROR_STARTLINE_MALFORMED ,
  M_HTTP_ERROR_UNKNOWN_VERSION ,
  M_HTTP_ERROR_REQUEST_METHOD ,
  M_HTTP_ERROR_HEADER_LENGTH ,
  M_HTTP_ERROR_HEADER_FOLD ,
  M_HTTP_ERROR_HEADER_INVALID ,
  M_HTTP_ERROR_HEADER_DUPLICATE ,
  M_HTTP_ERROR_CHUNK_STARTLINE_LENGTH ,
  M_HTTP_ERROR_CHUNK_LENGTH ,
  M_HTTP_ERROR_CHUNK_MALFORMED ,
  M_HTTP_ERROR_CHUNK_EXTENSION ,
  M_HTTP_ERROR_CHUNK_DATA_MALFORMED ,
  M_HTTP_ERROR_CONTENT_LENGTH_MALFORMED ,
  M_HTTP_ERROR_NOT_HTTP ,
  M_HTTP_ERROR_MULTIPART_NOBOUNDARY ,
  M_HTTP_ERROR_MULTIPART_MISSING ,
  M_HTTP_ERROR_MULTIPART_MISSING_DATA ,
  M_HTTP_ERROR_MULTIPART_INVALID ,
  M_HTTP_ERROR_UNSUPPORTED_DATA ,
  M_HTTP_ERROR_TEXTCODEC_FAILURE ,
  M_HTTP_ERROR_USER_FAILURE
}
 
enum  M_http_message_type_t {
  M_HTTP_MESSAGE_TYPE_UNKNOWN = 0 ,
  M_HTTP_MESSAGE_TYPE_REQUEST ,
  M_HTTP_MESSAGE_TYPE_RESPONSE
}
 
enum  M_http_version_t {
  M_HTTP_VERSION_UNKNOWN = 0 ,
  M_HTTP_VERSION_1_0 ,
  M_HTTP_VERSION_1_1
}
 
enum  M_http_method_t {
  M_HTTP_METHOD_UNKNOWN = 0 ,
  M_HTTP_METHOD_OPTIONS ,
  M_HTTP_METHOD_GET ,
  M_HTTP_METHOD_HEAD ,
  M_HTTP_METHOD_POST ,
  M_HTTP_METHOD_PUT ,
  M_HTTP_METHOD_DELETE ,
  M_HTTP_METHOD_TRACE ,
  M_HTTP_METHOD_CONNECT ,
  M_HTTP_METHOD_PATCH
}
 
enum  M_http_data_format_t {
  M_HTTP_DATA_FORMAT_UNKNOWN = 0 ,
  M_HTTP_DATA_FORMAT_NONE ,
  M_HTTP_DATA_FORMAT_BODY ,
  M_HTTP_DATA_FORMAT_CHUNKED ,
  M_HTTP_DATA_FORMAT_MULTIPART
}
 

Functions

M_http_version_t M_http_version_from_str (const char *version)
 
const char * M_http_version_to_str (M_http_version_t version)
 
M_http_method_t M_http_method_from_str (const char *method)
 
const char * M_http_method_to_str (M_http_method_t method)
 
const char * M_http_code_to_reason (M_uint32 code)
 
const char * M_http_errcode_to_str (M_http_error_t err)
 
char * M_http_generate_query_string (const char *uri, const M_hash_dict_t *params)
 
M_bool M_http_generate_query_string_buf (M_buf_t *buf, const char *uri, const M_hash_dict_t *params)
 
M_hash_dict_tM_http_parse_query_string (const char *data, M_textcodec_codec_t codec)
 
char * M_http_generate_form_data_string (const M_hash_dict_t *params)
 
M_bool M_http_generate_form_data_string_buf (M_buf_t *buf, const M_hash_dict_t *params)
 
M_hash_dict_tM_http_parse_form_data_string (const char *data, M_textcodec_codec_t codec)
 

Detailed Description

HTTP 1.0/1.1 message reading and writing.

Conforms to:

There are two types of message parsing supported.

Currently supported Read:

Currently support Write:

Enumeration Type Documentation

◆ M_http_error_t

Error codes.

M_HTTP_ERROR_SUCCESS, and M_HTTP_ERROR_SUCCESS_MORE_POSSIBLE are both Success conditions. All data is valid and has been parsed.

M_HTTP_ERROR_MOREDATA indicates valid data and isn't always considered an error condition. It typically indicates a retry once more data is received condition. For example more headers could follow or the content length or chucked data was not complete. There is more data needed to complete the message. It is only an error if end of data has been reached.

M_HTTP_ERROR_STOP is not considered an error and means no more processing will/should take place. A callback should generate this if all data the caller wants has been processed if partial processing is taking place. For example, a proxy looking for X-Forwarded-For header in order to blacklist an abusive IP before forwarding the message.

Enumerator
M_HTTP_ERROR_SUCCESS 

Success. Data fully parsed and all data is present.

M_HTTP_ERROR_SUCCESS_MORE_POSSIBLE 

Success but more data possible. No content length was sent or chunking was used. The only way to know all data was received is by a disconnect.

M_HTTP_ERROR_MOREDATA 

Incomplete message, more data required. Not necessarily an error if parsing as data is streaming.

M_HTTP_ERROR_STOP 

Stop processing (Used by callback functions to indicate non-error but stop processing).

M_HTTP_ERROR_INVALIDUSE 

Invalid use.

M_HTTP_ERROR_LENGTH_REQUIRED 

Content-Length is required but not provided. 411 code.

M_HTTP_ERROR_CHUNK_EXTENSION_NOTALLOWED 

Chunk extensions are present but not allowed.

M_HTTP_ERROR_TRAILER_NOTALLOWED 

Chunk trailer present but not allowed.

M_HTTP_ERROR_URI 

Invalid URI. 400 code.

M_HTTP_ERROR_STARTLINE_LENGTH 

Start line exceed maximum length (6k limit). 414 code.

M_HTTP_ERROR_STARTLINE_MALFORMED 

Start line is malformed. 400 code.

M_HTTP_ERROR_UNKNOWN_VERSION 

Unknown or unsupported HTTP version.

M_HTTP_ERROR_REQUEST_METHOD 

Invalid request method. 501 code.

M_HTTP_ERROR_HEADER_LENGTH 

Header exceeds maximum length (8k limit). 413 code.

M_HTTP_ERROR_HEADER_FOLD 

Header folded. Folding is deprecated and should not be used. 400/502 code.

M_HTTP_ERROR_HEADER_INVALID 

Header is malformed. 400 code.

M_HTTP_ERROR_HEADER_DUPLICATE 

Duplicate header present. 400 code.

M_HTTP_ERROR_CHUNK_STARTLINE_LENGTH 

Chunk start line exceed maximum length (6k limit). 414 code.

M_HTTP_ERROR_CHUNK_LENGTH 

Failed to parse chunk length.

M_HTTP_ERROR_CHUNK_MALFORMED 

Chunk is malformed.

M_HTTP_ERROR_CHUNK_EXTENSION 

Chunk extensions present but malformed.

M_HTTP_ERROR_CHUNK_DATA_MALFORMED 

Chunk data malformed.

M_HTTP_ERROR_CONTENT_LENGTH_MALFORMED 

Content-Length present but malformed.

M_HTTP_ERROR_NOT_HTTP 

Not an HTTP message.

M_HTTP_ERROR_MULTIPART_NOBOUNDARY 

Multipart message missing boundary.

M_HTTP_ERROR_MULTIPART_MISSING 

Multipart message but multipart missing.

M_HTTP_ERROR_MULTIPART_MISSING_DATA 

Multipart data missing.

M_HTTP_ERROR_MULTIPART_INVALID 

Multipart is invalid.

M_HTTP_ERROR_UNSUPPORTED_DATA 

Data received is unsupported.

M_HTTP_ERROR_TEXTCODEC_FAILURE 

Text decode failure.

M_HTTP_ERROR_USER_FAILURE 

Generic callback generated failure.

◆ M_http_message_type_t

Message type.

Enumerator
M_HTTP_MESSAGE_TYPE_UNKNOWN 

Unknown message type.

M_HTTP_MESSAGE_TYPE_REQUEST 

Request message.

M_HTTP_MESSAGE_TYPE_RESPONSE 

Response message.

◆ M_http_version_t

HTTP version in use.

Enumerator
M_HTTP_VERSION_UNKNOWN 

Unknown.

M_HTTP_VERSION_1_0 

1.0

M_HTTP_VERSION_1_1 

1.1

◆ M_http_method_t

HTTP methods.

Enumerator
M_HTTP_METHOD_UNKNOWN 

Unknown method (null value).

M_HTTP_METHOD_OPTIONS 

Options.

M_HTTP_METHOD_GET 

Get.

M_HTTP_METHOD_HEAD 

Head.

M_HTTP_METHOD_POST 

Post.

M_HTTP_METHOD_PUT 

Put.

M_HTTP_METHOD_DELETE 

Delete.

M_HTTP_METHOD_TRACE 

Trace.

M_HTTP_METHOD_CONNECT 

Connect.

M_HTTP_METHOD_PATCH 

Patch.

◆ M_http_data_format_t

HTTP Content type.

Enumerator
M_HTTP_DATA_FORMAT_UNKNOWN 
M_HTTP_DATA_FORMAT_NONE 

Could not determine the format of the data. There is no data, Content-Length = 0.

M_HTTP_DATA_FORMAT_BODY 

Body.

M_HTTP_DATA_FORMAT_CHUNKED 

Data is chunked.

M_HTTP_DATA_FORMAT_MULTIPART 

Data is multipart.

Function Documentation

◆ M_http_version_from_str()

M_http_version_t M_http_version_from_str ( const char *  version)

Convert a version string into a version value.

The version can start with "HTTP/" or without.

Parameters
[in]versionVersion string.
Returns
Version.

◆ M_http_version_to_str()

const char * M_http_version_to_str ( M_http_version_t  version)

Convert an http version to a string.

Returns in the format "HTTP/#".

Parameters
[in]versionVersion.
Returns
String.

◆ M_http_method_from_str()

M_http_method_t M_http_method_from_str ( const char *  method)

Convert a method string into a method value.

Parameters
[in]methodMethod string.
Returns
Method.

◆ M_http_method_to_str()

const char * M_http_method_to_str ( M_http_method_t  method)

Convert an http method to a string.

Parameters
[in]methodMethod.
Returns
String.

◆ M_http_code_to_reason()

const char * M_http_code_to_reason ( M_uint32  code)

Convert an http code to a string.

Not all codes can be converted to a string. Codes that cannot be converted will return "Generic".

Parameters
[in]codeCode.
Returns
String.

◆ M_http_errcode_to_str()

const char * M_http_errcode_to_str ( M_http_error_t  err)

Convert an http error code to a string.

Parameters
[in]errError code.
Returns
Name of error code (not a description, just the enum name, like M_HTTP_ERROR_SUCCESS).

◆ M_http_generate_query_string()

char * M_http_generate_query_string ( const char *  uri,
const M_hash_dict_t params 
)

Create query string, append to given URI, return as new string.

Empty values are not permitted - keys whose values are set to the empty string will be left out of the query string.

Query string are defined as application/x-www-form-urlencoded data so url form encoding is used.

See also
M_http_generate_query_string_buf()
Parameters
[in]uriUri string (e.g., /cgi-bin/payment/start, or http://google.com/whatever).
[in]paramsKey-value pairs to encode in query string.
Returns
New string with URI + query string, or NULL if there was an encoding error.

◆ M_http_generate_query_string_buf()

M_bool M_http_generate_query_string_buf ( M_buf_t buf,
const char *  uri,
const M_hash_dict_t params 
)

Create query string, append URI + query string to buffer.

Empty values are not permitted - keys whose values are set to the empty string will be left out of the query string.

Query string are defined as application/x-www-form-urlencoded data so url form encoding is used.

See also
M_http_generate_query_string()
Parameters
[out]bufBuffer to add URI + query string to, contents remain unchanged if there was an error.
[in]uriUri string (e.g., /cgi-bin/payment/start, or http://google.com/whatever).
[in]paramsKey-value pairs to encode in query string.
Returns
M_TRUE if successful, or M_FALSE if there was an encoding error.

◆ M_http_parse_query_string()

M_hash_dict_t * M_http_parse_query_string ( const char *  data,
M_textcodec_codec_t  codec 
)

Parse a query string.

Components are expected to be application/x-www-form-urlencoded and will be decoded.

See also
M_http_generate_query_string()
Parameters
[in]dataThe formatted query arguments.
[in]codecAdditional encodings before the data was form encoded. The form decoded data will be decoded to utf-8 from this encoding. Use M_TEXTCODEC_UNKNOWN to skip additional decoding.
Returns
Multi value dict of key value pairs. Keys are considered case insensitive.

◆ M_http_generate_form_data_string()

char * M_http_generate_form_data_string ( const M_hash_dict_t params)

Create form data string.

Data is defined as application/x-www-form-urlencoded data so url form encoding is used.

See also
M_http_generate_form_data_string_buf()
Parameters
[in]paramsKey-value pairs to encode in form data string.
Returns
New string.

◆ M_http_generate_form_data_string_buf()

M_bool M_http_generate_form_data_string_buf ( M_buf_t buf,
const M_hash_dict_t params 
)

Create form data string.

See also
M_http_generate_query_string()
Parameters
[out]bufBuffer to add URI + query string to, contents remain unchanged if there was an error.
[in]paramsKey-value pairs to encode in query string.
Returns
M_TRUE if successful, or M_FALSE if there was an encoding error.

◆ M_http_parse_form_data_string()

M_hash_dict_t * M_http_parse_form_data_string ( const char *  data,
M_textcodec_codec_t  codec 
)

Parse a application/x-www-form-urlencoded paramter string.

Components are expected to be application/x-www-form-urlencoded and will be decoded. This is similar to decoding a query string but this is intended for form data from the body of a message.

See also
M_http_parse_query_string()
Parameters
[in]dataThe formatted query arguments.
[in]codecAdditional encodings before the data was form encoded. The form decoded data will be decoded to utf-8 from this encoding. Use M_TEXTCODEC_UNKNOWN to skip additional decoding.
Returns
Multi value dict of key value pairs. Keys are considered case insensitive.