HomePort
Data Structures | Macros | Typedefs | Enumerations | Functions
hpd_httpd.h File Reference
Include dependency graph for hpd_httpd.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  hpd_httpd_settings
 Settings struct for webserver. More...
 

Macros

#define HPD_HTTPD_SETTINGS_DEFAULT
 Default settings for httpd. More...
 

Typedefs

typedef hpd_httpd_return_t(* hpd_httpd_data_f) (hpd_httpd_t *httpd, hpd_httpd_request_t *req, void *httpd_ctx, void **req_data, const char *buf, size_t len)
 
typedef hpd_httpd_return_t(* hpd_httpd_nodata_f) (hpd_httpd_t *httpd, hpd_httpd_request_t *req, void *httpd_ctx, void **req_data)
 
typedef struct hpd_httpd_request hpd_httpd_request_t
 
typedef struct hpd_httpd_response hpd_httpd_response_t
 
typedef enum hpd_httpd_return hpd_httpd_return_t
 
typedef struct hpd_httpd_settings hpd_httpd_settings_t
 
typedef struct hpd_httpd hpd_httpd_t
 

Enumerations

enum  hpd_httpd_return { HPD_HTTPD_R_CONTINUE = 0, HPD_HTTPD_R_STOP = 1 }
 

Functions

hpd_error_t hpd_httpd_create (hpd_httpd_t **httpd, hpd_httpd_settings_t *settings, const hpd_module_t *context, hpd_ev_loop_t *loop)
 Create a new httpd instance. More...
 
hpd_error_t hpd_httpd_destroy (hpd_httpd_t *httpd)
 Destroy a httpd instance. More...
 
hpd_error_t hpd_httpd_request_get_argument (hpd_httpd_request_t *req, const char *key, const char **val)
 Get a specific argument of a request. More...
 
hpd_error_t hpd_httpd_request_get_arguments (hpd_httpd_request_t *req, hpd_map_t **arguments)
 Get a linked map of all URL arguements for a request. More...
 
hpd_error_t hpd_httpd_request_get_cookie (hpd_httpd_request_t *req, const char *key, const char **val)
 Get a specific cookie for a request. More...
 
hpd_error_t hpd_httpd_request_get_cookies (hpd_httpd_request_t *req, hpd_map_t **cookies)
 Get a all cookies for a request. More...
 
hpd_error_t hpd_httpd_request_get_header (hpd_httpd_request_t *req, const char *key, const char **value)
 Get a specific header of a request. More...
 
hpd_error_t hpd_httpd_request_get_headers (hpd_httpd_request_t *req, hpd_map_t **headers)
 Get a linked map of all headers for a request. More...
 
hpd_error_t hpd_httpd_request_get_ip (hpd_httpd_request_t *req, const char **ip)
 Get the IP of a request. More...
 
hpd_error_t hpd_httpd_request_get_method (hpd_httpd_request_t *req, hpd_httpd_method_t *method)
 Get the method of the http request. More...
 
hpd_error_t hpd_httpd_request_get_url (hpd_httpd_request_t *req, const char **url)
 Get the URL of this request. More...
 
hpd_error_t hpd_httpd_request_keep_open (hpd_httpd_request_t *req)
 Keep the connection for a request open. More...
 
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...
 
hpd_error_t hpd_httpd_start (hpd_httpd_t *httpd)
 Start a httpd instance. More...
 
hpd_error_t hpd_httpd_stop (hpd_httpd_t *httpd)
 Stop a httpd instance. More...
 

Data Structure Documentation

struct hpd_httpd_settings

Settings struct for webserver.

Please initialise this struct as following, to ensure that all settings have acceptable default values:

The settings hold a series of callbacks of type either data_cb or nodata_cb. Do not expect the string parameter in data callbacks to be null terminated. All data callbacks, except on_req_method, are called on chunks and therefore may be called multiple times. That is on_req_hdr_field may be called first with 'hos' and then with 't'. It is up to the implementer of these callbacks to concatenate the results if needed.

