- add http_build_cookie
authorMichael Wallner <mike@php.net>
Fri, 21 Jul 2006 16:08:33 +0000 (16:08 +0000)
committerMichael Wallner <mike@php.net>
Fri, 21 Jul 2006 16:08:33 +0000 (16:08 +0000)
- improve response performance: no need for ob and only chunk response if a buffer size is actually set

http.c
http_cookie_api.c
http_functions.c
http_response_object.c
http_send_api.c
http_util_object.c
package2.xml
php_http.h
php_http_cookie_api.h

diff --git a/http.c b/http.c
index 874528ed0221d96c0429655de979a23daea2d3e9..5ae77b4ef3f82c99a5ff6182f0188edaeb4af33e 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_build_cookie, NULL)
        PHP_FE(http_parse_params, NULL)
        PHP_FE(http_get_request_headers, NULL)
        PHP_FE(http_get_request_body, NULL)
@@ -188,7 +189,7 @@ static inline void _http_globals_init(zend_http_globals *G TSRMLS_DC)
 #else
        G->request.time = time(NULL);
 #endif
-       G->send.buffer_size = HTTP_SENDBUF_SIZE;
+       G->send.buffer_size = 0;
        G->send.not_found_404 = 1;
        G->read_post_data = 0;
 }
index 1ad997a63f377718f6108acdeb21760c166c9b87..aff04abfef4f3d46bed6c5c70091c8f3722652bb 100644 (file)
@@ -17,6 +17,9 @@
 #include "php_http_date_api.h"
 #include "php_http_cookie_api.h"
 
+#include "ext/standard/url.h"
+
+/* {{{ PHP_MINIT_FUNCTION(http_cookie) */
 PHP_MINIT_FUNCTION(http_cookie)
 {
        HTTP_LONG_CONSTANT("HTTP_COOKIE_PARSE_RAW", HTTP_COOKIE_PARSE_RAW);
@@ -25,7 +28,9 @@ PHP_MINIT_FUNCTION(http_cookie)
        
        return SUCCESS;
 }
+/* }}} */
 
