235401cda382f949aa438471bbba0aaab87b2d51
[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
43 #include "http.h"
44
45 #include "hw/hw_sd.h"
46 #include "hw/hw_http.h"
47
48 #include "cfg/cfg_http.h"
49
50 // Define logging setting (for cfg/log.h module).
51 #define LOG_LEVEL         HTTP_LOG_LEVEL
52 #define LOG_VERBOSITY     HTTP_LOG_FORMAT
53 #include <cfg/log.h>
54
55 #include <drv/sd.h>
56
57 #include <fs/fat.h>
58
59 #include <stdio.h>
60 #include <string.h>
61
62
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";
65
66 void http_sendOk(struct netconn *client)
67 {
68         netconn_write(client, http_html_hdr_200, sizeof(http_html_hdr_200) - 1, NETCONN_NOCOPY);
69 }
70
71 void http_sendFileNotFound(struct netconn *client)
72 {
73         netconn_write(client, http_html_hdr_404, sizeof(http_html_hdr_404) - 1, NETCONN_NOCOPY);
74 }
75
76
77 static void get_fileName(char *revc_buf, char *name, size_t len)
78 {
79         char *p = strstr(revc_buf, "GET");
80         if (p)
81         {
82                 //skip the "/" in get string request
83                 p += sizeof("GET") + 1;
84                 for (size_t i = 0; *p != ' '; i++,p++)
85                 {
86                         if (i > len)
87                                 break;
88                         name[i] = *p;
89                 }
90         }
91 }
92
93 static http_gci_handler_t cgi_search(const char *name,  HttpCGI *table)
94 {
95         for (int i = 0; table[i].name; i++)
96         {
97                 if (!strcmp(table[i].name, name))
98                         return table[i].handler;
99         }
100         return NULL;
101 }
102
103 static uint8_t tx_buf[2048];
104 static char file_name[80];
105
106 void http_server(struct netconn *server, struct HttpCGI *table)
107 {
108         // SD fat filesystem context
109         Sd sd;
110         FATFS fs;
111         FatFile in_file;
112         struct netconn *client;
113         struct netbuf *rx_buf_conn;
114         char *rx_buf;
115         u16_t len;
116         FRESULT result;
117
118         client = netconn_accept(server);
119         if (!client)
120                 return;
121
122         rx_buf_conn = netconn_recv(client);
123         if (rx_buf_conn)
124         {
125                 netbuf_data(rx_buf_conn, (void **)&rx_buf, &len);
126                 if (rx_buf)
127                 {
128                         memset(file_name, 0, sizeof(file_name));
129                         get_fileName(rx_buf, file_name, sizeof(file_name));
130
131                         LOG_INFO("Search %s\n", file_name);
132                         if (strlen(file_name) == 0)
133                                 strcpy(file_name, HTTP_DEFAULT_PAGE);
134
135                         http_gci_handler_t cgi = cgi_search(file_name,  table);
136                         if (cgi)
137                         {
138                                 cgi(rx_buf, client);
139                         }
140                         else if (SD_CARD_PRESENT())
141                         {
142                                 bool sd_ok = sd_init(&sd, NULL, 0);
143                                 if (sd_ok)
144                                 {
145                                         LOG_INFO("Mount FAT filesystem.\n");
146                                         result = f_mount(0, &fs);
147                                         if (result != FR_OK)
148                                         {
149                                                 LOG_ERR("Mounting FAT volumes error[%d]\n", result);
150                                                 sd_ok = false;
151                                                 f_mount(0, NULL);
152                                         }
153
154                                         if (sd_ok)
155                                         {
156                                                 result = fatfile_open(&in_file, file_name,  FA_OPEN_EXISTING | FA_READ);
157
158                                                 size_t count = 0;
159                                                 if (result == FR_OK)
160                                                 {
161                                                         LOG_INFO("Opened file '%s' size %ld\n", file_name, in_file.fat_file.fsize);
162
163                                                         http_sendOk(client);
164
165                                                         while (count < in_file.fat_file.fsize)
166                                                         {
167                                                                 int len = kfile_read(&in_file.fd, tx_buf, sizeof(tx_buf));
168                                                                 netconn_write(client, tx_buf, len, NETCONN_COPY);
169                                                                 count += len;
170                                                         }
171
172                                                         kfile_flush(&in_file.fd);
173                                                         kfile_close(&in_file.fd);
174
175                                                         LOG_INFO("Sent: %d\n", count);
176                                                 }
177                                                 else
178                                                 {
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);
182                                                 }
183                                         }
184                                 }
185                                 f_mount(0, NULL);
186                                 LOG_INFO("Umount FAT filesystem.\n");
187                         }
188                         else
189                         {
190                                 http_sendFileNotFound(client);
191                                 netconn_write(client, http_sd_not_present, http_sd_not_present_len, NETCONN_NOCOPY);
192                         }
193                 }
194                 netconn_close(client);
195                 netbuf_delete(rx_buf_conn);
196         }
197         netconn_delete(client);
198 }