Mstdlib-1.24.0
HTTP Simple Writer

Functions

unsigned char * M_http_simple_write_request (M_http_method_t method, const char *host, unsigned short port, const char *uri, const char *user_agent, const char *content_type, const M_hash_dict_t *headers, const unsigned char *data, size_t data_len, const char *charset, size_t *len)
 
M_bool M_http_simple_write_request_buf (M_buf_t *buf, M_http_method_t method, const char *host, unsigned short port, const char *uri, const char *user_agent, const char *content_type, const M_hash_dict_t *headers, const unsigned char *data, size_t data_len, const char *charset)
 
unsigned char * M_http_simple_write_response (M_uint32 code, const char *reason, const char *content_type, const M_hash_dict_t *headers, const unsigned char *data, size_t data_len, const char *charset, size_t *len)
 
M_bool M_http_simple_write_response_buf (M_buf_t *buf, M_uint32 code, const char *reason, const char *content_type, const M_hash_dict_t *headers, const unsigned char *data, size_t data_len, const char *charset)
 

Detailed Description

Generate request and response messages.

Does not support:

Function Documentation

◆ M_http_simple_write_request()

unsigned char * M_http_simple_write_request ( M_http_method_t  method,
const char *  host,
unsigned short  port,
const char *  uri,
const char *  user_agent,
const char *  content_type,
const M_hash_dict_t headers,
const unsigned char *  data,
size_t  data_len,
const char *  charset,
size_t *  len 
)

Create an HTTP/1.1 request message, return as a new string.

Caller is responsible for freeing the returned string.

If the Content-Length header is not provided in headers, it will be added automatically for you, using data_len as the length. Data can be NULL If either the header or data_len is provided. Data can be sent later. This allows generating the header and sending large messages without buffering the data in memory. In this case, this function will generate the necessary HTTP header part of the message.

If set host, port, uri, user_agent, and content_type will all override any that were previoiusly set in the headers.

See also
M_http_simple_write_request_buf
Parameters
[in]methodHTTP verb to use (GET, POST, etc).
[in]hostHost the request will be sent to. Required if 'Host' header is not set.
[in]portPort the request will be sent on. 0 or 80 will not set the port part of the 'Host' Header. However, if the port is 0 it is assumed to be 80. Will be ignored if 'Host' header is set.
[in]uriURI (may be absolute or relative, may include query string). Typically, this will be relative. if absolute, the host and port should match the host and port parameters.
[in]user_agentOptional user agent to include in the request.
[in]content_typeOptional content type. If not set and not in headers this will default to 'plain/text'.
[in]headersAdditional headers to include in request.
[in]dataString to place in body of message (may be empty).
[in]data_lenNumber of chars to use from data (may be 0). Cannot be 0 if data is NULL.
[in]charsetEncoding of the data. M_TEXTCODEC_UNKNOWN should be used if charset should not be added to the Content-Type header. This will overwrite the charset if already present in Content-Type. Also use M_TEXTCODEC_UNKNOWN for non-text data.
[out]lenPlace to store length of returned HTTP request message (may be NULL).
Returns
Allocated string containing HTTP request message. The string will be NULL terminated.

◆ M_http_simple_write_request_buf()

M_bool M_http_simple_write_request_buf ( M_buf_t buf,
M_http_method_t  method,
const char *  host,
unsigned short  port,
const char *  uri,
const char *  user_agent,
const char *  content_type,
const M_hash_dict_t headers,
const unsigned char *  data,
size_t  data_len,
const char *  charset 
)

Create an HTTP/1.1 request message, add it to the given buffer.

Same as M_http_simple_write_request(), except that it adds the new message to the given buffer instead of returning it in a newly-allocated string.

Parameters
[out]bufBuffer to add the message to.
[in]methodHTTP verb to use (GET, POST, etc).
[in]hostHost the request will be sent to. Required if 'Host' header is not set.
[in]portPort the request will be sent on. 0 or 80 will not set the port part of the 'Host' Header. However, if the port is 0 it is assumed to be 80. Will be ignored if 'Host' header is set.
[in]uriURI (may be absolute or relative, may include query string). Typically, this will be relative. if absolute, the host and port should match the host and port parameters.
[in]user_agentOptional user agent to include in the request.
[in]content_typeOptional content type. If not set and not in headers this will default to 'plain/text'.
[in]headersAdditional headers to include in request.
[in]dataString to place in body of message (may be empty).
[in]data_lenNumber of chars to use from data (may be 0). Cannot be 0 if data is NULL.
[in]charsetEncoding of the data. Set NULL or empty to not add a charset to the Content-Type header. If set, this will overwrite the charset if already present in Content-Type.
Returns
M_TRUE if add was successful, M_FALSE if message creation failed.
See also
M_http_simple_write_request

◆ M_http_simple_write_response()

unsigned char * M_http_simple_write_response ( M_uint32  code,
const char *  reason,
const char *  content_type,
const M_hash_dict_t headers,
const unsigned char *  data,
size_t  data_len,
const char *  charset,
size_t *  len 
)

Create an HTTP/1.1 response message, return as new string.

Caller is responsible for freeing the returned string.

If the Content-Length header is not provided in headers, it will be added automatically for you, using data_len as the length. Data can be NULL If either the header or data_len is provided. Data can be sent later. This allows generating the header and sending large messages without buffering the data in memory. In this case, this function will generate the necessary HTTP header part of the message.

Parameters
[in]codeHTTP status code to use (200, 404, etc).
[in]reasonHTTP status reason string. If NULL, will attempt to pick one automatically based on code.
[in]content_typeOptional content type. If not set and not in headers this will default to 'plain/text'.
[in]headersHeaders to include in response.
[in]dataString to place in body of message (may be empty).
[in]data_lenNumber of chars to use from data (may be 0).
[in]charsetEncoding of the data. Set NULL or empty to not add a charset to the Content-Type header. If set, this will overwrite the charset if already present in Content-Type.
[out]lenPlace to store length of returned HTTP response message (may be NULL).
Returns
New string containing HTTP response message.
See also
M_http_simple_write_response_buf

◆ M_http_simple_write_response_buf()

M_bool M_http_simple_write_response_buf ( M_buf_t buf,
M_uint32  code,
const char *  reason,
const char *  content_type,
const M_hash_dict_t headers,
const unsigned char *  data,
size_t  data_len,
const char *  charset 
)

Create an HTTP/1.1 response message, add it to the given buffer.

Same as M_http_simple_write_response(), except that it adds the new message to the given buffer instead of returning it in a newly-allocated string.

Parameters
[out]bufBuffer to add the message to.
[in]codeHTTP status code to use (200, 404, etc).
[in]reasonHTTP status reason string. If NULL, will attempt to pick one automatically based on code.
[in]content_typeOptional content type. If not set and not in headers this will default to 'plain/text'.
[in]headersHeaders to include in response.
[in]dataString to place in body of message (may be empty).
[in]data_lenNumber of chars to use from data (may be 0).
[in]charsetEncoding of the data. Set NULL or empty to not add a charset to the Content-Type header. If set, this will overwrite the charset if already present in Content-Type.
Returns
M_TRUE if add was successful, M_FALSE if message creation failed.
See also
M_http_simple_write_response