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(char *revc_buf, char *name, size_t len)
79 char *p = strstr(revc_buf, "GET");
82 //skip the "/" in get string request
83 p += sizeof("GET") + 1;
84 for (size_t i = 0; *p != ' '; i++,p++)
93 static http_gci_handler_t cgi_search(const char *name, HttpCGI *table)
95 for (int i = 0; table[i].name; i++)
97 if (!strcmp(table[i].name, name))
98 return table[i].handler;
103 static uint8_t tx_buf[2048];
104 static char file_name[80];
106 void http_server(struct netconn *server, struct HttpCGI *table)
108 // SD fat filesystem context
112 struct netconn *client;
113 struct netbuf *rx_buf_conn;
118 client = netconn_accept(server);
122 rx_buf_conn = netconn_recv(client);
125 netbuf_data(rx_buf_conn, (void **)&rx_buf, &len);
128 memset(file_name, 0, sizeof(file_name));
129 get_fileName(rx_buf, file_name, sizeof(file_name));
131 LOG_INFO("Search %s\n", file_name);
132 if (strlen(file_name) == 0)
133 strcpy(file_name, HTTP_DEFAULT_PAGE);
135 http_gci_handler_t cgi = cgi_search(file_name, table);
140 else if (SD_CARD_PRESENT())
142 bool sd_ok = sd_init(&sd, NULL, 0);
145 LOG_INFO("Mount FAT filesystem.\n");
146 result = f_mount(0, &fs);
149 LOG_ERR("Mounting FAT volumes error[%d]\n", result);
156 result = fatfile_open(&in_file, file_name, FA_OPEN_EXISTING | FA_READ);
161 LOG_INFO("Opened file '%s' size %ld\n", file_name, in_file.fat_file.fsize);
165 while (count < in_file.fat_file.fsize)
167 int len = kfile_read(&in_file.fd, tx_buf, sizeof(tx_buf));
168 netconn_write(client, tx_buf, len, NETCONN_COPY);
172 kfile_flush(&in_file.fd);
173 kfile_close(&in_file.fd);
175 LOG_INFO("Sent: %d\n", count);
179 LOG_ERR("Unable to open file: '%s' error[%d]\n", file_name, result);
180 http_sendFileNotFound(client);
181 netconn_write(client, http_file_not_found, http_file_not_found_len - 1, NETCONN_NOCOPY);
186 LOG_INFO("Umount FAT filesystem.\n");
190 http_sendFileNotFound(client);
191 netconn_write(client, http_sd_not_present, http_sd_not_present_len, NETCONN_NOCOPY);
194 netconn_close(client);
195 netbuf_delete(rx_buf_conn);
197 netconn_delete(client);