Fix find name function. Pass as argument to cgi callback the rx buffer len.
[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(const char *revc_buf, size_t recv_len, char *name, size_t len)
78 {
79         int i = 0;
80         char *p = strstr(revc_buf, "GET");
81         if (p)
82         {
83                 //Find the end of the page request.
84                 char *stop = strstr(revc_buf, "HTTP");
85                 if (!stop)
86                 {
87                         LOG_ERR("Bad GET request\n");
88                         name[0] = '\0';
89                         return;
90                 }
91
92                 //skip the "/" in get string request
93                 p += sizeof("GET") + 1;
94
95                 while (p != stop)
96                 {
97                         if ((size_t)i == len || (size_t)i >= recv_len)
98                         {
99                                 name[i] = '\0';
100                                 break;
101                         }
102
103                         name[i++] = *(p++);
104                 }
105         }
106
107         //Trail white space in the string.
108         while ( --i >= 0 )
109                 if (name[i] != ' ' && name[i] != '\t' && name[i] != '\n')
110                         break;
111
112         name[i + 1] = '\0';
113 }
114
115 static http_gci_handler_t cgi_search(const char *name,  HttpCGI *table)
116 {
117         for (int i = 0; table[i].name; i++)
118         {
119                 if (!strcmp(table[i].name, name))
120                         return table[i].handler;
121         }
122         return NULL;
123 }
124
125 static uint8_t tx_buf[2048];
126 static char file_name[80];
127
128 void http_server(struct netconn *server, struct HttpCGI *table)
129 {
130         // SD fat filesystem context
131         Sd sd;
132         FATFS fs;
133         FatFile in_file;
134         struct netconn *client;
135         struct netbuf *rx_buf_conn;
136         char *rx_buf;
137         u16_t len;
138         FRESULT result;
139
140         client = netconn_accept(server);
141         if (!client)
142                 return;
143
144         rx_buf_conn = netconn_recv(client);
145         if (rx_buf_conn)
146         {
147                 netbuf_data(rx_buf_conn, (void **)&rx_buf, &len);
148
149                 if (rx_buf)
150                 {
151                         memset(file_name, 0, sizeof(file_name));
152                         get_fileName(rx_buf, len, file_name, sizeof(file_name));
153
154                         LOG_INFO("Search %s\n", file_name);
155                         if (file_name[0] == '\0')
156                                 strcpy(file_name, HTTP_DEFAULT_PAGE);
157
158                         http_gci_handler_t cgi = cgi_search(file_name,  table);
159                         if (cgi)
160                         {
161                                 cgi(client, rx_buf, len);
162                         }
163                         else if (SD_CARD_PRESENT())
164                         {
165                                 bool sd_ok = sd_init(&sd, NULL, 0);
166                                 if (sd_ok)
167                                 {
168                                         LOG_INFO("Mount FAT filesystem.\n");
169                                         result = f_mount(0, &fs);
170                                         if (result != FR_OK)
171                                         {
172                                                 LOG_ERR("Mounting FAT volumes error[%d]\n", result);
173                                                 sd_ok = false;
174                                                 f_mount(0, NULL);
175                                         }
176
177                                         if (sd_ok)
178                                         {
179                                                 result = fatfile_open(&in_file, file_name,  FA_OPEN_EXISTING | FA_READ);
180
181                                                 size_t count = 0;
182                                                 if (result == FR_OK)
183                                                 {
184                                                         LOG_INFO("Opened file '%s' size %ld\n", file_name, in_file.fat_file.fsize);
185
186                                                         http_sendOk(client);
187
188                                                         while (count < in_file.fat_file.fsize)
189                                                         {
190                                                                 int len = kfile_read(&in_file.fd, tx_buf, sizeof(tx_buf));
191                                                                 netconn_write(client, tx_buf, len, NETCONN_COPY);
192                                                                 count += len;
193                                                         }
194
195                                                         kfile_flush(&in_file.fd);
196                                                         kfile_close(&in_file.fd);
197
198                                                         LOG_INFO("Sent: %d\n", count);
199                                                 }
200                                                 else
201                                                 {
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);
205                                                 }
206                                         }
207                                 }
208                                 f_mount(0, NULL);
209                                 LOG_INFO("Umount FAT filesystem.\n");
210                         }
211                         else
212                         {
213                                 http_sendFileNotFound(client);
214                                 netconn_write(client, http_sd_not_present, http_sd_not_present_len, NETCONN_NOCOPY);
215                         }
216                 }
217                 netconn_close(client);
218                 netbuf_delete(rx_buf_conn);
219         }
220         netconn_delete(client);
221 }