The callbacks are called in the following order (on_req_destroy may be called at any time):

Return values should generally be interpreted as follows:

  • zero: Continue parsing of message.
  • non-zero: Stop any further parsing of message. Note that the connection is kept open in both cases, thus a response can be sent to the client without parsing it entirely. The return value of on_req_destroy is ignored.

Definition at line 100 of file hpd_httpd.h.

Collaboration diagram for hpd_httpd_settings:
Data Fields
void * httpd_ctx
hpd_httpd_nodata_f on_req_begin
hpd_httpd_data_f on_req_body
hpd_httpd_nodata_f on_req_cmpl
hpd_httpd_nodata_f on_req_destroy
hpd_httpd_nodata_f on_req_hdr_cmpl
hpd_httpd_data_f on_req_hdr_field
hpd_httpd_data_f on_req_hdr_value
hpd_httpd_data_f on_req_url
hpd_httpd_nodata_f on_req_url_cmpl
hpd_tcpd_port_t port
int timeout

Macro Definition Documentation

#define HPD_HTTPD_SETTINGS_DEFAULT
Value:
{ \
.port = HPD_TCPD_P_HTTP, \
.timeout = 15, \
.httpd_ctx = NULL, \
.on_req_begin = NULL, \
.on_req_url = NULL, \
.on_req_url_cmpl = NULL, \
.on_req_hdr_field = NULL, \
.on_req_hdr_value = NULL, \
.on_req_hdr_cmpl = NULL, \
.on_req_body = NULL, \
.on_req_destroy = NULL, \
.on_req_cmpl = NULL }

Default settings for httpd.

Use this as:

1 hpd_httpd_settings_t *settings = HPD_HTTPD_SETTINGS_DEFAULT;

Definition at line 123 of file hpd_httpd.h.

Typedef Documentation

typedef hpd_httpd_return_t(* hpd_httpd_data_f) (hpd_httpd_t *httpd, hpd_httpd_request_t *req, void *httpd_ctx, void **req_data, const char *buf, size_t len)

Definition at line 52 of file hpd_httpd.h.

typedef hpd_httpd_return_t(* hpd_httpd_nodata_f) (hpd_httpd_t *httpd, hpd_httpd_request_t *req, void *httpd_ctx, void **req_data)

Definition at line 53 of file hpd_httpd.h.

Definition at line 39 of file hpd_httpd.h.

Definition at line 40 of file hpd_httpd.h.

Definition at line 41 of file hpd_httpd.h.

typedef struct hpd_httpd hpd_httpd_t

Definition at line 38 of file hpd_httpd.h.

Enumeration Type Documentation

Enumerator
HPD_HTTPD_R_CONTINUE 
HPD_HTTPD_R_STOP 

Definition at line 43 of file hpd_httpd.h.

Function Documentation

hpd_error_t hpd_httpd_create ( hpd_httpd_t **  httpd,
hpd_httpd_settings_t settings,
const hpd_module_t context,
hpd_ev_loop_t loop 
)

Create a new httpd instance.

Allocates a new httpd instance, that should be freed with hpd_httpd_destroy()

The settings is copied to the instance and a webserver instance is created for it.

Parameters
settingsThe settings for the httpd.
loopThe event loop to start the server on.
Returns
The newly created instance.

Definition at line 135 of file httpd.c.

Here is the call graph for this function:

Here is the caller graph for this function:

hpd_error_t hpd_httpd_destroy ( hpd_httpd_t httpd)

Destroy a httpd instance.

Destroy a httpd instance created with hpd_httpd_create(). If the server is started it should be stopped first by hpd_httpd_stop().

Parameters
instanceThe httpd instance to destroy.

Definition at line 178 of file httpd.c.

Here is the call graph for this function:

Here is the caller graph for this function:

hpd_error_t hpd_httpd_request_get_argument ( hpd_httpd_request_t req,
const char *  key,
const char **  val 
)