+/* {{{ http_cookie_list *http_cookie_list_init(http_cookie_list *) */
 PHP_HTTP_API http_cookie_list *_http_cookie_list_init(http_cookie_list *list ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
 {
        if (!list) {
@@ -42,7 +47,9 @@ PHP_HTTP_API http_cookie_list *_http_cookie_list_init(http_cookie_list *list ZEN
        
        return list;
 }
+/* }}} */
 
+/* {{{ void http_cookie_list_dtor(http_cookie_list *) */
 PHP_HTTP_API void _http_cookie_list_dtor(http_cookie_list *list TSRMLS_DC)
 {
        if (list) {
@@ -53,7 +60,9 @@ PHP_HTTP_API void _http_cookie_list_dtor(http_cookie_list *list TSRMLS_DC)
                STR_SET(list->domain, NULL);
        }
 }
+/* }}} */
 
+/* {{{ void http_cookie_list_free(http_cookie_list **) */
 PHP_HTTP_API void _http_cookie_list_free(http_cookie_list **list TSRMLS_DC)
 {
        if (list) {
@@ -62,7 +71,9 @@ PHP_HTTP_API void _http_cookie_list_free(http_cookie_list **list TSRMLS_DC)
                *list = NULL;
        }
 }
+/* }}} */
 
+/* {{{ const char *http_cookie_list_get_cookie(http_cookie_list *, const char*, size_t) */
 PHP_HTTP_API const char *_http_cookie_list_get_cookie(http_cookie_list *list, const char *name, size_t name_len TSRMLS_DC)
 {
        zval **cookie = NULL;
@@ -71,7 +82,9 @@ PHP_HTTP_API const char *_http_cookie_list_get_cookie(http_cookie_list *list, co
        }
        return Z_STRVAL_PP(cookie);
 }
+/* }}} */
 
+/* {{{ const char *http_cookie_list_get_extra(http_cookie_list *, const char *, size_t) */
 PHP_HTTP_API const char *_http_cookie_list_get_extra(http_cookie_list *list, const char *name, size_t name_len TSRMLS_DC)
 {
        zval **extra = NULL;
@@ -80,7 +93,9 @@ PHP_HTTP_API const char *_http_cookie_list_get_extra(http_cookie_list *list, con
        }
        return Z_STRVAL_PP(extra);
 }
+/* }}} */
 
+/* {{{ void http_cookie_list_add_cookie(http_cookie_list *, const char *, size_t, const char *, size_t) */
 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;
@@ -90,7 +105,9 @@ PHP_HTTP_API void _http_cookie_list_add_cookie(http_cookie_list *list, const cha
        zend_hash_update(&list->cookies, key, name_len + 1, (void *) &cookie_value, sizeof(zval *), NULL);
        efree(key);
 }
+/* }}} */
 
+/* {{{ void http_cookie_list_add_extr(http_cookie_list *, const char *, size_t, const char *, size_t) */
 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;
@@ -100,6 +117,7 @@ PHP_HTTP_API void _http_cookie_list_add_extra(http_cookie_list *list, const char
        zend_hash_update(&list->extras, key, name_len + 1, (void *) &cookie_value, sizeof(zval *), NULL);
        efree(key);
 }
+/* }}} */
 
 typedef struct _http_parse_param_cb_arg_t {
        http_cookie_list *list;
@@ -107,6 +125,7 @@ typedef struct _http_parse_param_cb_arg_t {
        char **allowed_extras;
 } http_parse_param_cb_arg;
 
+/* {{{ static void http_parse_cookie_callback */
 static void http_parse_cookie_callback(void *ptr, const char *key, int keylen, const char *val, int vallen TSRMLS_DC)
 {
        http_parse_param_cb_arg *arg = (http_parse_param_cb_arg *) ptr;
@@ -131,15 +150,32 @@ static void http_parse_cookie_callback(void *ptr, const char *key, int keylen, c
                        
                        for (; *ae; ++ae) {
                                if ((size_t) keylen == strlen(*ae) && !strncasecmp(key, *ae, keylen)) {
-                                       http_cookie_list_add_extra(arg->list, key, keylen, val, vallen);
+                                       if (arg->flags & HTTP_COOKIE_PARSE_RAW) {
+                                               http_cookie_list_add_extra(arg->list, key, keylen, val, vallen);
+                                       } else {
+                                               char *dec = estrndup(val, vallen);
+                                               int declen = php_url_decode(dec, vallen);
+                                               
+                                               http_cookie_list_add_extra(arg->list, key, keylen, dec, declen);
+                                               efree(dec);
+                                       }
                                        return;
                                }
                        }
                }
                /* new cookie */
-               http_cookie_list_add_cookie(arg->list, key, keylen, val, vallen);
+               if (arg->flags & HTTP_COOKIE_PARSE_RAW) {
+                       http_cookie_list_add_cookie(arg->list, key, keylen, val, vallen);
+               } else {
+                       char *dec = estrndup(val, vallen);
+                       int declen = php_url_decode(dec, vallen);
+                       
+                       http_cookie_list_add_cookie(arg->list, key, keylen, dec, declen);
+                       efree(dec);
+               }
        }
 }
+/* }}} */
 
 /* {{{ 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)
@@ -166,6 +202,7 @@ PHP_HTTP_API http_cookie_list *_http_parse_cookie_ex(http_cookie_list *list, con
 }
 /* }}} */
 
+/* {{{ void http_cookie_list_tostruct(http_cookie_list *, zval *) */
 PHP_HTTP_API void _http_cookie_list_tostruct(http_cookie_list *list, zval *strct TSRMLS_DC)
 {
        zval array, *cookies, *extras;
@@ -187,6 +224,143 @@ PHP_HTTP_API void _http_cookie_list_tostruct(http_cookie_list *list, zval *strct
        add_assoc_string(&array, "path", list->path?list->path:"", 1);
        add_assoc_string(&array, "domain", list->domain?list->domain:"", 1);
 }
+/* }}} */
+
+/* {{{ http_cookie_list *http_cookie_list_fromstruct(http_cookie_list *, zval *strct) */
+PHP_HTTP_API http_cookie_list *_http_cookie_list_fromstruct(http_cookie_list *list, zval *strct TSRMLS_DC)
+{
+       zval **tmp, *cpy;
+       HashTable *ht = HASH_OF(strct);
+       
+       list = http_cookie_list_init(list);
+       
+       if (SUCCESS == zend_hash_find(ht, "cookies", sizeof("cookies"), (void *) &tmp) && Z_TYPE_PP(tmp) == IS_ARRAY) {
+               zend_hash_copy(&list->cookies, Z_ARRVAL_PP(tmp), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
+       }
+       if (SUCCESS == zend_hash_find(ht, "extras", sizeof("extras"), (void *) &tmp) && Z_TYPE_PP(tmp) == IS_ARRAY) {
+               zend_hash_copy(&list->extras, Z_ARRVAL_PP(tmp), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
+       }
+       if (SUCCESS == zend_hash_find(ht, "flags", sizeof("flags"), (void *) &tmp)) {
+               switch (Z_TYPE_PP(tmp)) {
+                       case IS_LONG:
+                               list->flags = Z_LVAL_PP(tmp);
+                               break;
+                       case IS_DOUBLE:
+                               list->flags = (long) Z_DVAL_PP(tmp);
+                               break;
+                       case IS_STRING:
+                               cpy = zval_copy(IS_LONG, *tmp);
+                               list->flags = Z_LVAL_P(cpy);
+                               zval_free(&cpy);
+                               break;
+                       default:
+                               break;
+               }
+       }
+       if (SUCCESS == zend_hash_find(ht, "expires", sizeof("expires"), (void *) &tmp)) {
+               switch (Z_TYPE_PP(tmp)) {
+                       case IS_LONG:
+                               list->expires = Z_LVAL_PP(tmp);
+                               break;
+                       case IS_DOUBLE:
+                               list->expires = (long) Z_DVAL_PP(tmp);
+                               break;
+                       case IS_STRING:
+                               cpy = zval_copy(IS_LONG, *tmp);
+                               if (Z_LVAL_P(cpy)) {
+                                       list->expires = Z_LVAL_P(cpy);
+                               } else {
+                                       time_t expires = http_parse_date(Z_STRVAL_PP(tmp));
+                                       if (expires > 0) {
+                                               list->expires = expires;
+                                       }
+                               }
+                               zval_free(&cpy);
+                               break;
+                       default:
+                               break;
+               }
+       }
+       if (SUCCESS == zend_hash_find(ht, "path", sizeof("path"), (void *) &tmp) && Z_TYPE_PP(tmp) == IS_STRING) {
+               list->path = estrndup(Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
+       }
+       if (SUCCESS == zend_hash_find(ht, "domain", sizeof("domain"), (void *) &tmp)) {
+               list->domain = estrndup(Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
+       }
+       
+       return list;
+}
+/* }}} */
+
+/* {{{ inline append_encoded */
+static inline void append_encoded(phpstr *buf, const char *key, size_t key_len, const char *val, size_t val_len)
+{
+       char *enc_str[2];
+       int enc_len[2];
+       
+       enc_str[0] = php_url_encode(key, key_len, &enc_len[0]);
+       enc_str[1] = php_url_encode(val, val_len, &enc_len[1]);
+       
+       phpstr_append(buf, enc_str[0], enc_len[0]);
+       phpstr_appends(buf, "=");
+       phpstr_append(buf, enc_str[1], enc_len[1]);
+       phpstr_appends(buf, "; ");
+       
+       efree(enc_str[0]);
+       efree(enc_str[1]);
+}
+/* }}} */
+
+/* {{{ void http_cookie_list_tostring(http_cookie_list *, char **, size_t *) */
+PHP_HTTP_API void _http_cookie_list_tostring(http_cookie_list *list, char **str, size_t *len TSRMLS_DC)
+{
+       phpstr buf;
+       zval **val;
+       ulong idx = 0;
+       uint keylen = 0;
+       char *key = NULL;
+       HashPosition pos;
+       
+       phpstr_init(&buf);
+       
+       FOREACH_HASH_KEYLENVAL(pos, &list->cookies, key, keylen, idx, val) {
+               if (key && keylen) {
+                       append_encoded(&buf, key, keylen-1, Z_STRVAL_PP(val), Z_STRLEN_PP(val));
+                       key = NULL;
+               }
+       }
+       
+       if (list->domain && *list->domain) {
+               phpstr_appendf(&buf, "domain=%s; ", list->domain);
+       }
+       if (list->path && *list->path) {
+               phpstr_appendf(&buf, "path=%s; ", list->path);
+       }
+       if (list->expires) {
+               char *date = http_date(list->expires);
+               phpstr_appendf(&buf, "expires=%s; ", date);
+               efree(date);
+       }
+       
+       FOREACH_HASH_KEYLENVAL(pos, &list->extras, key, keylen, idx, val) {
+               if (key && keylen) {
+                       append_encoded(&buf, key, keylen-1, Z_STRVAL_PP(val), Z_STRLEN_PP(val));
+                       key = NULL;
+               }
+       }
+       
+       if (list->flags & HTTP_COOKIE_SECURE) {
+               phpstr_appends(&buf, "secure; ");
+       }
+       if (list->flags & HTTP_COOKIE_HTTPONLY) {
+               phpstr_appends(&buf, "httpOnly; ");
+       }
+       
+       phpstr_fix(&buf);
+       *str = PHPSTR_VAL(&buf);
+       *len = PHPSTR_LEN(&buf);
+}
+/* }}} */
 
 /*
  * Local variables:
index 33c1e4a34a3a856826ae94ae6fe3cab0b721d1fe..82597f4517f4d22aa91d79d8426878065fdcdc3a 100644 (file)
@@ -1068,7 +1068,7 @@ PHP_FUNCTION(http_parse_cookie)
                FOREACH_VAL(pos, allowed_extras_array, entry) {
                        ZVAL_ADDREF(*entry);
                        convert_to_string_ex(entry);
-                       allowed_extras[i] = estrndup(Z_STRVAL_PP(entry), Z_STRLEN_PP(entry));
+                       allowed_extras[i++] = estrndup(Z_STRVAL_PP(entry), Z_STRLEN_PP(entry));
                        zval_ptr_dtor(entry);
                }
        }
@@ -1088,6 +1088,30 @@ PHP_FUNCTION(http_parse_cookie)
                efree(allowed_extras);
        }
 }
+/* }}} */
+
+/* {{{ proto string http_build_cookie(array cookie)
+ *
+ * Build a cookie string from an array/object like returned by http_parse_cookie().
+ */
+PHP_FUNCTION(http_build_cookie)
+{
+       char *str = NULL;
+       size_t len = 0;
+       zval *strct;
+       http_cookie_list list;
+       
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &strct)) {
+               RETURN_FALSE;
+       }
+       
+       http_cookie_list_fromstruct(&list, strct);
+       http_cookie_list_tostring(&list, &str, &len);
+       http_cookie_list_dtor(&list);
+       
+       RETURN_STRINGL(str, len, 0);
+}
+/* }}} */
 
 /* {{{ proto object http_parse_params(string param[, int flags = HTTP_PARAMS_DEFAULT])
  *
index fc4bf22536ffd4383d3efaa5d8272ce65741f3b4..89e5f2b30374d877ff77f57616a97aa9c1ea873c 100644 (file)
@@ -1156,9 +1156,6 @@ PHP_METHOD(HttpResponse, send)
        /* gzip */
        HTTP_G->send.deflate.encoding = zval_is_true(GET_STATIC_PROP(gzip));
        
-       /* start ob */
-       php_start_ob_buffer(NULL, HTTP_G->send.buffer_size, 0 TSRMLS_CC);
-
        /* send */
        switch (Z_LVAL_P(GET_STATIC_PROP(mode))) {
                case SEND_DATA:
index 587870f8a85fb170ec408dc5aedd30eab7b4b22e..10bcdc0ba3b83db4233b390f37166b4513dcc69b 100644 (file)
@@ -88,13 +88,19 @@ static inline void _http_send_response_data_plain(void **buffer, const char *dat
                http_encoding_stream *s = *((http_encoding_stream **) buffer);
                
                http_encoding_deflate_stream_update(s, data, data_len, &encoded, &encoded_len);
-               phpstr_chunked_output((phpstr **) &s->storage, encoded, encoded_len, HTTP_G->send.buffer_size, _http_flush, NULL TSRMLS_CC);
+               if (HTTP_G->send.buffer_size) {
+                       phpstr_chunked_output((phpstr **) &s->storage, encoded, encoded_len, HTTP_G->send.buffer_size, _http_flush, NULL TSRMLS_CC);
+               } else {
+                       http_flush(encoded, encoded_len);
+               }
                efree(encoded);
 #else
                http_error(HE_ERROR, HTTP_E_RESPONSE, "Attempt to send GZIP response despite being able to do so; please report this bug");
 #endif
-       } else {
+       } else if (HTTP_G->send.buffer_size) {
                phpstr_chunked_output((phpstr **) buffer, data, data_len, HTTP_G->send.buffer_size, _http_flush, NULL TSRMLS_CC);
+       } else {
+               http_flush(data, data_len);
        }
 }
 /* }}} */
@@ -154,13 +160,17 @@ static inline void _http_send_response_finish(void **buffer TSRMLS_DC)
                http_encoding_stream *s = *((http_encoding_stream **) buffer);
                
                http_encoding_deflate_stream_finish(s, &encoded, &encoded_len);
-               phpstr_chunked_output((phpstr **) &s->storage, encoded, encoded_len, 0, _http_flush, NULL TSRMLS_CC);
+               if (HTTP_G->send.buffer_size) {
+                       phpstr_chunked_output((phpstr **) &s->storage, encoded, encoded_len, 0, _http_flush, NULL TSRMLS_CC);
+               } else {
+                       http_flush(encoded, encoded_len);
+               }
                http_encoding_deflate_stream_free(&s);
                STR_FREE(encoded);
 #else
                http_error(HE_ERROR, HTTP_E_RESPONSE, "Attempt to send GZIP response despite being able to do so; please report this bug");
 #endif
-       } else {
+       } else if (HTTP_G->send.buffer_size) {
                phpstr_chunked_output((phpstr **) buffer, NULL, 0, 0, _http_flush, NULL TSRMLS_CC);
        }
 }
index ad75f7a51f56c5137e9bc6a8240ed0123930361e..a985dbb507b9206a943d8badbacc5e9c8393085d 100644 (file)
@@ -85,6 +85,10 @@ HTTP_BEGIN_ARGS(parseCookie, 1)
        HTTP_ARG_VAL(cookie_string, 0)
 HTTP_END_ARGS;
 
+HTTP_BEGIN_ARGS(buildCookie, 1)
+       HTTP_ARG_VAL(cookie_array, 0)
+HTTP_END_ARGS;
+
 HTTP_BEGIN_ARGS(parseParams, 1)
        HTTP_ARG_VAL(param_string, 0)
        HTTP_ARG_VAL(flags, 0)
@@ -123,6 +127,7 @@ zend_function_entry http_util_object_fe[] = {
        HTTP_UTIL_ALIAS(parseMessage, http_parse_message)
        HTTP_UTIL_ALIAS(parseHeaders, http_parse_headers)
        HTTP_UTIL_ALIAS(parseCookie, http_parse_cookie)
+       HTTP_UTIL_ALIAS(buildCookie, http_build_cookie)
        HTTP_UTIL_ALIAS(parseParams, http_parse_params)
        HTTP_UTIL_ALIAS(chunkedDecode, http_chunked_decode)
 #ifdef HTTP_HAVE_ZLIB
index 04c2cd769735dfa66eec147d2ef516ea9bed2719..400d12fde9cec1b781e5339f1cba840553271e2e 100644 (file)
@@ -28,10 +28,10 @@ support. Parallel requests are available for PHP 5 and greater.
   <email>mike@php.net</email>
   <active>yes</active>
  </lead>
- <date>2006-07-17</date>
+ <date>2006-00-00</date>
  <version>
-  <release>1.1.0</release>
-  <api>1.1.0</api>
+  <release>1.2.0</release>
+  <api>1.2.0</api>
  </version>
  <stability>
   <release>stable</release>
@@ -39,13 +39,9 @@ support. Parallel requests are available for PHP 5 and greater.
  </stability>
  <license>BSD, revised</license>
  <notes><![CDATA[
-* Fixed endless loop in http_build_url("..")
-* Fixed HttpResponse::capture() failure if buffered output exceeds 40k
-* Fixed HttpQueryString failures with objects as params
-* Fixed memory leaks with overloaded classes extending HTTP classes
-* Added HttpQueryString::mod(array $params) method
-* Added ArrayAccess to interfaces implemented by HttpQueryString
-* Added HttpMessage::getHeader(string $name) method
+- Improve response performance
+* Fixed http_parse_cookie() allowed_extras and flags parameters
+* Added http_build_cookie() function
 ]]></notes>
  <contents>
   <dir name="/">
index 4ae657420b3b458aa86b49d444e4106998315cdf..dd49c6cfe6216760dff0d3e4ad259c2f41c290ce 100644 (file)
@@ -15,7 +15,7 @@
 #ifndef PHP_EXT_HTTP_H
 #define PHP_EXT_HTTP_H
 
-#define PHP_EXT_HTTP_VERSION "1.1.0"
+#define PHP_EXT_HTTP_VERSION "1.2.0dev"
 
 #ifdef HAVE_CONFIG_H
 #      include "config.h"
@@ -183,6 +183,7 @@ PHP_FUNCTION(http_chunked_decode);
 PHP_FUNCTION(http_parse_message);
 PHP_FUNCTION(http_parse_headers);
 PHP_FUNCTION(http_parse_cookie);
+PHP_FUNCTION(http_build_cookie);
 PHP_FUNCTION(http_parse_params);
 PHP_FUNCTION(http_get_request_headers);
 PHP_FUNCTION(http_get_request_body);
index dfdcdc5f3928bdcbe4c8b252754e19871dca31fe..48312b9b063cd534ef806175569dfecb2776fee2 100644 (file)
@@ -68,6 +68,12 @@ PHP_HTTP_API http_cookie_list *_http_parse_cookie_ex(http_cookie_list * list, co
 #define http_cookie_list_tostruct(l, s) _http_cookie_list_tostruct((l), (s) TSRMLS_CC)
 PHP_HTTP_API void _http_cookie_list_tostruct(http_cookie_list *list, zval *strct TSRMLS_DC);
 
+#define http_cookie_list_fromstruct(l, s) _http_cookie_list_fromstruct((l), (s) TSRMLS_CC)
+PHP_HTTP_API http_cookie_list *_http_cookie_list_fromstruct(http_cookie_list *list, zval *strct TSRMLS_DC);
+
+#define http_cookie_list_tostring(l, str, len) _http_cookie_list_tostring((l), (str), (len) TSRMLS_CC)
+PHP_HTTP_API void _http_cookie_list_tostring(http_cookie_list *list, char **str, size_t *len TSRMLS_DC);
+
 #endif
 
 /*