- add http_parse_params()
authorMichael Wallner <mike@php.net>
Fri, 28 Apr 2006 14:45:22 +0000 (14:45 +0000)
committerMichael Wallner <mike@php.net>
Fri, 28 Apr 2006 14:45:22 +0000 (14:45 +0000)
KnownIssues.txt
http.c
http_api.c
http_cookie_api.c
http_functions.c
package2.xml
php_http.h
php_http_api.h

index 62ba03294c4bc96bb1b98c6c2356ebf110add072..d54a683521503571adc3836aa37e8516e268abf0 100644 (file)
@@ -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 c951383007f5c83029c60a84a151d3de533f9bea..729d025891237e71dd85c8ae9909a1ef9cfdc165 100644 (file)
--- 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)
index c932266c5d38f75a5b315c26f76fdb3692459921..33cd17d1009c6adf5efc5050f3561a363725ac7d 100644 (file)
@@ -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
index fa6b2536e1bfe775e0a6e0741d88e9b44a006777..7d10ba5c524f05835d06d8824e329a68645d50b3 100644 (file)
@@ -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);
index 9b42801830c6ec197a14cf45836da807a747ff6b..c7b2dbf5953940939772b0fdcd213dbad7d70230 100644 (file)
@@ -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", &param, &param_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.
index a840438cb7fe23ee00c849493bcbb25afc71d8e9..2e373b148ce72bccc316c744f668afe9dd2654db 100644 (file)
@@ -46,6 +46,7 @@ HttpResponse
  </stability>
  <license>BSD, revised</license>
  <notes><![CDATA[
++ Added http_parse_params() function
 * Fixed possible crash in progress/onfinish request callbacks
 * Fixed http_redirect() and http_build_url() without arguments
 * Enabled recursive query string merging in http_build_url() with HTTP_URL_JOIN_QUERY
index 1c62e822d03178bb91baefc886edcc1cab048009..d945565eed8608dd2cab469bbff53f7d13c9e798 100644 (file)
@@ -169,6 +169,7 @@ PHP_FUNCTION(http_chunked_decode);
 PHP_FUNCTION(http_parse_message);
 PHP_FUNCTION(http_parse_headers);
 PHP_FUNCTION(http_parse_cookie);
+PHP_FUNCTION(http_parse_params);
 PHP_FUNCTION(http_get_request_headers);
 PHP_FUNCTION(http_get_request_body);
 PHP_FUNCTION(http_get_request_body_stream);
index f7d7db19f300b57fd92ee1bcfe13489d0f8d1b47..ac56e9a295602976d8a6fe8c86ed060d2b8e2dcf 100644 (file)
@@ -119,6 +119,17 @@ PHP_HTTP_API STATUS _http_get_request_body_ex(char **body, size_t *length, zend_
 #define http_get_request_body_stream() _http_get_request_body_stream(TSRMLS_C)
 PHP_HTTP_API php_stream *_http_get_request_body_stream(TSRMLS_D);
 
+
+typedef void (*http_parse_params_callback)(void *cb_arg, const char *key, int keylen, const char *val, int vallen TSRMLS_DC);
+
+#define http_parse_params_default_callback _http_parse_params_default_callback
+PHP_HTTP_API void _http_parse_params_default_callback(void *ht, const char *key, int keylen, const char *val, int vallen TSRMLS_DC);
+
+#define http_parse_params(s, ht) _http_parse_params_ex((s), 1, _http_parse_params_default_callback, (ht) TSRMLS_CC)
+#define http_parse_params_ex(s, comma, cb, a) _http_parse_params_ex((s), (comma), (cb), (a) TSRMLS_CC)
+PHP_HTTP_API STATUS _http_parse_params_ex(const char *params, int alloc_comma_sep, http_parse_params_callback cb, void *cb_arg TSRMLS_DC);
+
+
 #define http_locate_body _http_locate_body
 static inline const char *_http_locate_body(const char *message)
 {