HomePort
hpd_httpd.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Aalborg University. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are
5  * permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  * conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  * of conditions and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY Aalborg University ''AS IS'' AND ANY EXPRESS OR IMPLIED
15  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Aalborg University OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * The views and conclusions contained in the software and documentation are those of the
25  * authors and should not be interpreted as representing official policies, either expressed
26  */
27 
28 #ifndef HOMEPORT_HPD_HTTPD_H
29 #define HOMEPORT_HPD_HTTPD_H
30 
31 #include <hpd/common/hpd_map.h>
32 #include <stddef.h>
33 #include <stdarg.h>
34 #include <hpd/hpd_types.h>
36 
37 // Structs
38 typedef struct hpd_httpd hpd_httpd_t;
42 
43 typedef enum hpd_httpd_return {
47 
48 /**********************************************************************
49  * Callbacks *
50  **********************************************************************/
51 
52 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);
54 
102  int timeout;
103  void* httpd_ctx;
113 };
114 
123 #define HPD_HTTPD_SETTINGS_DEFAULT { \
124  .port = HPD_TCPD_P_HTTP, \
125  .timeout = 15, \
126  .httpd_ctx = NULL, \
127  .on_req_begin = NULL, \
128  .on_req_url = NULL, \
129  .on_req_url_cmpl = NULL, \
130  .on_req_hdr_field = NULL, \
131  .on_req_hdr_value = NULL, \
132  .on_req_hdr_cmpl = NULL, \
133  .on_req_body = NULL, \
134  .on_req_destroy = NULL, \
135  .on_req_cmpl = NULL }
136 
137 // Webserver functions
143 
144 // Request functions
148 hpd_error_t hpd_httpd_request_get_header(hpd_httpd_request_t *req, const char *key, const char **value);
150 hpd_error_t hpd_httpd_request_get_argument(hpd_httpd_request_t *req, const char *key, const char **val);
152 hpd_error_t hpd_httpd_request_get_cookie(hpd_httpd_request_t *req, const char *key, const char **val);
155 
156 // Response functions
159  hpd_status_t status);
161 hpd_error_t hpd_httpd_response_sendf(hpd_httpd_response_t *res, const char *fmt, ...);
162 hpd_error_t hpd_httpd_response_vsendf(hpd_httpd_response_t *res, const char *fmt, va_list arg);
164  const char *expires, const char *max_age, const char *domain,
165  const char *path,
166  int secure, int http_only, const char *extension);
167 
168 #endif // HOMEPORT_HTTPD_H
enum hpd_status hpd_status_t
Definition: hpd_types.h:168
enum hpd_tcpd_port hpd_tcpd_port_t
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.
hpd_httpd_return_t(* hpd_httpd_nodata_f)(hpd_httpd_t *httpd, hpd_httpd_request_t *req, void *httpd_ctx, void **req_data)
Definition: hpd_httpd.h:53
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.
hpd_httpd_data_f on_req_hdr_field
Definition: hpd_httpd.h:107
data field
hpd_error_t hpd_httpd_destroy(hpd_httpd_t *httpd)
Destroy a httpd instance.
Definition: httpd.c:178
data value
hpd_error_t hpd_httpd_response_sendf(hpd_httpd_response_t *res, const char *fmt,...)
Send response to client.
hpd_error_t hpd_httpd_request_get_url(hpd_httpd_request_t *req, const char **url)
Get the URL of this request.
hpd_error_t hpd_httpd_response_vsendf(hpd_httpd_response_t *res, const char *fmt, va_list arg)
Send response to client.
data path
char * url
hpd_httpd_return
Definition: hpd_httpd.h:43
struct hp_settings settings
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.
hpd_tcpd_port_t port
Definition: hpd_httpd.h:101
An http request.
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.
hpd_error_t hpd_httpd_request_keep_open(hpd_httpd_request_t *req)
Keep the connection for a request open.
hpd_httpd_nodata_f on_req_url_cmpl
Definition: hpd_httpd.h:106
data key
hpd_error_t hpd_httpd_request_get_cookies(hpd_httpd_request_t *req, hpd_map_t **cookies)
Get a all cookies for a request.
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.
enum hpd_error hpd_error_t
Definition: hpd_types.h:167
hpd_httpd_nodata_f on_req_begin
Definition: hpd_httpd.h:104
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.
hpd_httpd_nodata_f on_req_hdr_cmpl
Definition: hpd_httpd.h:109
Settings struct for webserver.
Definition: hpd_httpd.h:100
hpd_error_t hpd_httpd_response_destroy(hpd_httpd_response_t *res)
Destroy a hpd_httpd_response.
enum hpd_httpd_return hpd_httpd_return_t
static struct ev_loop * loop
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.
Definition: httpd.c:135
hpd_error_t hpd_httpd_stop(hpd_httpd_t *httpd)
Stop a httpd instance.
Definition: httpd.c:211
hpd_error_t hpd_httpd_start(hpd_httpd_t *httpd)
Start a httpd instance.
Definition: httpd.c:196
hpd_httpd_nodata_f on_req_destroy
Definition: hpd_httpd.h:112
hpd_httpd_nodata_f on_req_cmpl
Definition: hpd_httpd.h:111
httpd instance struct
Definition: httpd.c:38
hpd_error_t hpd_httpd_response_add_header(hpd_httpd_response_t *res, const char *field, const char *value)
Add header to a response.
hpd_error_t hpd_httpd_request_get_ip(hpd_httpd_request_t *req, const char **ip)
Get the IP of a request.
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: hpd_httpd.h:52
hpd_httpd_data_f on_req_body
Definition: hpd_httpd.h:110
enum hpd_httpd_method hpd_httpd_method_t
struct ev_loop hpd_ev_loop_t
Definition: hpd_types.h:51
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.
hpd_httpd_data_f on_req_url
Definition: hpd_httpd.h:105
hpd_httpd_data_f on_req_hdr_value
Definition: hpd_httpd.h:108
struct hpd_map hpd_map_t
Definition: hpd_map.h:40
static char * req_data
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.
A http response.