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.
46 #include "hw/hw_http.h"
48 #include "cfg/cfg_http.h"
50 // Define logging setting (for cfg/log.h module).
51 #define LOG_LEVEL HTTP_LOG_LEVEL
52 #define LOG_VERBOSITY HTTP_LOG_FORMAT
59 static const char http_html_hdr_200[] = "HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n";
60 static const char http_html_hdr_404[] = "HTTP/1.1 404 Not Found\r\nContent-type: text/html\r\n\r\n";
61 static const char http_html_hdr_500[] = "HTTP/1.1 500 Internal Server Error\r\nContent-type: text/html\r\n\r\n";
63 static HttpCGI *cgi_table;
64 static http_handler_t http_callback;
67 void http_sendOk(struct netconn *client)
69 netconn_write(client, http_html_hdr_200, sizeof(http_html_hdr_200) - 1, NETCONN_NOCOPY);
72 void http_sendFileNotFound(struct netconn *client)
74 netconn_write(client, http_html_hdr_404, sizeof(http_html_hdr_404) - 1, NETCONN_NOCOPY);
77 void http_sendInternalErr(struct netconn *client)
79 netconn_write(client, http_html_hdr_500, sizeof(http_html_hdr_500) - 1, NETCONN_NOCOPY);
82 static void get_fileName(const char *revc_buf, size_t recv_len, char *name, size_t len)
85 char *p = strstr(revc_buf, "GET");
88 //Find the end of the page request.
89 char *stop = strstr(revc_buf, "HTTP");
92 LOG_ERR("Bad GET request\n");
97 //skip the "/" in get string request
98 p += sizeof("GET") + 1;
102 if ((size_t)i == len || (size_t)i >= recv_len)
112 //Trail white space in the string.
114 if (name[i] != ' ' && name[i] != '\t' && name[i] != '\n')
120 INLINE const char *get_ext(const char *name)
122 const char *ext = strstr(name, ".");
129 static http_handler_t cgi_search(const char *name, HttpCGI *table)
135 const char *ext = get_ext(name);
136 LOG_INFO("EXT %s\n", ext);
139 if (ext && table[i].type == CGI_MATCH_EXT)
141 LOG_INFO("Match all ext %s\n", ext);
142 if (!strcmp(table[i].name, ext))
145 else /* (table[i].type == CGI_MATCH_NAME) */
147 LOG_INFO("Match all name %s\n", name);
148 if (!strcmp(table[i].name, name))
155 return table[i].handler;
158 static char req_string[80];
160 void http_poll(struct netconn *server)
162 struct netconn *client;
163 struct netbuf *rx_buf_conn;
167 client = netconn_accept(server);
171 rx_buf_conn = netconn_recv(client);
174 netbuf_data(rx_buf_conn, (void **)&rx_buf, &len);
177 memset(req_string, 0, sizeof(req_string));
178 get_fileName(rx_buf, len, req_string, sizeof(req_string));
180 LOG_INFO("Search %s\n", req_string);
181 if (req_string[0] == '\0')
182 strcpy(req_string, HTTP_DEFAULT_PAGE);
184 http_handler_t cgi = cgi_search(req_string, cgi_table);
187 if (cgi(client, req_string, rx_buf, len) < 0)
189 LOG_ERR("Internal server error\n");
190 http_sendInternalErr(client);
191 netconn_write(client, http_server_error, http_server_error_len - 1, NETCONN_NOCOPY);
196 http_callback(client, req_string, rx_buf, len);
199 netconn_close(client);
200 netbuf_delete(rx_buf_conn);
202 netconn_delete(client);
205 void http_init(http_handler_t default_callback, struct HttpCGI *table)
207 ASSERT(default_callback);
210 http_callback = default_callback;