HomePort
Data Structures | Macros | Functions
httpd_response.c File Reference
Include dependency graph for httpd_response.c:

Go to the source code of this file.

Data Structures

struct  hpd_httpd_response
 A http response. More...
 

Macros

#define HTTPD_CRLF   "\r\n"
 
#define HTTPD_HTTP_VERSION   "HTTP/1.1 "
 
#define XX(num, str)   if(status == num) {return #str;}
 

Functions

hpd_error_t hpd_httpd_response_add_cookie (hpd_httpd_response_t *res, const char *field, const char *value, const char *expires, const char *max_age, const char *domain, const char *path, int secure, int http_only, const char *extension)
 Add cookie header to response. More...
 
hpd_error_t hpd_httpd_response_add_header (hpd_httpd_response_t *res, const char *field, const char *value)
 Add header to a response. More...
 
hpd_error_t hpd_httpd_response_create (hpd_httpd_response_t **response, hpd_httpd_request_t *req, hpd_status_t status)
 Create the reponse and constructs the status line. More...
 
hpd_error_t hpd_httpd_response_destroy (hpd_httpd_response_t *res)
 Destroy a hpd_httpd_response. More...
 
hpd_error_t hpd_httpd_response_sendf (hpd_httpd_response_t *res, const char *fmt,...)
 Send response to client. More...
 
hpd_error_t hpd_httpd_response_vsendf (hpd_httpd_response_t *res, const char *fmt, va_list arg)
 Send response to client. More...
 
static char * httpd_status_codes_to_str (hpd_status_t status)
 Convert a status code to string. More...
 

Data Structure Documentation

struct hpd_httpd_response

A http response.

A response should be created to an already existing request with hpd_httpd_response_create(), sent with http_response_sendf() or hpd_httpd_response_vsendf(), and freed with hpd_httpd_response_destroy().

Headers are added with hpd_httpd_response_add_header(), if the header is a cookie it can also be added with hpd_httpd_response_add_cookie().

The body is sent in chunks by repeating the calls to hpd_httpd_response_sendf() and http_response_vsentf(). The status and headers will be sent on the first call.

Definition at line 52 of file httpd_response.c.

Collaboration diagram for hpd_httpd_response:
Data Fields
hpd_tcpd_conn_t * conn The connection to send on.
const hpd_module_t * context
char * msg Status/headers to send.
hpd_status_t status

Macro Definition Documentation

#define HTTPD_CRLF   "\r\n"

Definition at line 36 of file httpd_response.c.

#define HTTPD_HTTP_VERSION   "HTTP/1.1 "

Definition at line 35 of file httpd_response.c.

#define XX (   num,
  str 
)    if(status == num) {return #str;}

Function Documentation

hpd_error_t hpd_httpd_response_add_cookie ( hpd_httpd_response_t res,
const char *  field,
const char *  value,
const char *  expires,
const char *  max_age,
const char *  domain,
const char *  path,
int  secure,
int  http_only,
const char *  extension 
)

Add cookie header to response.

Works similarily to hpd_httpd_response_add_header(), but constructs a cookie header based on the details in RFC 6265. Refer to this for the correct syntax of the parameters.

Parameters
resHttp reponse to add cookie to
fieldThe field of the cookie
valueThe value of the cookie
expiresThe expiring time as string or NULL to avoid
max_ageThe maximum age as string or NULL to avoid
domainThe domain as string or NULL to avoid
pathThe path as string or NULL to avoid
secure0 to avoid, any other to add the keyword
http_only0 to avoid, any other to add the keyword
extensionThe extension as string or NULL to avoid
Returns
0 on success and 1 otherwise

Definition at line 222 of file httpd_response.c.

hpd_error_t hpd_httpd_response_add_header ( hpd_httpd_response_t res,
const char *  field,
const char *  value 
)

Add header to a response.

This returns an error if any of the send functions has already been called.

The field/value pair is stored internally in the response.

Parameters
resThe response to add headers to
fieldThe field of the header
valueThe value of the header
Returns
0 on success and 1 on failure

Definition at line 178 of file httpd_response.c.

Here is the caller graph for this function:

hpd_error_t hpd_httpd_response_create ( hpd_httpd_response_t **  response,
hpd_httpd_request_t req,
hpd_status_t  status 
)

Create the reponse and constructs the status line.

Currently it also adds the header "Connection: close", because it do not yet support persistant connections. That is, connection where status and headers are sent multiple times.

The response is not send before one of the send functions are called, it is possible to call these with a NULL body to send messages without it.

Parameters
reqThe http request to repond to
statusThe status code to respond with
Returns
The http respond created

Definition at line 115 of file httpd_response.c.

Here is the call graph for this function:

Here is the caller graph for this function:

hpd_error_t hpd_httpd_response_destroy ( hpd_httpd_response_t res)

Destroy a hpd_httpd_response.

The will close the connection and free up any memory used by the response.

Any data sent with hpd_httpd_response_sendf() and http_reponse_vsendf() will be sent before the connection is closed.

Parameters
resThe HTTP Response to destroy

Definition at line 89 of file httpd_response.c.

Here is the call graph for this function:

Here is the caller graph for this function:

hpd_error_t hpd_httpd_response_sendf ( hpd_httpd_response_t res,
const char *  fmt,
  ... 
)

Send response to client.

Similar to the standard printf function. See hpd_httpd_response_vsendf() for details.

Parameters
resThe respond to sent
fmtThe format string for the body

Definition at line 293 of file httpd_response.c.

Here is the call graph for this function:

Here is the caller graph for this function:

hpd_error_t hpd_httpd_response_vsendf ( hpd_httpd_response_t res,
const char *  fmt,
va_list  arg 
)

Send response to client.

Similar to the standard vprintf functions

First it sends the status and header lines, if these haven't been sent yet. Then it sends the body as given in the format string and variable arguments.

If NULL is given as format no body is sent.

The response is sent delayed, when the connection is ready for it. Likewise the connection is kept open afterwards for further messages.

Parameters
resThe http response to send.
fmtThe format string for the body

Definition at line 321 of file httpd_response.c.

Here is the call graph for this function:

Here is the caller graph for this function:

static char* httpd_status_codes_to_str ( hpd_status_t  status)
static

Convert a status code to string.

The result is constructed to match the textual status code in the first line in a http response, according to RFC 2616.

Parameters
Statuscode as enum
Returns
The status code as text

Definition at line 70 of file httpd_response.c.

Here is the caller graph for this function: