netconn_write(client, http_html_hdr_500, sizeof(http_html_hdr_500) - 1, NETCONN_NOCOPY);
}
-static void get_fileName(const char *revc_buf, size_t recv_len, char *name, size_t len)
+void http_getPageName(const char *revc_buf, size_t recv_len, char *page_name, size_t len)
{
int i = 0;
- char *p = strstr(revc_buf, "GET");
- if (p)
- {
- /* Find the end of the page request. */
- char *stop = strstr(revc_buf, "HTTP");
- if (!stop)
- {
- LOG_ERR("Bad GET request\n");
- name[0] = '\0';
- return;
- }
-
- /* skip the "/" in get string request */
- p += sizeof("GET") + 1;
+ bool str_ok = false;
- while (p != stop)
- {
- if ((size_t)i == len || (size_t)i >= recv_len)
+ if (revc_buf && (recv_len > sizeof("GET /")))
+ {
+ if (*revc_buf++ == 'G' &&
+ *revc_buf++ == 'E' && *revc_buf++ == 'T')
{
- name[i] = '\0';
- break;
+ str_ok = true;
+ revc_buf += 2;
}
+ }
- name[i++] = *(p++);
+ if (str_ok)
+ {
+ while ((size_t)i < recv_len)
+ {
+ char ch = *(revc_buf++);
+ if (ch == ' ' || ch == '\t' || ch == '\n')
+ break;
+ if((size_t)i == len - 1)
+ break;
+ page_name[i++] = ch;
}
}
- /* Trail white space in the string. */
- while ( --i >= 0 )
- if (name[i] != ' ' && name[i] != '\t' && name[i] != '\n')
- break;
-
- name[i + 1] = '\0';
+ page_name[i] = '\0';
}
INLINE const char *get_ext(const char *name)
if (rx_buf)
{
memset(req_string, 0, sizeof(req_string));
- get_fileName(rx_buf, len, req_string, sizeof(req_string));
+ http_getPageName(rx_buf, len, req_string, sizeof(req_string));
LOG_INFO("Search %s\n", req_string);
if (req_string[0] == '\0')
#ifndef NET_HTTP_H
#define NET_HTTP_H
-
-#include <netif/ethernetif.h>
-
-#include <lwip/ip.h>
-#include <lwip/ip_addr.h>
-#include <lwip/netif.h>
#include <lwip/tcpip.h>
-#include <lwip/dhcp.h>
typedef int (*http_handler_t)(struct netconn *client, const char *name, char *revc_buf, size_t revc_len);
#define CGI_MATCH_NAME 1 ///< Select item in table only if string match
#define CGI_MATCH_EXT 2 ///< Select item in table if the extention match
+void http_getPageName(const char *revc_buf, size_t recv_len, char *page_name, size_t len);
+
void http_sendOk(struct netconn *client);
void http_sendFileNotFound(struct netconn *client);
void http_sendInternalErr(struct netconn *client);
void http_poll(struct netconn *server);
void http_init(http_handler_t default_callback, struct HttpCGI *table);
+int http_testSetup(void);
+int http_testRun(void);
+int http_testTearDown(void);
+
#endif /* NET_HTTP_H */
--- /dev/null
+/**
+ * \file
+ * <!--
+ * This file is part of BeRTOS.
+ *
+ * Bertos is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * As a special exception, you may use this file as part of a free software
+ * library without restriction. Specifically, if other files instantiate
+ * templates or use macros or inline functions from this file, or you compile
+ * this file and link it with other files to produce an executable, this
+ * file does not by itself cause the resulting executable to be covered by
+ * the GNU General Public License. This exception does not however
+ * invalidate any other reasons why the executable file might be covered by
+ * the GNU General Public License.
+ *
+ * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
+ *
+ * -->
+ *
+ * \brief HTTP Server test
+ *
+ * \author Daniele Basile <asterix@develer.com>
+ */
+
+#include <cfg/compiler.h>
+#include <cfg/test.h>
+#include <cfg/debug.h>
+
+#include <net/http.h>
+
+#include <string.h>
+
+static const char get_str[] = "\
+GET /test/page1 HTTP/1.1 Host: 10.3.3.199 Connection: keep-alive \
+User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.1 \
+(KHTML, like Gecko) Chrome/14.0.835.186 Safari/535.1 \
+Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 \
+Accept-Encoding: gzip,deflate,sdch Accept-Language: it-IT,it;q=0.8,en-US;\
+q=0.6,en;q=0.4 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3";
+
+
+static const char get_str1[] = "\
+GET /test/page1";
+
+static const char get_str2[] = "\
+GET ";
+
+static const char get_str3[] = "\
+GAT /test/page1 HTTP/1.1 Host: 10.3.3.199 Connection: keep-alive \
+User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.1 \
+(KHTML, like Gecko) Chrome/14.0.835.186 Safari/535.1 \
+Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 \
+Accept-Encoding: gzip,deflate,sdch Accept-Language: it-IT,it;q=0.8,en-US;\
+q=0.6,en;q=0.4 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3";
+
+int http_testSetup(void)
+{
+ kdbg_init();
+ return 0;
+}
+
+int http_testRun(void)
+{
+ char name[80];
+ memset(name, 0, sizeof(name));
+ http_getPageName(get_str, sizeof(get_str), name, sizeof(name));
+
+ if (!strcmp("/test/page1", name))
+ goto error;
+
+ http_getPageName(get_str1, sizeof(get_str1), name, sizeof(name));
+
+ kprintf("1 name %s\n", name);
+ if (!strcmp("/test/page1", name))
+ goto error;
+
+ http_getPageName(get_str2, sizeof(get_str2), name, sizeof(name));
+
+ kprintf("2 name %s\n", name);
+ if (name[0] != '\0')
+ goto error;
+
+ http_getPageName(get_str2, sizeof(get_str2), name, sizeof(name));
+
+ kprintf("3 name %s\n", name);
+ if (name[0] != '\0')
+ goto error;
+
+ return 0;
+
+error:
+ kprintf("Error!\n");
+ return -1;
+}
+
+int http_testTearDown(void)
+{
+ return 0;
+}
+
+TEST_MAIN(http);