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
63 static const char http_html_hdr_200[] = "HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n";
64 static const char http_html_hdr_404[] = "HTTP/1.1 404 Not Found\r\nContent-type: text/html\r\n\r\n";
66 void http_sendOk(struct netconn *client)
68 netconn_write(client, http_html_hdr_200, sizeof(http_html_hdr_200) - 1, NETCONN_NOCOPY);
71 void http_sendFileNotFound(struct netconn *client)
73 netconn_write(client, http_html_hdr_404, sizeof(http_html_hdr_404) - 1, NETCONN_NOCOPY);
77 static void get_fileName(const char *revc_buf, size_t recv_len, char *name, size_t len)
80 char *p = strstr(revc_buf, "GET");
83 //Find the end of the page request.
84 char *stop = strstr(revc_buf, "HTTP");
87 LOG_ERR("Bad GET request\n");
92 //skip the "/" in get string request
93 p += sizeof("GET") + 1;
97 if ((size_t)i == len || (size_t)i >= recv_len)
107 //Trail white space in the string.
109 if (name[i] != ' ' && name[i] != '\t' && name[i] != '\n')
115 static http_gci_handler_t cgi_search(const char *name, HttpCGI *table)
117 for (int i = 0; table[i].name; i++)
119 if (!strcmp(table[i].name, name))
120 return table[i].handler;
125 static uint8_t tx_buf[2048];
126 static char file_name[80];
128 void http_server(struct netconn *server, struct HttpCGI *table)
130 // SD fat filesystem context
134 struct netconn *client;
135 struct netbuf *rx_buf_conn;
140 client = netconn_accept(server);
144 rx_buf_conn = netconn_recv(client);
147 netbuf_data(rx_buf_conn, (void **)&rx_buf, &len);
151 memset(file_name, 0, sizeof(file_name));
152 get_fileName(rx_buf, len, file_name, sizeof(file_name));
154 LOG_INFO("Search %s\n", file_name);
155 if (file_name[0] == '\0')
156 strcpy(file_name, HTTP_DEFAULT_PAGE);
158 http_gci_handler_t cgi = cgi_search(file_name, table);
161 cgi(client, rx_buf, len);
163 else if (SD_CARD_PRESENT())
165 bool sd_ok = sd_init(&sd, NULL, 0);
168 LOG_INFO("Mount FAT filesystem.\n");
169 result = f_mount(0, &fs);
172 LOG_ERR("Mounting FAT volumes error[%d]\n", result);
179 result = fatfile_open(&in_file, file_name, FA_OPEN_EXISTING | FA_READ);
184 LOG_INFO("Opened file '%s' size %ld\n", file_name, in_file.fat_file.fsize);
188 while (count < in_file.fat_file.fsize)
190 int len = kfile_read(&in_file.fd, tx_buf, sizeof(tx_buf));
191 netconn_write(client, tx_buf, len, NETCONN_COPY);
195 kfile_flush(&in_file.fd);
196 kfile_close(&in_file.fd);
198 LOG_INFO("Sent: %d\n", count);
202 LOG_ERR("Unable to open file: '%s' error[%d]\n", file_name, result);
203 http_sendFileNotFound(client);
204 netconn_write(client, http_file_not_found, http_file_not_found_len - 1, NETCONN_NOCOPY);
209 LOG_INFO("Umount FAT filesystem.\n");
213 http_sendFileNotFound(client);
214 netconn_write(client, http_sd_not_present, http_sd_not_present_len, NETCONN_NOCOPY);
217 netconn_close(client);
218 netbuf_delete(rx_buf_conn);
220 netconn_delete(client);