Check if the return buffer is biggest enough.
[bertos.git] / bertos / net / http.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \author Daniele Basile <asterix@develer.com>
34  *
35  * \brief Simple Http server.
36  *
37  * This simple web server read the site's pages from SD card, and manage
38  * the cases where SD is not present or page not found, using embedded pages.
39  * Quering from browser the /status page, the server return a json dictionary where are store
40  * some board status info, like board temperature, up-time, etc.
41  *
42  * notest: avr
43  */
44
45 #include "http.h"
46
47 #include "hw/hw_sd.h"
48 #include "hw/hw_http.h"
49
50 #include "cfg/cfg_http.h"
51
52 // Define logging setting (for cfg/log.h module).
53 #define LOG_LEVEL         HTTP_LOG_LEVEL
54 #define LOG_VERBOSITY     HTTP_LOG_FORMAT
55 #include <cfg/log.h>
56
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60
61
62 static const char http_html_hdr_200[] = "HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n";
63 static const char http_html_hdr_404[] = "HTTP/1.1 404 Not Found\r\nContent-type: text/html\r\n\r\n";
64 static const char http_html_hdr_500[] = "HTTP/1.1 500 Internal Server Error\r\nContent-type: text/html\r\n\r\n";
65
66 static HttpCGI *cgi_table;
67 static http_handler_t http_callback;
68
69
70 static char http_hexToAscii(char first, char second)
71 {
72         char hex[5], *stop;
73         hex[0] = '0';
74         hex[1] = 'x';
75         hex[2] = first;
76         hex[3] = second;
77         hex[4] = 0;
78         return strtol(hex, &stop, 16);
79 }
80
81 void http_decodeUri(const char *raw_buf, size_t raw_len, char *decodec_buf, size_t len)
82 {
83         char value;
84         for (size_t i = 0; i < raw_len; i++)
85         {
86                 if (!len)
87                         return;
88
89                 if (raw_buf[i] == '%')
90                 {
91                         if (i + 2 < raw_len)
92                         {
93                                 /* convert hex value after % */
94                                 value = http_hexToAscii(raw_buf[i + 1], raw_buf[i + 2]);
95                                 if (value)
96                                 {
97                                         *decodec_buf++ = value;
98                                         len--;
99                                         i += 2;
100                                         continue;
101                                 }
102                         }
103                 }
104                 *decodec_buf++ = raw_buf[i];
105                 len--;
106         }
107 }
108
109 void http_getPageName(const char *recv_buf, size_t recv_len, char *page_name, size_t len)
110 {
111         int i = 0;
112         bool str_ok = false;
113
114         if (recv_buf && (recv_len > sizeof("GET /")))
115         {
116                 if (*recv_buf++ == 'G' &&
117                         *recv_buf++ == 'E' && *recv_buf++ == 'T')
118                         {
119                                 str_ok = true;
120                                 /* skip the space and "/" */
121                                 recv_buf += 2;
122                         }
123         }
124
125         if (str_ok)
126         {
127                 while ((size_t)i < recv_len)
128                 {
129                         char ch = *(recv_buf++);
130                         if (ch == ' ' || ch == '\t' || ch == '\n')
131                                 break;
132                         if((size_t)i == len - 1)
133                                 break;
134                         page_name[i++] = ch;
135                 }
136         }
137
138         page_name[i] = '\0';
139 }
140
141 INLINE const char *get_ext(const char *name)
142 {
143         const char *ext = strstr(name, ".");
144         if(ext && (ext + 1))
145                 return (ext + 1);
146
147         return NULL;
148 }
149
150
151 /**
152  * Send on \param client socket
153  * the 200 Ok http header
154  */
155 void http_sendOk(struct netconn *client)
156 {
157         netconn_write(client, http_html_hdr_200, sizeof(http_html_hdr_200) - 1, NETCONN_NOCOPY);
158 }
159
160
161 /**
162  * Send on \param client socket
163  * the 404 File not found http header
164  */
165 void http_sendFileNotFound(struct netconn *client)
166 {
167         netconn_write(client, http_html_hdr_404, sizeof(http_html_hdr_404) - 1, NETCONN_NOCOPY);
168 }
169
170 /**
171  * Send on \param client socket
172  * the 500 internal server error http header
173  */
174 void http_sendInternalErr(struct netconn *client)
175 {
176         netconn_write(client, http_html_hdr_500, sizeof(http_html_hdr_500) - 1, NETCONN_NOCOPY);
177 }
178
179 static http_handler_t cgi_search(const char *name,  HttpCGI *table)
180 {
181         if (!table)
182                 return NULL;
183
184         int i = 0;
185         const char *ext = get_ext(name);
186         LOG_INFO("EXT %s\n", ext);
187         while(table[i].name)
188         {
189                 if (ext && table[i].type == CGI_MATCH_EXT)
190                 {
191                         LOG_INFO("Match all ext %s\n", ext);
192                         if (!strcmp(table[i].name, ext))
193                                 break;
194                 }
195                 else if (table[i].type == CGI_MATCH_NAME)
196                 {
197                         LOG_INFO("Match all name %s\n", name);
198                         if (strstr(name, table[i].name) != NULL)
199                                 break;
200                 }
201                 else /* (table[i].type == CGI_MATCH_WORD) */
202                 {
203                         LOG_INFO("Match all word %s\n", name);
204                         if (!strcmp(table[i].name, name))
205                                 break;
206                 }
207
208                 i++;
209         }
210
211         return table[i].handler;
212 }
213
214 static char req_string[80];
215
216 /**
217  * Http polling function.
218  *
219  * Call this functions to process each client connections.
220  *
221  */
222 void http_poll(struct netconn *server)
223 {
224         struct netconn *client;
225         struct netbuf *rx_buf_conn;
226         char *rx_buf;
227         uint16_t len;
228
229         client = netconn_accept(server);
230         if (!client)
231                 return;
232
233         rx_buf_conn = netconn_recv(client);
234         if (rx_buf_conn)
235         {
236                 netbuf_data(rx_buf_conn, (void **)&rx_buf, &len);
237                 if (rx_buf)
238                 {
239                         memset(req_string, 0, sizeof(req_string));
240                         http_getPageName(rx_buf, len, req_string, sizeof(req_string));
241
242                         LOG_INFO("Search %s\n", req_string);
243                         if (req_string[0] == '\0')
244                                 strcpy(req_string, HTTP_DEFAULT_PAGE);
245
246                         http_handler_t cgi = cgi_search(req_string, cgi_table);
247                         if (cgi)
248                         {
249                                 if (cgi(client, req_string, rx_buf, len) < 0)
250                                 {
251                                         LOG_ERR("Internal server error\n");
252                                         http_sendInternalErr(client);
253                                         netconn_write(client, http_server_error, http_server_error_len - 1, NETCONN_NOCOPY);
254                                 }
255                         }
256                         else
257                         {
258                                 http_callback(client, req_string, rx_buf, len);
259                         }
260                 }
261                 netconn_close(client);
262                 netbuf_delete(rx_buf_conn);
263         }
264         netconn_delete(client);
265 }
266
267 /**
268  * Init the http server.
269  *
270  * The simply http server call for each client request the default_callback function. The
271  * user should define this callback to manage the client request, i.e. reading site's page
272  * from SD card. The user can define the cgi_table, where associate one callback to the user string.
273  * In this way the user could filter some client request and redirect they to custom callback, i.e.
274  * the client could request status of the device only loading the particular page name.
275  *
276  * \param default_callback fuction that server call for all request, that does'nt match cgi table.
277  * \param table of callcack to call when client request a particular page.
278  */
279 void http_init(http_handler_t default_callback, struct HttpCGI *table)
280 {
281         ASSERT(default_callback);
282
283         cgi_table = table;
284         http_callback = default_callback;
285 }
286