From 540dc81aaba32706787f118b9ae05153103f7902 Mon Sep 17 00:00:00 2001 From: Daniele Basile Date: Wed, 18 Jan 2012 16:36:36 +0100 Subject: [PATCH] Some fix and simple optimizations. --- bertos/net/http.c | 28 +++++++++++++++------------- bertos/net/http.h | 2 +- bertos/net/http_test.c | 12 ++++++++++++ 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/bertos/net/http.c b/bertos/net/http.c index 15b4b13d..e7ffd7eb 100644 --- a/bertos/net/http.c +++ b/bertos/net/http.c @@ -101,8 +101,7 @@ int http_getValue(char *tolenized_buf, size_t tolenized_buf_len, const char *key /* skip key */ p += token_len + 1; - http_decodeUrl(p, strlen(p), decoded_str, sizeof(decoded_str)); - value_len = strlen(decoded_str); + value_len = http_decodeUrl(p, strlen(p), decoded_str, sizeof(decoded_str)); if (value_len >= len) return -1; @@ -150,17 +149,18 @@ static char http_hexToAscii(char first, char second) return strtol(hex, &stop, 16); } -void http_decodeUrl(const char *raw_buf, size_t raw_len, char *decodec_buf, size_t len) +size_t http_decodeUrl(const char *raw_buf, size_t raw_len, char *decodec_buf, size_t len) { ASSERT(decodec_buf); char value; + size_t i; memset(decodec_buf, 0, len); - for (size_t i = 0; i < raw_len; i++) + for (i = 0; i < raw_len; i++) { - if (!len) - return; + if (len <= 1) + return i; if (raw_buf[i] == '%') { @@ -183,6 +183,8 @@ void http_decodeUrl(const char *raw_buf, size_t raw_len, char *decodec_buf, size *decodec_buf++ = (raw_buf[i] == '+' ? ' ' : raw_buf[i]); len--; } + + return i; } void http_getPageName(const char *recv_buf, size_t recv_len, char *page_name, size_t len) @@ -219,7 +221,7 @@ void http_getPageName(const char *recv_buf, size_t recv_len, char *page_name, si INLINE const char *get_ext(const char *name) { const char *ext = strstr(name, "."); - if(ext && (ext + 1)) + if((ext != NULL) && ((ext + 1) != '\0')) return (ext + 1); return NULL; @@ -257,9 +259,9 @@ void http_sendOk(struct netconn *client, int content_type) { ASSERT(content_type < HTTP_CONTENT_CNT); - netconn_write(client, http_html_hdr_200, sizeof(http_html_hdr_200) - 1, NETCONN_COPY); + netconn_write(client, http_html_hdr_200, sizeof(http_html_hdr_200) - 1, NETCONN_NOCOPY); netconn_write(client, http_content_type[content_type].content, - strlen(http_content_type[content_type].content), NETCONN_COPY); + strlen(http_content_type[content_type].content), NETCONN_NOCOPY); } @@ -271,9 +273,9 @@ void http_sendFileNotFound(struct netconn *client, int content_type) { ASSERT(content_type < HTTP_CONTENT_CNT); - netconn_write(client, http_html_hdr_404, sizeof(http_html_hdr_404) - 1, NETCONN_COPY); + netconn_write(client, http_html_hdr_404, sizeof(http_html_hdr_404) - 1, NETCONN_NOCOPY); netconn_write(client, http_content_type[content_type].content, - strlen(http_content_type[content_type].content), NETCONN_COPY); + strlen(http_content_type[content_type].content), NETCONN_NOCOPY); } /** @@ -284,9 +286,9 @@ void http_sendInternalErr(struct netconn *client, int content_type) { ASSERT(content_type < HTTP_CONTENT_CNT); - netconn_write(client, http_html_hdr_500, sizeof(http_html_hdr_500) - 1, NETCONN_COPY); + netconn_write(client, http_html_hdr_500, sizeof(http_html_hdr_500) - 1, NETCONN_NOCOPY); netconn_write(client, http_content_type[content_type].content, - strlen(http_content_type[content_type].content), NETCONN_COPY); + strlen(http_content_type[content_type].content), NETCONN_NOCOPY); } static http_handler_t cgi_search(const char *name, HttpCGI *table) diff --git a/bertos/net/http.h b/bertos/net/http.h index 29fa27d6..15c3035b 100644 --- a/bertos/net/http.h +++ b/bertos/net/http.h @@ -77,7 +77,7 @@ enum int http_getValue(char *tolenized_buf, size_t tolenized_buf_len, const char *key, char *value, size_t len); int http_tokenizeGetRequest(char *raw_buf, size_t raw_len); void http_getPageName(const char *recv_buf, size_t recv_len, char *page_name, size_t len); -void http_decodeUrl(const char *raw_buf, size_t raw_len, char *decodec_buf, size_t len); +size_t http_decodeUrl(const char *raw_buf, size_t raw_len, char *decodec_buf, size_t len); int http_searchContentType(const char *name); void http_sendOk(struct netconn *client, int content_type); diff --git a/bertos/net/http_test.c b/bertos/net/http_test.c index f0dbbf49..43bace22 100644 --- a/bertos/net/http_test.c +++ b/bertos/net/http_test.c @@ -72,6 +72,9 @@ q=0.6,en;q=0.4 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3"; static const char uri[] = "test%5B%5D!@;'%22%5C.%20"; static const char uri_check[] = "test[]!@;'\"\\. "; +static const char uri0[] = "12345"; +static const char uri_check0[] = "1234"; + static const char uri1[] = "!*'();:@&=%2B%24%2C/?#%5B%5D%3C%3E%7E.%22%7B%7D%7C%5C-%60_%5E%25"; static const char uri_check1[] = "!*'();:@&=+$,/?#[]<>~.\"{}|\\-`_^%"; @@ -155,6 +158,15 @@ int http_testRun(void) } + char decoded0[5]; + http_decodeUrl(uri0, 6, decoded0, 5); + + if (strcmp(decoded0, uri_check0)) + { + kprintf("error 04 %s\n", decoded0); + goto error; + } + char decoded[sizeof(uri)]; http_decodeUrl(uri,sizeof(uri), decoded, sizeof(uri)); -- 2.25.1