Typedefs | Enumerations | Functions
http_response.h File Reference

HTTP response. More...

Typedefs

typedef struct vtm_http_res vtm_http_res
 

Enumerations

enum  vtm_http_res_mode { VTM_HTTP_RES_MODE_FIXED, VTM_HTTP_RES_MODE_CHUNKED }
 
enum  vtm_http_res_act { VTM_HTTP_RES_ACT_CLOSE_CON, VTM_HTTP_RES_ACT_KEEP_CON, VTM_HTTP_RES_ACT_UPGRADE_WS }
 

Functions

VTM_API int vtm_http_res_begin (vtm_http_res *res, enum vtm_http_res_mode mode, int status)
 
VTM_API int vtm_http_res_header (vtm_http_res *res, const char *name, const char *value)
 
VTM_API int vtm_http_res_body_str (vtm_http_res *res, const char *data)
 
VTM_API int vtm_http_res_body_raw (vtm_http_res *res, const char *src, size_t len)
 
VTM_API int vtm_http_res_body_emt (vtm_http_res *res, struct vtm_socket_emitter *se)
 
VTM_API int vtm_http_res_set_action (vtm_http_res *res, enum vtm_http_res_act act, void *data)
 
VTM_API int vtm_http_res_end (vtm_http_res *res)
 
VTM_API int vtm_http_res_set_date (vtm_http_res *res)
 
VTM_API bool vtm_http_res_was_started (vtm_http_res *res)
 
VTM_API bool vtm_http_res_was_sent (vtm_http_res *res)
 
VTM_API enum vtm_http_version vtm_http_res_get_version (vtm_http_res *res)
 

Enumeration Type Documentation

Enumerator
VTM_HTTP_RES_MODE_FIXED 

Response body is buffered and then sent as whole

VTM_HTTP_RES_MODE_CHUNKED 

Response body is sent in chunks without buffering

Enumerator
VTM_HTTP_RES_ACT_CLOSE_CON 

close connection after response has been sent

VTM_HTTP_RES_ACT_KEEP_CON 

keep connection alive

VTM_HTTP_RES_ACT_UPGRADE_WS 

Upgrade connection to a WebSocket connection

Function Documentation

VTM_API int vtm_http_res_begin ( vtm_http_res *  res,
enum vtm_http_res_mode  mode,
int  status 
)

Initializes and starts the response with mode and status code.

This must always be the first call on a response handle.

Parameters
resthe response
modethe mode how the body is sent
statusone of the three digit HTTP status codes
Returns
VTM_OK if the call succeeded
VTM_E_IO_UNKNOWN or VTM_ERROR if an error occured
VTM_API int vtm_http_res_header ( vtm_http_res *  res,
const char *  name,
const char *  value 
)

Sets a response header.

Parameters
resthe response
namethe name of the header field
valuethe value for the header field
Returns
VTM_OK if the call succeeded
VTM_E_IO_UNKNOWN or VTM_ERROR if an error occured
VTM_API int vtm_http_res_body_str ( vtm_http_res *  res,
const char *  data 
)

Adds a string to the response body.

Parameters
resthe response
datathe string that should be appended to the body
Returns
VTM_OK if the call succeeded
VTM_E_IO_UNKNOWN or VTM_ERROR if an error occured
VTM_API int vtm_http_res_body_raw ( vtm_http_res *  res,
const char *  src,
size_t  len 
)

Adds a chunk of memory to the response body

Parameters
resthe response
srcpointer to the memory chunk
lenlength of memory region in bytes
Returns
VTM_OK if the call succeeded
VTM_E_IO_UNKNOWN or VTM_ERROR if an error occured
VTM_API int vtm_http_res_body_emt ( vtm_http_res *  res,
struct vtm_socket_emitter se 
)

Lets the response body be defined by a socket emitter.

The emitter is automatically released.

Parameters
resthe response
sepointer to the socket emitter
Returns
VTM_OK if the call succeeded
VTM_E_IO_UNKNOWN or VTM_ERROR if an error occured
VTM_API int vtm_http_res_set_action ( vtm_http_res *  res,
enum vtm_http_res_act  act,
void *  data 
)

Sets the action that is performed after the response has been sent.

This is used for example when upgrading an HTTP connection to a WebSocket connection.

The action can only be changed before the body was written.

Parameters
resthe response
actthe action that should be performed
dataadditional data needed by the action
Returns
VTM_OK if the call succeeded
VTM_E_INVALID_STATE if the response body was already started
VTM_API int vtm_http_res_end ( vtm_http_res *  res)

Marks the response as complete and starts sending.

Parameters
resthe response that is complete
Returns
VTM_OK if the call succeeded
VTM_E_IO_UNKNOWN or VTM_ERROR if an error occured
VTM_API int vtm_http_res_set_date ( vtm_http_res *  res)

Sets HTTP date header.

Parameters
resthe response
Returns
VTM_OK if the call succeeded
VTM_E_IO_UNKNOWN or VTM_ERROR if an error occured
VTM_API bool vtm_http_res_was_started ( vtm_http_res *  res)

Checks if the reponse was already started.

Parameters
resthe response that should be checked
Returns
true if the response was started, false otherwise
VTM_API bool vtm_http_res_was_sent ( vtm_http_res *  res)

Checks if the response was sent by vtm_http_res_end().

Parameters
resthe response that should be checked
Returns
true if the response was sent, false otherwise
VTM_API enum vtm_http_version vtm_http_res_get_version ( vtm_http_res *  res)

Gets the HTTP version of the response

res the response whose version should be retrieved

Returns
the HTTP version of the response