From: Michael Wallner Date: Fri, 28 Apr 2006 14:45:22 +0000 (+0000) Subject: - add http_parse_params() X-Git-Tag: RELEASE_1_0_0~34 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=562b92bca8c6c98f2358b429d8113861e49877a2;p=m6w6%2Fext-http - add http_parse_params() --- diff --git a/KnownIssues.txt b/KnownIssues.txt index 62ba032..d54a683 100644 --- a/KnownIssues.txt +++ b/KnownIssues.txt @@ -31,3 +31,6 @@ Internals: - our http_urlencode_hash() only handles arrays and does not differentiate between prefixes for numeric or string keys. + + - check all places where hash table entries are created if + the keys are properly NUL terminated (prior 1.0!) diff --git a/http.c b/http.c index c951383..729d025 100644 --- a/http.c +++ b/http.c @@ -93,6 +93,7 @@ zend_function_entry http_functions[] = { PHP_FE(http_parse_message, NULL) PHP_FE(http_parse_headers, NULL) PHP_FE(http_parse_cookie, NULL) + PHP_FE(http_parse_params, NULL) PHP_FE(http_get_request_headers, NULL) PHP_FE(http_get_request_body, NULL) PHP_FE(http_get_request_body_stream, NULL) diff --git a/http_api.c b/http_api.c index c932266..33cd17d 100644 --- a/http_api.c +++ b/http_api.c @@ -261,7 +261,7 @@ PHP_HTTP_API STATUS _http_get_request_body_ex(char **body, size_t *length, zend_ } /* }}} */ -/* {{{ php_stream *_http_get_request_body_stream(void) */ +/* {{{ php_stream *http_get_request_body_stream(void) */ PHP_HTTP_API php_stream *_http_get_request_body_stream(TSRMLS_D) { php_stream *s = NULL; @@ -292,6 +292,220 @@ PHP_HTTP_API php_stream *_http_get_request_body_stream(TSRMLS_D) } /* }}} */ +/* {{{ void http_parse_params_default_callback(...) */ +PHP_HTTP_API void _http_parse_params_default_callback(void *arg, const char *key, int keylen, const char *val, int vallen TSRMLS_DC) +{ + char *kdup; + zval tmp, *entry; + HashTable *ht = (HashTable *) arg; + + if (ht) { + INIT_ZARR(tmp, ht); + + if (vallen) { + MAKE_STD_ZVAL(entry); + array_init(entry); + kdup = estrndup(key, keylen); + add_assoc_stringl_ex(entry, kdup, keylen + 1, (char *) val, vallen, 1); + efree(kdup); + add_next_index_zval(&tmp, entry); + } else { + add_next_index_stringl(&tmp, (char *) key, keylen, 1); + } + } +} +/* }}} */ + +/* {{{ STATUS http_parse_params(const char *, HashTable *) */ +PHP_HTTP_API STATUS _http_parse_params_ex(const char *param, int allow_comma_sep, http_parse_params_callback cb, void *cb_arg TSRMLS_DC) +{ +#define ST_QUOTE 1 +#define ST_VALUE 2 +#define ST_KEY 3 +#define ST_ASSIGN 4 +#define ST_ADD 5 + + int st = ST_KEY, keylen = 0, vallen = 0; + char *s, *c, *key = NULL, *val = NULL; + + for(c = s = estrdup(param);;) { +#if 0 + char *tk = NULL, *tv = NULL; + + if (key) { + if (keylen) { + tk= estrndup(key, keylen); + } else { + tk = ecalloc(1, 7); + memcpy(tk, key, 3); + tk[3]='.'; tk[4]='.'; tk[5]='.'; + } + } + if (val) { + if (vallen) { + tv = estrndup(val, vallen); + } else { + tv = ecalloc(1, 7); + memcpy(tv, val, 3); + tv[3]='.'; tv[4]='.'; tv[5]='.'; + } + } + fprintf(stderr, "[%6s] %c \"%s=%s\"\n", + ( + st == ST_QUOTE ? "QUOTE" : + st == ST_VALUE ? "VALUE" : + st == ST_KEY ? "KEY" : + st == ST_ASSIGN ? "ASSIGN" : + st == ST_ADD ? "ADD": + "HUH?" + ), *c, tk, tv + ); + STR_FREE(tk); STR_FREE(tv); +#endif + switch (st) + { + case ST_QUOTE: + quote: + if (*c == '"') { + if (*(c-1) == '\\') { + memmove(c-1, c, strlen(c)+1); + goto quote; + } else { + goto add; + } + } else { + if (!val) { + val = c; + } + if (!*c) { + --val; + st = ST_ADD; + } + } + break; + + case ST_VALUE: + switch (*c) + { + case '"': + if (!val) { + st = ST_QUOTE; + } + break; + + case ' ': + break; + + case ';': + case '\0': + goto add; + break; + + default: + if (!val) { + val = c; + } + break; + } + break; + + case ST_KEY: + switch (*c) + { + case ',': + if (allow_comma_sep) { + goto allow_comma; + } + case '\r': + case '\n': + case '\t': + case '\013': + case '\014': + goto failure; + break; + + case '=': + if (key) { + keylen = c - key; + st = ST_VALUE; + } else { + goto failure; + } + break; + + case ' ': + if (key) { + keylen = c - key; + st = ST_ASSIGN; + } + break; + + case ';': + case '\0': + allow_comma: + if (key) { + keylen = c - key; + st = ST_ADD; + } + break; + + default: + if (!key) { + key = c; + } + break; + } + break; + + case ST_ASSIGN: + if (*c == '=') { + st = ST_VALUE; + } else if (!*c || *c == ';') { + st = ST_ADD; + } else if (*c != ' ') { + goto failure; + } + break; + + case ST_ADD: + add: + if (val) { + vallen = c - val; + if (st != ST_QUOTE) { + while (val[vallen-1] == ' ') --vallen; + } + } else { + val = ""; + vallen = 0; + } + + cb(cb_arg, key, keylen, val, vallen TSRMLS_CC); + + st = ST_KEY; + key = val = NULL; + keylen = vallen = 0; + break; + } + + if (*c) { + ++c; + } else if (st == ST_ADD) { + goto add; + } else { + break; + } + } + + efree(s); + return SUCCESS; + +failure: + http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Unexpected character (%c) at pos %tu of %zu", *c, c-s, strlen(s)); + efree(s); + return FAILURE; +} +/* }}} */ + /* * Local variables: * tab-width: 4 diff --git a/http_cookie_api.c b/http_cookie_api.c index fa6b253..7d10ba5 100644 --- a/http_cookie_api.c +++ b/http_cookie_api.c @@ -84,244 +84,85 @@ PHP_HTTP_API const char *_http_cookie_list_get_extra(http_cookie_list *list, con PHP_HTTP_API void _http_cookie_list_add_cookie(http_cookie_list *list, const char *name, size_t name_len, const char *value, size_t value_len TSRMLS_DC) { zval *cookie_value; + char *key = estrndup(name, name_len); MAKE_STD_ZVAL(cookie_value); ZVAL_STRINGL(cookie_value, estrndup(value, value_len), value_len, 0); - zend_hash_update(&list->cookies, (char *) name, name_len + 1, (void *) &cookie_value, sizeof(zval *), NULL); + zend_hash_update(&list->cookies, key, name_len + 1, (void *) &cookie_value, sizeof(zval *), NULL); + efree(key); } PHP_HTTP_API void _http_cookie_list_add_extra(http_cookie_list *list, const char *name, size_t name_len, const char *value, size_t value_len TSRMLS_DC) { zval *cookie_value; + char *key = estrndup(name, name_len); MAKE_STD_ZVAL(cookie_value); ZVAL_STRINGL(cookie_value, estrndup(value, value_len), value_len, 0); - zend_hash_update(&list->extras, (char *) name, name_len + 1, (void *) &cookie_value, sizeof(zval *), NULL); + zend_hash_update(&list->extras, key, name_len + 1, (void *) &cookie_value, sizeof(zval *), NULL); + efree(key); } -#define http_cookie_list_set_item_ex(l, i, il, v, vl, f, a) _http_cookie_list_set_item_ex((l), (i), (il), (v), (vl), (f), (a) TSRMLS_CC) -static inline void _http_cookie_list_set_item_ex(http_cookie_list *list, const char *item, int item_len, const char *value, int value_len, long flags, char **allowed_extras TSRMLS_DC) +typedef struct _http_parse_param_cb_arg_t { + http_cookie_list *list; + long flags; + char **allowed_extras; +} http_parse_param_cb_arg; + +static void http_parse_cookie_callback(void *ptr, const char *key, int keylen, const char *val, int vallen TSRMLS_DC) { - char *key = estrndup(item, item_len); + http_parse_param_cb_arg *arg = (http_parse_param_cb_arg *) ptr; - if (!strcasecmp(key, "path")) { - STR_SET(list->path, estrndup(value, value_len)); - } else if (!strcasecmp(key, "domain")) { - STR_SET(list->domain, estrndup(value, value_len)); - } else if (!strcasecmp(key, "expires")) { - char *date = estrndup(value, value_len); - list->expires = http_parse_date(date); +#define _KEY_IS(s) (keylen == lenof(s) && !strncasecmp(key, (s), keylen)) + if _KEY_IS("path") { + STR_SET(arg->list->path, estrndup(val, vallen)); + } else if _KEY_IS("domain") { + STR_SET(arg->list->domain, estrndup(val, vallen)); + } else if _KEY_IS("expires") { + char *date = estrndup(val, vallen); + arg->list->expires = http_parse_date(date); efree(date); - } else if (!strcasecmp(key, "secure")) { - list->flags |= HTTP_COOKIE_SECURE; - } else if (!strcasecmp(key, "httpOnly")) { - list->flags |= HTTP_COOKIE_HTTPONLY; + } else if _KEY_IS("secure") { + arg->list->flags |= HTTP_COOKIE_SECURE; + } else if _KEY_IS("httpOnly") { + arg->list->flags |= HTTP_COOKIE_HTTPONLY; } else { /* check for extra */ - if (allowed_extras) { - for (; *allowed_extras; ++allowed_extras) { - if (!strcasecmp(key, *allowed_extras)) { - http_cookie_list_add_extra(list, key, item_len, value, value_len); - - efree(key); + if (arg->allowed_extras) { + char **ae = arg->allowed_extras; + + for (; *ae; ++ae) { + if ((size_t) keylen == strlen(*ae) && !strncasecmp(key, *ae, keylen)) { + http_cookie_list_add_extra(arg->list, key, keylen, val, vallen); return; } } } /* new cookie */ - http_cookie_list_add_cookie(list, key, item_len, value, value_len); + http_cookie_list_add_cookie(arg->list, key, keylen, val, vallen); } - efree(key); } - -#define ST_QUOTE 1 -#define ST_VALUE 2 -#define ST_KEY 3 -#define ST_ASSIGN 4 -#define ST_ADD 5 - /* {{{ http_cookie_list *http_parse_cookie(char *, long) */ PHP_HTTP_API http_cookie_list *_http_parse_cookie_ex(http_cookie_list *list, const char *string, long flags, char **allowed_extras TSRMLS_DC) { - int free_list = !list, st = ST_KEY, keylen = 0, vallen = 0; - char *s, *c, *key = NULL, *val = NULL; + int free_list = !list; + http_parse_param_cb_arg arg; list = http_cookie_list_init(list); - c = s = estrdup(string); - for(;;) { -#if 0 - char *tk = NULL, *tv = NULL; - - if (key) { - if (keylen) { - tk= estrndup(key, keylen); - } else { - tk = ecalloc(1, 7); - memcpy(tk, key, 3); - tk[3]='.'; tk[4]='.'; tk[5]='.'; - } - } - if (val) { - if (vallen) { - tv = estrndup(val, vallen); - } else { - tv = ecalloc(1, 7); - memcpy(tv, val, 3); - tv[3]='.'; tv[4]='.'; tv[5]='.'; - } - } - fprintf(stderr, "[%6s] %c \"%s=%s\"\n", - ( - st == ST_QUOTE ? "QUOTE" : - st == ST_VALUE ? "VALUE" : - st == ST_KEY ? "KEY" : - st == ST_ASSIGN ? "ASSIGN" : - st == ST_ADD ? "ADD": - "HUH?" - ), *c, tk, tv - ); - STR_FREE(tk); STR_FREE(tv); -#endif - switch (st) - { - case ST_QUOTE: - quote: - if (*c == '"') { - if (*(c-1) == '\\') { - memmove(c-1, c, strlen(c)+1); - goto quote; - } else { - goto add; - } - } else { - if (!val) { - val = c; - } - if (!*c) { - --val; - st = ST_ADD; - } - } - break; - - case ST_VALUE: - switch (*c) - { - case '"': - if (!val) { - st = ST_QUOTE; - } - break; - - case ' ': - break; - - case ';': - case '\0': - goto add; - break; - - default: - if (!val) { - val = c; - } - break; - } - break; - - case ST_KEY: - switch (*c) - { - case ',': - case '\r': - case '\n': - case '\t': - case '\013': - case '\014': - goto failure; - break; - - case '=': - if (key) { - keylen = c - key; - st = ST_VALUE; - } else { - goto failure; - } - break; - - case ' ': - if (key) { - keylen = c - key; - st = ST_ASSIGN; - } - break; - - case ';': - case '\0': - if (key) { - keylen = c - key; - st = ST_ADD; - } - break; - - default: - if (!key) { - key = c; - } - break; - } - break; - - case ST_ASSIGN: - if (*c == '=') { - st = ST_VALUE; - } else if (!*c || *c == ';') { - st = ST_ADD; - } else if (*c != ' ') { - goto failure; - } - break; - - case ST_ADD: - add: - if (val) { - vallen = c - val; - if (st != ST_QUOTE) { - while (val[vallen-1] == ' ') --vallen; - } - } else { - val = ""; - vallen = 0; - } - http_cookie_list_set_item_ex(list, key, keylen, val, vallen, flags, allowed_extras); - st = ST_KEY; - key = val = NULL; - keylen = vallen = 0; - break; - } - - if (*c) { - ++c; - } else if (st == ST_ADD) { - goto add; + arg.list = list; + arg.flags = flags; + arg.allowed_extras = allowed_extras; + + if (SUCCESS != http_parse_params_ex(string, 0, http_parse_cookie_callback, &arg)) { + if (free_list) { + http_cookie_list_free(&list); } else { - break; + http_cookie_list_dtor(list); } + list = NULL; } - efree(s); return list; - -failure: - http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Unexpected character (%c) at pos %tu of %zu", *c, c-s, strlen(s)); - if (free_list) { - http_cookie_list_free(&list); - } else { - http_cookie_list_dtor(list); - } - efree(s); - return NULL; } /* }}} */ @@ -330,17 +171,17 @@ PHP_HTTP_API void _http_cookie_list_tostruct(http_cookie_list *list, zval *strct zval array, *cookies, *extras; INIT_ZARR(array, HASH_OF(strct)); - + MAKE_STD_ZVAL(cookies); array_init(cookies); zend_hash_copy(Z_ARRVAL_P(cookies), &list->cookies, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); add_assoc_zval(&array, "cookies", cookies); - + MAKE_STD_ZVAL(extras); array_init(extras); zend_hash_copy(Z_ARRVAL_P(extras), &list->extras, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); add_assoc_zval(&array, "extras", extras); - + add_assoc_long(&array, "flags", list->flags); add_assoc_long(&array, "expires", (long) list->expires); add_assoc_string(&array, "path", list->path?list->path:"", 1); diff --git a/http_functions.c b/http_functions.c index 9b42801..c7b2dbf 100644 --- a/http_functions.c +++ b/http_functions.c @@ -1117,6 +1117,27 @@ PHP_FUNCTION(http_parse_cookie) } } +/* {{{ proto object http_parse_params(string param) + * + * Parse parameter list. + */ +PHP_FUNCTION(http_parse_params) +{ + char *param; + int param_len; + + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", ¶m, ¶m_len)) { + RETURN_FALSE; + } + + object_init(return_value); + if (SUCCESS != http_parse_params(param, HASH_OF(return_value))) { + zval_dtor(return_value); + RETURN_FALSE; + } +} +/* }}} */ + /* {{{ proto array http_get_request_headers(void) * * Get a list of incoming HTTP headers. diff --git a/package2.xml b/package2.xml index a840438..2e373b1 100644 --- a/package2.xml +++ b/package2.xml @@ -46,6 +46,7 @@ HttpResponse BSD, revised