HomePort
|
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... | |
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.
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 |
#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;} |
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.
|
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.
Status | code as enum |
Definition at line 70 of file httpd_response.c.