Add comments.
[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 <stdio.h>
56 #include <string.h>
57
58
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";
62
63 static HttpCGI *cgi_table;
64 static http_handler_t http_callback;
65
66 /**
67  * Send on \param client socket
68  * the 200 Ok http header
69  */
70 void http_sendOk(struct netconn *client)
71 {
72         netconn_write(client, http_html_hdr_200, sizeof(http_html_hdr_200) - 1, NETCONN_NOCOPY);
73 }
74
75
76 /**
77  * Send on \param client socket
78  * the 404 File not found http header
79  */
80 void http_sendFileNotFound(struct netconn *client)
81 {
82         netconn_write(client, http_html_hdr_404, sizeof(http_html_hdr_404) - 1, NETCONN_NOCOPY);
83 }
84
85 /**
86  * Send on \param client socket
87  * the 500 internal server error http header
88  */
89 void http_sendInternalErr(struct netconn *client)
90 {
91         netconn_write(client, http_html_hdr_500, sizeof(http_html_hdr_500) - 1, NETCONN_NOCOPY);
92 }
93
94 static void get_fileName(const char *revc_buf, size_t recv_len, char *name, size_t len)
95 {
96         int i = 0;
97         char *p = strstr(revc_buf, "GET");
98         if (p)
99         {
100                 //Find the end of the page request.
101                 char *stop = strstr(revc_buf, "HTTP");
102                 if (!stop)
103                 {
104                         LOG_ERR("Bad GET request\n");
105                         name[0] = '\0';
106                         return;
107                 }
108
109                 //skip the "/" in get string request
110                 p += sizeof("GET") + 1;
111
112                 while (p != stop)
113                 {
114                         if ((size_t)i == len || (size_t)i >= recv_len)
115                         {
116                                 name[i] = '\0';
117                                 break;
118                         }
119
120                         name[i++] = *(p++);
121                 }
122         }
123
124         //Trail white space in the string.
125         while ( --i >= 0 )
126                 if (name[i] != ' ' && name[i] != '\t' && name[i] != '\n')
127                         break;
128
129         name[i + 1] = '\0';
130 }
131
132 INLINE const char *get_ext(const char *name)
133 {
134         const char *ext = strstr(name, ".");
135         if(ext && (ext + 1))
136                 return (ext + 1);
137
138         return NULL;
139 }
140
141 static http_handler_t cgi_search(const char *name,  HttpCGI *table)
142 {
143         if (!table)
144                 return NULL;
145
146         int i = 0;
147         const char *ext = get_ext(name);
148         LOG_INFO("EXT %s\n", ext);
149         while(table[i].name)
150         {
151                 if (ext && table[i].type == CGI_MATCH_EXT)
152                 {
153                         LOG_INFO("Match all ext %s\n", ext);
154                         if (!strcmp(table[i].name, ext))
155                                 break;
156                 }
157                 else /* (table[i].type == CGI_MATCH_NAME) */
158                 {
159                         LOG_INFO("Match all name %s\n", name);
160                         if (!strcmp(table[i].name, name))
161                                 break;
162                 }
163
164                 i++;
165         }
166
167         return table[i].handler;
168 }
169
170 static char req_string[80];
171
172 /**
173  * Http polling function.
174  *
175  * Call this functions to process each client connections.
176  *
177  */
178 void http_poll(struct netconn *server)
179 {
180         struct netconn *client;
181         struct netbuf *rx_buf_conn;
182         char *rx_buf;
183         uint16_t len;
184
185         client = netconn_accept(server);
186         if (!client)
187                 return;
188
189         rx_buf_conn = netconn_recv(client);
190         if (rx_buf_conn)
191         {
192                 netbuf_data(rx_buf_conn, (void **)&rx_buf, &len);
193                 if (rx_buf)
194                 {
195                         memset(req_string, 0, sizeof(req_string));
196                         get_fileName(rx_buf, len, req_string, sizeof(req_string));
197
198                         LOG_INFO("Search %s\n", req_string);
199                         if (req_string[0] == '\0')
200                                 strcpy(req_string, HTTP_DEFAULT_PAGE);
201
202                         http_handler_t cgi = cgi_search(req_string, cgi_table);
203                         if (cgi)
204                         {
205                                 if (cgi(client, req_string, rx_buf, len) < 0)
206                                 {
207                                         LOG_ERR("Internal server error\n");
208                                         http_sendInternalErr(client);
209                                         netconn_write(client, http_server_error, http_server_error_len - 1, NETCONN_NOCOPY);
210                                 }
211                         }
212                         else
213                         {
214                                 http_callback(client, req_string, rx_buf, len);
215                         }
216                 }
217                 netconn_close(client);
218                 netbuf_delete(rx_buf_conn);
219         }
220         netconn_delete(client);
221 }
222
223 /**
224  * Init the http server.
225  *
226  * The simply http server call for each client request the default_callback function. The
227  * user should define this callback to manage the client request, i.e. reading site's page
228  * from SD card. The user can define the cgi_table, where associate one callback to the user string.
229  * In this way the user could filter some client request and redirect they to custom callback, i.e.
230  * the client could request status of the device only loading the particular page name.
231  *
232  * \param default_callback fuction that server call for all request, that does'nt match cgi table.
233  * \param table of callcack to call when client request a particular page.
234  */
235 void http_init(http_handler_t default_callback, struct HttpCGI *table)
236 {
237         ASSERT(default_callback);
238
239         cgi_table = table;
240         http_callback = default_callback;
241 }
242