From 93598b186202aef9c3a25a9c566ce02e097e6cc1 Mon Sep 17 00:00:00 2001 From: yuqing Date: Mon, 11 Aug 2014 16:23:27 +0800 Subject: [PATCH] http_parse_query set both lengths of key and name --- HISTORY | 3 ++- src/common_define.h | 2 ++ src/http_func.c | 28 +++++++++++++++------------- src/http_func.h | 2 +- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/HISTORY b/HISTORY index 4551fd2..385180c 100644 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,5 @@ -Version 1.06 2014-08-10 +Version 1.06 2014-08-11 * update source code from FastDFS V5.02 * add function short2buff and buff2short * add object memory pool (fast_mblock.h and fast_mblock.c) @@ -21,6 +21,7 @@ Version 1.06 2014-08-10 * replace INT64_PRINTF_FORMAT with PRId64 * support OS Darwin * socket send and recv ignore erno EINTR + * http_parse_query set both lengths of key and name Version 1.05 2012-07-08 * update source code from FastDFS V3.09 diff --git a/src/common_define.h b/src/common_define.h index 1d50376..657a2fb 100644 --- a/src/common_define.h +++ b/src/common_define.h @@ -136,6 +136,8 @@ typedef struct { char *key; char *value; + int key_len; + int value_len; } KeyValuePair; typedef struct diff --git a/src/http_func.c b/src/http_func.c index 7db86cb..3dd0ad6 100644 --- a/src/http_func.c +++ b/src/http_func.c @@ -275,8 +275,8 @@ int http_parse_query(char *url, KeyValuePair *params, const int max_count) KeyValuePair *pEnd; char *pParamStart; char *p; - char *pStrEnd; - int value_len; + char *pKeyEnd; + char *pValueEnd; pParamStart = strchr(url, '?'); if (pParamStart == NULL) @@ -297,32 +297,34 @@ int http_parse_query(char *url, KeyValuePair *params, const int max_count) } pCurrent->key = p; - pStrEnd = strchr(p, '&'); - if (pStrEnd == NULL) + pValueEnd = strchr(p, '&'); + if (pValueEnd == NULL) { + pValueEnd = p + strlen(p); p = NULL; } else { - *pStrEnd = '\0'; - p = pStrEnd + 1; + *pValueEnd = '\0'; + p = pValueEnd + 1; } - pStrEnd = strchr(pCurrent->key, '='); - if (pStrEnd == NULL) + pKeyEnd = strchr(pCurrent->key, '='); + if (pKeyEnd == NULL) //no = { continue; } - *pStrEnd = '\0'; - pCurrent->value = pStrEnd + 1; - if (*pCurrent->key == '\0') + *pKeyEnd = '\0'; + pCurrent->key_len = (int)(pKeyEnd - pCurrent->key); + if (pCurrent->key_len == 0) //empty key { continue; } - urldecode(pCurrent->value, strlen(pCurrent->value), \ - pCurrent->value, &value_len); + pCurrent->value = pKeyEnd + 1; + urldecode(pCurrent->value, (int)(pValueEnd - pCurrent->value), + pCurrent->value, &pCurrent->value_len); pCurrent++; } diff --git a/src/http_func.h b/src/http_func.h index a5f6be2..95edc88 100644 --- a/src/http_func.h +++ b/src/http_func.h @@ -57,7 +57,7 @@ int get_url_content(const char *url, const int connect_timeout, \ /** parse url params: - url: the url to parse + url: the url to parse, the url be modified after parse params: params array to store param and it's value max_count: max param count return: param count