From 470f5cb37d5adce8fa5efd0092b2885528d7fb32 Mon Sep 17 00:00:00 2001 From: asterix Date: Tue, 4 Oct 2011 10:23:02 +0000 Subject: [PATCH] Deconding query string when we get a key value from it. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@5140 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/net/http.c | 40 +++++++++++++++++---------- bertos/net/http.h | 2 +- bertos/net/http_test.c | 63 +++++++++++++++++++++++++++++++++++++++--- 3 files changed, 85 insertions(+), 20 deletions(-) diff --git a/bertos/net/http.c b/bertos/net/http.c index c2dbef4d..2ea10044 100644 --- a/bertos/net/http.c +++ b/bertos/net/http.c @@ -65,6 +65,7 @@ static const char http_html_hdr_500[] = "HTTP/1.0 500 Internal Server Error\r\nC static HttpCGI *cgi_table; static http_handler_t http_callback; +static char decoded_str[80]; /** * Get key value from tokenized buffer @@ -81,21 +82,25 @@ int http_getValue(char *tolenized_buf, size_t tolenized_buf_len, const char *key for (size_t i = 0; i < tolenized_buf_len; i++) { - if (!strcmp(key, p)) + size_t token_len = strlen(p); + http_decodeUrl(p, token_len, decoded_str, sizeof(decoded_str)); + + if (!strcmp(key, decoded_str)) { /* skip key */ - size_t jump = strlen(p) + 1; - p += jump; + p += token_len + 1; + + http_decodeUrl(p, strlen(p), decoded_str, sizeof(decoded_str)); + value_len = strlen(decoded_str); - value_len = strlen(p); if (value_len >= len) return -1; - strcpy(value, p); + strcpy(value, decoded_str); break; } /* jump to next pair */ - p += strlen(p) + 1; + p += token_len + 1; } return value_len; @@ -134,9 +139,13 @@ static char http_hexToAscii(char first, char second) return strtol(hex, &stop, 16); } -void http_decodeUri(const char *raw_buf, size_t raw_len, char *decodec_buf, size_t len) +void http_decodeUrl(const char *raw_buf, size_t raw_len, char *decodec_buf, size_t len) { + ASSERT(decodec_buf); + char value; + memset(decodec_buf, 0, len); + for (size_t i = 0; i < raw_len; i++) { if (!len) @@ -158,7 +167,9 @@ void http_decodeUri(const char *raw_buf, size_t raw_len, char *decodec_buf, size } } } - *decodec_buf++ = raw_buf[i]; + + /* Manage special case of '+', that it should be convert in space */ + *decodec_buf++ = (raw_buf[i] == '+' ? ' ' : raw_buf[i]); len--; } } @@ -170,13 +181,12 @@ void http_getPageName(const char *recv_buf, size_t recv_len, char *page_name, si const char *p = recv_buf; if (p && (recv_len > sizeof("GET /"))) { - if (*p++ == 'G' && - *p++ == 'E' && *p++ == 'T') - { - str_ok = true; - /* skip the space and "/" */ - p += 2; - } + if (*p++ == 'G' && *p++ == 'E' && *p++ == 'T') + { + str_ok = true; + /* skip the space and "/" */ + p += 2; + } } if (str_ok) diff --git a/bertos/net/http.h b/bertos/net/http.h index 9fe4c381..4395f1ee 100644 --- a/bertos/net/http.h +++ b/bertos/net/http.h @@ -64,7 +64,7 @@ typedef struct HttpCGI 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_decodeUri(const char *raw_buf, size_t raw_len, char *decodec_buf, size_t len); +void http_decodeUrl(const char *raw_buf, size_t raw_len, char *decodec_buf, size_t len); void http_sendOk(struct netconn *client); void http_sendFileNotFound(struct netconn *client); diff --git a/bertos/net/http_test.c b/bertos/net/http_test.c index 59902e26..28828277 100644 --- a/bertos/net/http_test.c +++ b/bertos/net/http_test.c @@ -33,6 +33,8 @@ * \brief HTTP Server test * * \author Daniele Basile + * + * notest: avr */ #include @@ -73,8 +75,12 @@ static const char uri_check[] = "test[]!@;'\"\\. "; 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[] = "!*'();:@&=+$,/?#[]<>~.\"{}|\\-`_^%"; +static const char uri2[] = "test+test1+test2"; +static const char uri_check2[] = "test test1 test2"; + static char token_str[] = "var1=1&var2=2&var3=3&var4=4"; static char token_str1[] = "var1=1&var2=2&=3&var4="; +static char token_str2[] = "var1=test+test&var2=2&var3=test%5B%5D!@;'%22%5C.%20&var4=4"; int http_testSetup(void) { @@ -122,7 +128,7 @@ int http_testRun(void) char decoded[sizeof(uri)]; - http_decodeUri(uri,sizeof(uri), decoded, sizeof(uri)); + http_decodeUrl(uri,sizeof(uri), decoded, sizeof(uri)); if (strcmp(decoded, uri_check)) { @@ -131,7 +137,7 @@ int http_testRun(void) } char decoded1[sizeof(uri1)]; - http_decodeUri(uri1,sizeof(uri1), decoded1, sizeof(uri1)); + http_decodeUrl(uri1,sizeof(uri1), decoded1, sizeof(uri1)); if (strcmp(decoded1, uri_check1)) { @@ -139,6 +145,14 @@ int http_testRun(void) goto error; } + char decoded2[sizeof(uri2)]; + http_decodeUrl(uri2,sizeof(uri2), decoded2, sizeof(uri2)); + + if (strcmp(decoded2, uri_check2)) + { + kprintf("error 5 %s\n", decoded2); + goto error; + } int len = http_tokenizeGetRequest(token_str, sizeof(token_str)); if (len != 4) @@ -172,7 +186,11 @@ int http_testRun(void) goto error; } - http_getValue(token_str1, sizeof(token_str1), "var1", value, sizeof(value)); + if (http_getValue(token_str1, sizeof(token_str1), "var1", value, sizeof(value)) < 0) + { + kprintf("error 7 during get key %s\n", "var1"); + goto error; + } if (strcmp(value, "1")) { kprintf("error 7 value %s expect %s\n", value, "1"); @@ -180,7 +198,12 @@ int http_testRun(void) } - http_getValue(token_str1, sizeof(token_str1), "var4", value, sizeof(value)); + if (http_getValue(token_str1, sizeof(token_str1), "var4", value, sizeof(value)) < 0) + { + kprintf("error 7 during get key %s\n", "var4"); + goto error; + } + if (strcmp(value, "")) { kprintf("error 7 value %s expect %s\n", value, ""); @@ -188,6 +211,38 @@ int http_testRun(void) } + len = http_tokenizeGetRequest(token_str2, sizeof(token_str2)); + if (len != 4) + { + kprintf("error 8 len %d expect %d\n", len, 4); + goto error; + } + + if (http_getValue(token_str2, sizeof(token_str2), "var3", value, sizeof(value)) < 0) + { + kprintf("error 8 during get key %s\n", "var3"); + goto error; + } + + if (strcmp(value, "test[]!@;'\"\\. ")) + { + kprintf("error 8 value %s expect %s\n", value, "test[]!@;'\"\\. "); + goto error; + + } + + if (http_getValue(token_str2, sizeof(token_str2), "var1", value, sizeof(value)) < 0) + { + kprintf("error 7 during get key %s\n", "var1"); + goto error; + } + if (strcmp(value, "test test")) + { + kprintf("error 7 value %s expect %s\n", value, "test test"); + goto error; + + } + return 0; error: -- 2.25.1