From: asterix Date: Mon, 3 Oct 2011 10:53:38 +0000 (+0000) Subject: Reformar get name funtion. Add test. X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=d603ad8d35758a54f6cb08a7f8c16f6327ea86e0;hp=00a56e3c9af3422795c598fda5e9e6ff74ca6b48;p=bertos.git Reformar get name funtion. Add test. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@5130 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/bertos/net/http.c b/bertos/net/http.c index f994079b..e3fc8085 100644 --- a/bertos/net/http.c +++ b/bertos/net/http.c @@ -93,42 +93,35 @@ void http_sendInternalErr(struct netconn *client) 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) @@ -195,7 +188,7 @@ void http_poll(struct netconn *server) 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') diff --git a/bertos/net/http.h b/bertos/net/http.h index 53d8100d..665e97aa 100644 --- a/bertos/net/http.h +++ b/bertos/net/http.h @@ -43,14 +43,7 @@ #ifndef NET_HTTP_H #define NET_HTTP_H - -#include - -#include -#include -#include #include -#include typedef int (*http_handler_t)(struct netconn *client, const char *name, char *revc_buf, size_t revc_len); @@ -66,6 +59,8 @@ typedef struct HttpCGI #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); @@ -73,4 +68,8 @@ 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 */ diff --git a/bertos/net/http_test.c b/bertos/net/http_test.c new file mode 100644 index 00000000..e57ced9a --- /dev/null +++ b/bertos/net/http_test.c @@ -0,0 +1,114 @@ +/** + * \file + * + * + * \brief HTTP Server test + * + * \author Daniele Basile + */ + +#include +#include +#include + +#include + +#include + +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);