From 024bf80e5f29e4de00d0813d23a4d3b67245ead7 Mon Sep 17 00:00:00 2001 From: asterix Date: Mon, 3 Oct 2011 17:15:43 +0000 Subject: [PATCH] Add some utility to manage query strings. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@5136 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/net/http.c | 74 +++++++++++++++++++++++++++++++++++++----- bertos/net/http.h | 5 ++- bertos/net/http_test.c | 51 +++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 10 deletions(-) diff --git a/bertos/net/http.c b/bertos/net/http.c index dd4ee4fd..05d8e1b2 100644 --- a/bertos/net/http.c +++ b/bertos/net/http.c @@ -59,13 +59,69 @@ #include -static const char http_html_hdr_200[] = "HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n"; -static const char http_html_hdr_404[] = "HTTP/1.1 404 Not Found\r\nContent-type: text/html\r\n\r\n"; -static const char http_html_hdr_500[] = "HTTP/1.1 500 Internal Server Error\r\nContent-type: text/html\r\n\r\n"; +static const char http_html_hdr_200[] = "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"; +static const char http_html_hdr_404[] = "HTTP/1.0 404 Not Found\r\nContent-type: text/html\r\n\r\n"; +static const char http_html_hdr_500[] = "HTTP/1.0 500 Internal Server Error\r\nContent-type: text/html\r\n\r\n"; static HttpCGI *cgi_table; static http_handler_t http_callback; +/** + * Get key value from tokenized buffer + */ +int http_getValue(char *tolenized_buf, size_t tolenized_buf_len, const char *key, char *value, size_t len) +{ + if (!tolenized_buf || !key || !value) + return -1; + + char *p = tolenized_buf; + size_t value_len = 0; + + memset(value, 0, len); + + for (size_t i = 0; i < tolenized_buf_len; i++) + { + if (!strcmp(key, p)) + { + /* skip key */ + size_t jump = strlen(p) + 1; + p += jump; + + value_len = strlen(p); + if (value_len >= len) + return -1; + + strcpy(value, p); + break; + } + /* jump to next pair */ + p += strlen(p) + 1; + } + + return value_len; +} + +/** + * tokenize a buffer + */ +int http_tokenizeGetRequest(char *raw_buf, size_t raw_len) +{ + size_t token = 0; + + for(size_t i = 0; (i < raw_len) && raw_buf; i++) + { + if (raw_buf[i] == '&') + { + token++; + raw_buf[i] = '\0'; + } + + if (raw_buf[i] == '=') + raw_buf[i] = '\0'; + } + + return token + 1; +} static char http_hexToAscii(char first, char second) { @@ -110,15 +166,15 @@ void http_getPageName(const char *recv_buf, size_t recv_len, char *page_name, si { int i = 0; bool str_ok = false; - - if (recv_buf && (recv_len > sizeof("GET /"))) + const char *p = recv_buf; + if (p && (recv_len > sizeof("GET /"))) { - if (*recv_buf++ == 'G' && - *recv_buf++ == 'E' && *recv_buf++ == 'T') + if (*p++ == 'G' && + *p++ == 'E' && *p++ == 'T') { str_ok = true; /* skip the space and "/" */ - recv_buf += 2; + p += 2; } } @@ -126,7 +182,7 @@ void http_getPageName(const char *recv_buf, size_t recv_len, char *page_name, si { while ((size_t)i < recv_len) { - char ch = *(recv_buf++); + char ch = *(p++); if (ch == ' ' || ch == '\t' || ch == '\n') break; if((size_t)i == len - 1) diff --git a/bertos/net/http.h b/bertos/net/http.h index 4cbdaac0..9fe4c381 100644 --- a/bertos/net/http.h +++ b/bertos/net/http.h @@ -54,12 +54,15 @@ typedef struct HttpCGI http_handler_t handler; ///< Callback to process the special request } HttpCGI; - #define CGI_MATCH_NONE 0 #define CGI_MATCH_WORD 1 ///< Select item in table only if string match #define CGI_MATCH_EXT 2 ///< Select item in table if the extention match #define CGI_MATCH_NAME 3 ///< Select item in table if the string is content +#define HTTP_MAX_GET_TOKENS 5 + +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); diff --git a/bertos/net/http_test.c b/bertos/net/http_test.c index 1636cf90..59902e26 100644 --- a/bertos/net/http_test.c +++ b/bertos/net/http_test.c @@ -73,6 +73,8 @@ 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 char token_str[] = "var1=1&var2=2&var3=3&var4=4"; +static char token_str1[] = "var1=1&var2=2&=3&var4="; int http_testSetup(void) { @@ -137,6 +139,55 @@ int http_testRun(void) goto error; } + + int len = http_tokenizeGetRequest(token_str, sizeof(token_str)); + if (len != 4) + { + kprintf("error 6 len %d expect %d\n", len, 4); + goto error; + } + + char value[80]; + http_getValue(token_str, sizeof(token_str), "var1", value, sizeof(value)); + if (strcmp(value, "1")) + { + kprintf("error 6 value %s expect %s\n", value, "1"); + goto error; + + } + + http_getValue(token_str, sizeof(token_str), "var4", value, sizeof(value)); + if (strcmp(value, "4")) + { + kprintf("error 6 value %s expect %s\n", value, "4"); + goto error; + + } + + + len = http_tokenizeGetRequest(token_str1, sizeof(token_str1)); + if (len != 4) + { + kprintf("error 7 len %d expect %d\n", len, 4); + goto error; + } + + http_getValue(token_str1, sizeof(token_str1), "var1", value, sizeof(value)); + if (strcmp(value, "1")) + { + kprintf("error 7 value %s expect %s\n", value, "1"); + goto error; + + } + + http_getValue(token_str1, sizeof(token_str1), "var4", value, sizeof(value)); + if (strcmp(value, "")) + { + kprintf("error 7 value %s expect %s\n", value, ""); + goto error; + + } + return 0; error: -- 2.25.1