HomePort
greeting.c
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 #include "hpd_httpd.h"
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ev.h>
33 
34 // The server instance
35 static hpd_httpd_t *server = NULL;
36 
37 // Handle correct exiting
38 static void exit_handler(int sig)
39 {
40  // Stop server
41  if (server) {
42  hpd_httpd_stop(server); // TODO Check error
43  hpd_httpd_destroy(server); // TODO Check error
44  }
45 
46  // Exit
47  printf("Exiting....\n");
48  exit(sig);
49 }
50 
51 void header_printer(void *data, const char* key, const char* value)
52 {
53  printf("\tHeader key/value = {%s : %s}\n",key,value);
54 }
55 
56 int handle_request(hpd_httpd_t *ins, hpd_httpd_request_t *req, void* ws_ctx, void** req_data)
57 {
59  hpd_httpd_request_get_method(req, &m); // TODO Error handling
60  const char *method = hpd_httpd_method_str(m);
61  const char *url = hpd_httpd_request_get_url(req, NULL);
62 
63  struct lm *headers = hpd_httpd_request_get_headers(req, NULL);
64 
65  printf("Got %s request on %s\n", method, url);
66 
67  // print headers
68  lm_map(headers, header_printer, NULL);
69 
70  char *body1 = "<html><body><h1>Hello</h1>Your language: ";
71  char *body2 = lm_find(headers, "Accept-Language");
72  if (body2 == NULL) body2 = "n/a";
73  char *body3 = "</body></html>";
74  char *body = malloc((strlen(body1)+strlen(body2)+strlen(body3)+1)*sizeof(char));
75  sprintf(body, "%s%s%s",body1, body2, body3);
76 
77  hpd_httpd_response_t *res = hpd_httpd_response_create(NULL, req, WS_HTTP_200);
78  hpd_httpd_response_sendf(res, body);
79 
80  free(body);
81 
82  return 0;
83 }
84 
85 // Main function
86 int main(int argc, char *argv[])
87 {
88  // The event loop for the webserver to run on
89  hpd_ev_loop_t *loop = EV_DEFAULT;
90 
91  // Set settings for the webserver
93  settings.on_req_cmpl = handle_request;
94  settings.port = HPD_TCPD_P_HTTP_ALT;
95 
96  // Inform if we have been built with debug flag
97 #ifdef DEBUG
98  printf("Debugging is set\n");
99 #endif
100 
101  // Register signals for correctly exiting
102  signal(SIGINT, exit_handler);
103  signal(SIGTERM, exit_handler);
104 
105  // Create server
106  // TODO Parameter cannot be null !
107  hpd_httpd_create(&server, &settings, NULL, loop); // TODO Check error
108 
109  // Start the event loop and webserver
110  if (!hpd_httpd_start(server))
111  ev_run(loop, 0);
112 
113  // Exit
114  exit_handler(0);
115  return 0;
116 }
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.
void header_printer(void *data, const char *key, const char *value)
Definition: greeting.c:51
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.
static hpd_httpd_t * server
Definition: greeting.c:35
hpd_error_t hpd_httpd_request_get_url(hpd_httpd_request_t *req, const char **url)
Get the URL of this request.
char * url
free(data.url)
struct hp_settings settings
hpd_tcpd_port_t port
Definition: hpd_httpd.h:101
An http request.
data key
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.
int handle_request(hpd_httpd_t *ins, hpd_httpd_request_t *req, void *ws_ctx, void **req_data)
Definition: greeting.c:56
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.
Settings struct for webserver.
Definition: hpd_httpd.h:100
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
int main(int argc, char *argv[])
Definition: greeting.c:86
hpd_error_t hpd_httpd_start(hpd_httpd_t *httpd)
Start a httpd instance.
Definition: httpd.c:196
hpd_httpd_nodata_f on_req_cmpl
Definition: hpd_httpd.h:111
httpd instance struct
Definition: httpd.c:38
#define HPD_HTTPD_SETTINGS_DEFAULT
Default settings for httpd.
Definition: hpd_httpd.h:123
enum hpd_httpd_method hpd_httpd_method_t
struct ev_loop hpd_ev_loop_t
Definition: hpd_types.h:51
static void exit_handler(int sig)
Definition: greeting.c:38
static char * req_data
A http response.