4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
33 * \author Daniele Basile <asterix@develer.com>
35 * \brief Simple Http server.
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.
48 #include "hw/hw_http.h"
50 #include "cfg/cfg_http.h"
52 // Define logging setting (for cfg/log.h module).
53 #define LOG_LEVEL HTTP_LOG_LEVEL
54 #define LOG_VERBOSITY HTTP_LOG_FORMAT
62 static const char http_html_hdr_200[] = "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n";
63 static const char http_html_hdr_404[] = "HTTP/1.0 404 Not Found\r\nContent-type: text/html\r\n\r\n";
64 static const char http_html_hdr_500[] = "HTTP/1.0 500 Internal Server Error\r\nContent-type: text/html\r\n\r\n";
66 static HttpCGI *cgi_table;
67 static http_handler_t http_callback;
70 * Get key value from tokenized buffer
72 int http_getValue(char *tolenized_buf, size_t tolenized_buf_len, const char *key, char *value, size_t len)
74 if (!tolenized_buf || !key || !value)
77 char *p = tolenized_buf;
80 memset(value, 0, len);
82 for (size_t i = 0; i < tolenized_buf_len; i++)
87 size_t jump = strlen(p) + 1;
90 value_len = strlen(p);
97 /* jump to next pair */
107 int http_tokenizeGetRequest(char *raw_buf, size_t raw_len)
111 for(size_t i = 0; (i < raw_len) && raw_buf; i++)
113 if (raw_buf[i] == '&')
119 if (raw_buf[i] == '=')
126 static char http_hexToAscii(char first, char second)
134 return strtol(hex, &stop, 16);
137 void http_decodeUri(const char *raw_buf, size_t raw_len, char *decodec_buf, size_t len)
140 for (size_t i = 0; i < raw_len; i++)
145 if (raw_buf[i] == '%')
149 /* convert hex value after % */
150 value = http_hexToAscii(raw_buf[i + 1], raw_buf[i + 2]);
153 *decodec_buf++ = value;
155 /* decoded two digit of hex value, go to next value*/
161 *decodec_buf++ = raw_buf[i];
166 void http_getPageName(const char *recv_buf, size_t recv_len, char *page_name, size_t len)
170 const char *p = recv_buf;
171 if (p && (recv_len > sizeof("GET /")))
174 *p++ == 'E' && *p++ == 'T')
177 /* skip the space and "/" */
184 while ((size_t)i < recv_len)
187 if (ch == ' ' || ch == '\t' || ch == '\n')
189 if((size_t)i == len - 1)
198 INLINE const char *get_ext(const char *name)
200 const char *ext = strstr(name, ".");
209 * Send on \param client socket
210 * the 200 Ok http header
212 void http_sendOk(struct netconn *client)
214 netconn_write(client, http_html_hdr_200, sizeof(http_html_hdr_200) - 1, NETCONN_NOCOPY);
219 * Send on \param client socket
220 * the 404 File not found http header
222 void http_sendFileNotFound(struct netconn *client)
224 netconn_write(client, http_html_hdr_404, sizeof(http_html_hdr_404) - 1, NETCONN_NOCOPY);
228 * Send on \param client socket
229 * the 500 internal server error http header
231 void http_sendInternalErr(struct netconn *client)
233 netconn_write(client, http_html_hdr_500, sizeof(http_html_hdr_500) - 1, NETCONN_NOCOPY);
236 static http_handler_t cgi_search(const char *name, HttpCGI *table)
242 const char *ext = get_ext(name);
243 LOG_INFO("EXT %s\n", ext);
246 if (ext && table[i].type == CGI_MATCH_EXT)
248 LOG_INFO("Match all ext %s\n", ext);
249 if (!strcmp(table[i].name, ext))
252 else if (table[i].type == CGI_MATCH_NAME)
254 LOG_INFO("Match all name %s\n", name);
255 if (strstr(name, table[i].name) != NULL)
258 else /* (table[i].type == CGI_MATCH_WORD) */
260 LOG_INFO("Match all word %s\n", name);
261 if (!strcmp(table[i].name, name))
268 return table[i].handler;
271 static char req_string[80];
274 * Http polling function.
276 * Call this functions to process each client connections.
279 void http_poll(struct netconn *server)
281 struct netconn *client;
282 struct netbuf *rx_buf_conn;
286 client = netconn_accept(server);
290 rx_buf_conn = netconn_recv(client);
293 netbuf_data(rx_buf_conn, (void **)&rx_buf, &len);
296 memset(req_string, 0, sizeof(req_string));
297 http_getPageName(rx_buf, len, req_string, sizeof(req_string));
299 LOG_INFO("Search %s\n", req_string);
300 if (req_string[0] == '\0')
301 strcpy(req_string, HTTP_DEFAULT_PAGE);
303 http_handler_t cgi = cgi_search(req_string, cgi_table);
306 if (cgi(client, req_string, rx_buf, len) < 0)
308 LOG_ERR("Internal server error\n");
309 http_sendInternalErr(client);
310 netconn_write(client, http_server_error, http_server_error_len - 1, NETCONN_NOCOPY);
315 http_callback(client, req_string, rx_buf, len);
318 netconn_close(client);
319 netbuf_delete(rx_buf_conn);
321 netconn_delete(client);
325 * Init the http server.
327 * The simply http server call for each client request the default_callback function. The
328 * user should define this callback to manage the client request, i.e. reading site's page
329 * from SD card. The user can define the cgi_table, where associate one callback to the user string.
330 * In this way the user could filter some client request and redirect they to custom callback, i.e.
331 * the client could request status of the device only loading the particular page name.
333 * \param default_callback fuction that server call for all request, that does'nt match cgi table.
334 * \param table of callcack to call when client request a particular page.
336 void http_init(http_handler_t default_callback, struct HttpCGI *table)
338 ASSERT(default_callback);
341 http_callback = default_callback;