HomePort
|
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 } |
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:
Definition at line 100 of file hpd_httpd.h.
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 |
#define HPD_HTTPD_SETTINGS_DEFAULT |
Default settings for httpd.
Use this as:
Definition at line 123 of file hpd_httpd.h.
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.
typedef struct hpd_httpd_request hpd_httpd_request_t |
Definition at line 39 of file hpd_httpd.h.
typedef struct hpd_httpd_response hpd_httpd_response_t |
Definition at line 40 of file hpd_httpd.h.
typedef enum hpd_httpd_return hpd_httpd_return_t |
typedef struct hpd_httpd_settings hpd_httpd_settings_t |
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.
enum hpd_httpd_return |
Enumerator | |
---|---|
HPD_HTTPD_R_CONTINUE | |
HPD_HTTPD_R_STOP |
Definition at line 43 of file hpd_httpd.h.
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.
settings | The settings for the httpd. |
loop | The event loop to start the server on. |
Definition at line 135 of file httpd.c.
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().
instance | The httpd instance to destroy. |
Definition at line 178 of file httpd.c.
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.
req | http request |
key | Key value of argument to get |
Definition at line 841 of file httpd_request.c.
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.
req | http request |
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.
req | http request |
key | Key of cookie to get |
Definition at line 874 of file httpd_request.c.
hpd_error_t hpd_httpd_request_get_cookies | ( | hpd_httpd_request_t * | req, |
hpd_map_t ** | cookies | ||
) |
Get a all cookies for a request.
req | http request |
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.
req | http request |
key | Key for the header to get |
Definition at line 809 of file httpd_request.c.
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.
req | http request |
Definition at line 791 of file httpd_request.c.
hpd_error_t hpd_httpd_request_get_ip | ( | hpd_httpd_request_t * | req, |
const char ** | ip | ||
) |
Get the IP of a request.
req | http request |
Definition at line 905 of file httpd_request.c.
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.
req | http request |
Definition at line 759 of file httpd_request.c.
hpd_error_t hpd_httpd_request_get_url | ( | hpd_httpd_request_t * | req, |
const char ** | url | ||
) |
Get the URL of this request.
req | http request |
Definition at line 775 of file httpd_request.c.
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.
req | http request to keep open |
Definition at line 924 of file httpd_request.c.
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.
res | Http reponse to add cookie to |
field | The field of the cookie |
value | The value of the cookie |
expires | The expiring time as string or NULL to avoid |
max_age | The maximum age as string or NULL to avoid |
domain | The domain as string or NULL to avoid |
path | The path as string or NULL to avoid |
secure | 0 to avoid, any other to add the keyword |
http_only | 0 to avoid, any other to add the keyword |
extension | The extension as string or NULL to avoid |
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.
res | The response to add headers to |
field | The field of the header |
value | The value of the header |
Definition at line 178 of file httpd_response.c.
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.
req | The http request to repond to |
status | The status code to respond with |
Definition at line 115 of file httpd_response.c.
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.
res | The HTTP Response to destroy |
Definition at line 89 of file httpd_response.c.
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.
res | The respond to sent |
fmt | The format string for the body |
Definition at line 293 of file httpd_response.c.
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.
res | The http response to send. |
fmt | The format string for the body |
Definition at line 321 of file httpd_response.c.
hpd_error_t hpd_httpd_start | ( | hpd_httpd_t * | httpd | ) |
Start a httpd instance.
Starts a created httpd.
instance | The instance to start. |
Definition at line 196 of file httpd.c.
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.
instance | Instance to stop. |
Definition at line 211 of file httpd.c.