http_parse_query_ex support binary buffer, and set both lengths of key and name

pull/2/head
yuqing 2014-08-11 16:58:27 +08:00
parent 93598b1862
commit 9ca29b6b06
6 changed files with 105 additions and 6 deletions

View File

@ -21,7 +21,7 @@ Version 1.06 2014-08-11
* replace INT64_PRINTF_FORMAT with PRId64 * replace INT64_PRINTF_FORMAT with PRId64
* support OS Darwin * support OS Darwin
* socket send and recv ignore erno EINTR * socket send and recv ignore erno EINTR
* http_parse_query set both lengths of key and name * http_parse_query_ex support binary buffer, and set both lengths of key and name
Version 1.05 2012-07-08 Version 1.05 2012-07-08
* update source code from FastDFS V3.09 * update source code from FastDFS V3.09

View File

@ -132,13 +132,19 @@ typedef struct
char minor; char minor;
} Version; } Version;
typedef struct
{
char *key;
char *value;
} KeyValuePair;
typedef struct typedef struct
{ {
char *key; char *key;
char *value; char *value;
int key_len; int key_len;
int value_len; int value_len;
} KeyValuePair; } KeyValuePairEx;
typedef struct typedef struct
{ {

View File

@ -277,6 +277,7 @@ int http_parse_query(char *url, KeyValuePair *params, const int max_count)
char *p; char *p;
char *pKeyEnd; char *pKeyEnd;
char *pValueEnd; char *pValueEnd;
int value_len;
pParamStart = strchr(url, '?'); pParamStart = strchr(url, '?');
if (pParamStart == NULL) if (pParamStart == NULL)
@ -316,6 +317,70 @@ int http_parse_query(char *url, KeyValuePair *params, const int max_count)
} }
*pKeyEnd = '\0'; *pKeyEnd = '\0';
if (*pCurrent->key == '\0') //empty key
{
continue;
}
pCurrent->value = pKeyEnd + 1;
urldecode(pCurrent->value, (int)(pValueEnd - pCurrent->value),
pCurrent->value, &value_len);
pCurrent++;
}
return pCurrent - params;
}
int http_parse_query_ex(char *url, const int url_len,
int *uri_len, KeyValuePairEx *params, const int max_count)
{
KeyValuePairEx *pCurrent;
KeyValuePairEx *pEnd;
char *pParamStart;
char *p;
char *pStrEnd;
char *pKeyEnd;
char *pValueEnd;
pParamStart = (char *)memchr(url, '?', url_len);
if (pParamStart == NULL)
{
*uri_len = url_len;
return 0;
}
*uri_len = pParamStart - url;
pStrEnd = url + url_len;
pEnd = params + max_count;
pCurrent = params;
p = pParamStart + 1;
while (p < pStrEnd)
{
if (pCurrent >= pEnd)
{
return pCurrent - params;
}
pCurrent->key = p;
pValueEnd = (char *)memchr(p, '&', pStrEnd - p);
if (pValueEnd == NULL)
{
pValueEnd = pStrEnd;
p = pStrEnd;
}
else
{
p = pValueEnd + 1;
}
pKeyEnd = (char *)memchr(pCurrent->key, '=',
pStrEnd - pCurrent->key);
if (pKeyEnd == NULL) //no =
{
continue;
}
pCurrent->key_len = (int)(pKeyEnd - pCurrent->key); pCurrent->key_len = (int)(pKeyEnd - pCurrent->key);
if (pCurrent->key_len == 0) //empty key if (pCurrent->key_len == 0) //empty key
{ {
@ -323,7 +388,7 @@ int http_parse_query(char *url, KeyValuePair *params, const int max_count)
} }
pCurrent->value = pKeyEnd + 1; pCurrent->value = pKeyEnd + 1;
urldecode(pCurrent->value, (int)(pValueEnd - pCurrent->value), urldecode_ex(pCurrent->value, (int)(pValueEnd - pCurrent->value),
pCurrent->value, &pCurrent->value_len); pCurrent->value, &pCurrent->value_len);
pCurrent++; pCurrent++;
} }

View File

@ -64,6 +64,19 @@ return: param count
**/ **/
int http_parse_query(char *url, KeyValuePair *params, const int max_count); int http_parse_query(char *url, KeyValuePair *params, const int max_count);
/**
parse url ex
params:
url: the url to parse, the url be modified after parse
url_len: the length of url
uri_len: return the uri length (not including ? and parameters)
params: params array to store param and it's value
max_count: max param count
return: param count
**/
int http_parse_query_ex(char *url, const int url_len,
int *uri_len, KeyValuePairEx *params, const int max_count);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -1887,6 +1887,13 @@ char *urlencode(const char *src, const int src_len, char *dest, int *dest_len)
} }
char *urldecode(const char *src, const int src_len, char *dest, int *dest_len) char *urldecode(const char *src, const int src_len, char *dest, int *dest_len)
{
(void)urldecode_ex(src, src_len, dest, dest_len);
*(dest + *dest_len) = '\0';
return dest;
}
char *urldecode_ex(const char *src, const int src_len, char *dest, int *dest_len)
{ {
#define IS_HEX_CHAR(ch) \ #define IS_HEX_CHAR(ch) \
((ch >= '0' && ch <= '9') || \ ((ch >= '0' && ch <= '9') || \
@ -1950,9 +1957,7 @@ char *urldecode(const char *src, const int src_len, char *dest, int *dest_len)
} }
} }
*pDest = '\0';
*dest_len = pDest - dest; *dest_len = pDest - dest;
return dest; return dest;
} }

View File

@ -240,7 +240,7 @@ int buffer_memcpy(BufferInfo *pBuff, const char *buff, const int len);
*/ */
char *urlencode(const char *src, const int src_len, char *dest, int *dest_len); char *urlencode(const char *src, const int src_len, char *dest, int *dest_len);
/** url decode /** url decode, terminated with \0
* parameters: * parameters:
* src: the source string to decode * src: the source string to decode
* src_len: source string length * src_len: source string length
@ -250,6 +250,16 @@ char *urlencode(const char *src, const int src_len, char *dest, int *dest_len);
*/ */
char *urldecode(const char *src, const int src_len, char *dest, int *dest_len); char *urldecode(const char *src, const int src_len, char *dest, int *dest_len);
/** url decode, no terminate with \0
* parameters:
* src: the source string to decode
* src_len: source string length
* dest: store dest string
* dest_len: store the dest string length
* return: error no, 0 success, != 0 fail
*/
char *urldecode_ex(const char *src, const int src_len, char *dest, int *dest_len);
/** get char occurs count /** get char occurs count
* parameters: * parameters:
* src: the source string * src: the source string