Get a specific argument of a request.

Parameters
reqhttp request
keyKey value of argument to get
Returns
Value of argument as string, or NULL if not found

Definition at line 841 of file httpd_request.c.

Here is the call graph for this function:

hpd_error_t hpd_httpd_request_get_arguments ( hpd_httpd_request_t req,
hpd_map_t **  arguments 
)

Get a linked map of all URL arguements for a request.

Parameters
reqhttp request
Returns
Arguments as a linkedmap (struct lm)

Definition at line 824 of file httpd_request.c.

hpd_error_t hpd_httpd_request_get_cookie ( hpd_httpd_request_t req,
const char *  key,
const char **  val 
)

Get a specific cookie for a request.

Parameters
reqhttp request
keyKey of cookie to get
Returns
The value of the cookie as String, or NULL if cookie was not found

Definition at line 874 of file httpd_request.c.

Here is the call graph for this function:

hpd_error_t hpd_httpd_request_get_cookies ( hpd_httpd_request_t req,
hpd_map_t **  cookies 
)

Get a all cookies for a request.

Parameters
reqhttp request
Returns
Cookies as a linkedmap (struct lm)

Definition at line 856 of file httpd_request.c.

hpd_error_t hpd_httpd_request_get_header ( hpd_httpd_request_t req,
const char *  key,
const char **  value 
)

Get a specific header of a request.

Parameters
reqhttp request
keyKey for the header to get
Returns
The value of the header with the specified key, or NULL if not found

Definition at line 809 of file httpd_request.c.

Here is the call graph for this function:

hpd_error_t hpd_httpd_request_get_headers ( hpd_httpd_request_t req,
hpd_map_t **  headers 
)

Get a linked map of all headers for a request.

Parameters
reqhttp request
Returns
Headers as a linkedmap (struct lm)

Definition at line 791 of file httpd_request.c.

Here is the caller graph for this function:

hpd_error_t hpd_httpd_request_get_ip ( hpd_httpd_request_t req,
const char **  ip 
)

Get the IP of a request.

Parameters
reqhttp request
Returns
IP as a string

Definition at line 905 of file httpd_request.c.

Here is the call graph for this function:

hpd_error_t hpd_httpd_request_get_method ( hpd_httpd_request_t req,
hpd_httpd_method_t method 
)

Get the method of the http request.

Parameters
reqhttp request
Returns
The method as a enum http_method

Definition at line 759 of file httpd_request.c.

Here is the caller graph for this function:

hpd_error_t hpd_httpd_request_get_url ( hpd_httpd_request_t req,
const char **  url 
)

Get the URL of this request.

Parameters
reqhttp request
Returns
URL as a string

Definition at line 775 of file httpd_request.c.

Here is the caller graph for this function:

hpd_error_t hpd_httpd_request_keep_open ( hpd_httpd_request_t req)

Keep the connection for a request open.

Normally connections are closed when there has been no activity on it for the amount specified in the timeout field in the httpd settings struct. To keep the connection open forever, issue or call to this function with the http request. The connection will still be closed and destroyed when the client closes it.

Parameters
reqhttp request to keep open

Definition at line 924 of file httpd_request.c.

Here is the call graph for this function:

Here is the caller graph for this function:

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:

hpd_error_t hpd_httpd_start ( hpd_httpd_t httpd)

Start a httpd instance.

Starts a created httpd.

Parameters
instanceThe instance to start.
Returns
The error code of hpd_tcpd_start()

Definition at line 196 of file httpd.c.

Here is the call graph for this function:

Here is the caller graph for this function:

hpd_error_t hpd_httpd_stop ( hpd_httpd_t httpd)

Stop a httpd instance.

Stops an already started httpd instance. All open connections will be killed without notifying clients, nor sending remaining data.

Parameters
instanceInstance to stop.

Definition at line 211 of file httpd.c.

Here is the call graph for this function:

Here is the caller graph for this function: