add max-age to http\Cookie
authorMichael Wallner <mike@php.net>
Thu, 10 Jan 2013 17:24:04 +0000 (17:24 +0000)
committerMichael Wallner <mike@php.net>
Thu, 10 Jan 2013 17:24:04 +0000 (17:24 +0000)
php_http_cookie.c
php_http_cookie.h
phpunit/CookieTest.php

index 8dfc62c814e9f8048a9d059c05e433ee6a044b65..008c76d05293cef905b1a2bf5c878d3346857637 100644 (file)
@@ -24,6 +24,7 @@ PHP_HTTP_API php_http_cookie_list_t *php_http_cookie_list_init(php_http_cookie_l
        list->path = NULL;
        list->domain = NULL;
        list->expires = -1;
+       list->max_age = -1;
        list->flags = 0;
        
        TSRMLS_SET_CTX(list->ts);
@@ -43,6 +44,7 @@ PHP_HTTP_API php_http_cookie_list_t *php_http_cookie_list_copy(php_http_cookie_l
        STR_SET(to->path, from->path ? estrdup(from->path) : NULL);
        STR_SET(to->domain, from->domain ? estrdup(from->domain) : NULL);
        to->expires = from->expires;
+       to->max_age = from->max_age;
        to->flags = from->flags;
 
        return to;
@@ -130,6 +132,8 @@ static void add_entry(php_http_cookie_list_t *list, char **allowed_extras, long
                char *date = estrndup(Z_STRVAL_P(arg), Z_STRLEN_P(arg));
                list->expires = php_parse_date(date, NULL);
                efree(date);
+       } else if _KEY_IS("max-age") {
+               list->max_age = strtol(Z_STRVAL_P(arg), NULL, 10);
        } else if _KEY_IS("secure") {
                list->flags |= PHP_HTTP_COOKIE_SECURE;
        } else if _KEY_IS("httpOnly") {
@@ -218,6 +222,7 @@ PHP_HTTP_API void php_http_cookie_list_to_struct(php_http_cookie_list_t *list, z
        
        add_assoc_long(&array, "flags", list->flags);
        add_assoc_long(&array, "expires", (long) list->expires);
+       add_assoc_long(&array, "max-age", (long) list->max_age);
        add_assoc_string(&array, "path", STR_PTR(list->path), 1);
        add_assoc_string(&array, "domain", STR_PTR(list->domain), 1);
 }
@@ -256,6 +261,20 @@ PHP_HTTP_API php_http_cookie_list_t *php_http_cookie_list_from_struct(php_http_c
                        zval_ptr_dtor(&cpy);
                }
        }
+       if (SUCCESS == zend_hash_find(ht, "max-age", sizeof("max-age"), (void *) &tmp)) {
+               if (Z_TYPE_PP(tmp) == IS_LONG) {
+                       list->max_age = Z_LVAL_PP(tmp);
+               } else {
+                       long lval;
+
+                       cpy = php_http_ztyp(IS_STRING, *tmp);
+                       if (IS_LONG == is_numeric_string(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy), &lval, NULL, 0)) {
+                               list->max_age = lval;
+                       }
+
+                       zval_ptr_dtor(&cpy);
+               }
+       }
        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));
        }
@@ -314,6 +333,9 @@ PHP_HTTP_API void php_http_cookie_list_to_string(php_http_cookie_list_t *list, c
                php_http_buffer_appendf(&buf, "expires=%s; ", date);
                efree(date);
        }
+       if (list->max_age >= 0) {
+               php_http_buffer_appendf(&buf, "max-age=%ld; ", list->max_age);
+       }
        
        FOREACH_HASH_KEYVAL(pos, &list->extras, key, val) {
                zval *tmp = php_http_ztyp(IS_STRING, *val);
@@ -353,6 +375,7 @@ PHP_HTTP_EMPTY_ARGS(getExtras);
 PHP_HTTP_EMPTY_ARGS(getDomain);
 PHP_HTTP_EMPTY_ARGS(getPath);
 PHP_HTTP_EMPTY_ARGS(getExpires);
+PHP_HTTP_EMPTY_ARGS(getMaxAge);
 PHP_HTTP_EMPTY_ARGS(getFlags);
 PHP_HTTP_EMPTY_ARGS(toString);
 
@@ -365,6 +388,9 @@ PHP_HTTP_END_ARGS;
 PHP_HTTP_BEGIN_ARGS(setExpires, 0)
        PHP_HTTP_ARG_VAL(value, 0)
 PHP_HTTP_END_ARGS;
+PHP_HTTP_BEGIN_ARGS(setMaxAge, 0)
+       PHP_HTTP_ARG_VAL(value, 0)
+PHP_HTTP_END_ARGS;
 PHP_HTTP_BEGIN_ARGS(setFlags, 0)
        PHP_HTTP_ARG_VAL(value, 0)
 PHP_HTTP_END_ARGS;
@@ -433,6 +459,8 @@ static zend_function_entry php_http_cookie_method_entry[] = {
        PHP_HTTP_COOKIE_ME(setPath, ZEND_ACC_PUBLIC)
        PHP_HTTP_COOKIE_ME(getExpires, ZEND_ACC_PUBLIC)
        PHP_HTTP_COOKIE_ME(setExpires, ZEND_ACC_PUBLIC)
+       PHP_HTTP_COOKIE_ME(getMaxAge, ZEND_ACC_PUBLIC)
+       PHP_HTTP_COOKIE_ME(setMaxAge, ZEND_ACC_PUBLIC)
        PHP_HTTP_COOKIE_ME(getFlags, ZEND_ACC_PUBLIC)
        PHP_HTTP_COOKIE_ME(setFlags, ZEND_ACC_PUBLIC)
 
@@ -849,6 +877,32 @@ PHP_METHOD(HttpCookie, setExpires)
        RETVAL_ZVAL(getThis(), 1, 0);
 }
 
+PHP_METHOD(HttpCookie, getMaxAge)
+{
+       if (SUCCESS == zend_parse_parameters_none()) {
+               php_http_cookie_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+               PHP_HTTP_COOKIE_OBJECT_INIT(obj);
+
+               RETURN_LONG(obj->list->max_age);
+       }
+       RETURN_FALSE;
+}
+
+PHP_METHOD(HttpCookie, setMaxAge)
+{
+       long ts = -1;
+
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &ts)) {
+               php_http_cookie_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+               PHP_HTTP_COOKIE_OBJECT_INIT(obj);
+
+               obj->list->max_age = ts;
+       }
+       RETVAL_ZVAL(getThis(), 1, 0);
+}
+
 PHP_METHOD(HttpCookie, getFlags)
 {
        if (SUCCESS == zend_parse_parameters_none()) {
index cda5b84840c43f120e5575955d28ad175497f3a2..7ff0b7cdaec9a34be67470e9f5e181b5dfeec3e1 100644 (file)
@@ -30,6 +30,7 @@ typedef struct php_http_cookie_list {
        char *path;
        char *domain;
        time_t expires;
+       time_t max_age;
 
 #ifdef ZTS
        void ***ts;
@@ -88,6 +89,8 @@ PHP_METHOD(HttpCookie, getPath);
 PHP_METHOD(HttpCookie, setPath);
 PHP_METHOD(HttpCookie, getExpires);
 PHP_METHOD(HttpCookie, setExpires);
+PHP_METHOD(HttpCookie, getMaxAge);
+PHP_METHOD(HttpCookie, setMaxAge);
 PHP_METHOD(HttpCookie, getFlags);
 PHP_METHOD(HttpCookie, setFlags);
 PHP_METHOD(HttpCookie, toString);
index a1a75db576a9b6c58ac39269f7c9f36c8b7d5c89..affe798dbb647c7bf130a02ffb472635e4ff065e 100644 (file)
@@ -10,7 +10,8 @@ class CookieTest extends PHPUnit_Framework_TestCase {
             "flags" => 0,
             "expires" => -1,
             "path" => "",
-            "domain" => ""
+            "domain" => "",
+               "max-age" => -1,
         );
         $this->assertEquals($a, $c->toArray());
         $this->assertEquals($a, $o->toArray());
@@ -40,6 +41,7 @@ class CookieTest extends PHPUnit_Framework_TestCase {
         foreach (array($orig, $copy) as $c) {
             $this->assertEquals("value", $c->getCookie("key"));
             $this->assertEquals(-1, $c->getExpires());
+            $this->assertEquals(-1, $c->getMaxAge());
             $this->assertEquals(0, $c->getFlags());
             $this->assertEquals(null, $c->getPath());
             $this->assertEquals(null, $c->getDomain());
@@ -59,6 +61,7 @@ class CookieTest extends PHPUnit_Framework_TestCase {
                     "expires" => -1,
                     "path" => "",
                     "domain" => "",
+                       "max-age" => -1,
                 ),
                 $c->toArray()
             );
@@ -86,6 +89,24 @@ class CookieTest extends PHPUnit_Framework_TestCase {
         );
     }
 
+    function testMaxAge() {
+        $c = new http\Cookie("this=max-age; max-age=12345");
+        $this->assertEquals("max-age", $c->getCookie("this"));
+        $this->assertEquals(12345, $c->getMaxAge());
+        $o = clone $c;
+        $t = 54321;
+        $o->setMaxAge();
+        $this->assertEquals(-1, $o->getMaxAge());
+        $this->assertNotEquals(-1, $c->getMaxAge());
+        $o->setMaxAge($t);
+        $this->assertEquals($t, $o->getMaxAge());
+        $this->assertNotEquals($t, $c->getMaxAge());
+        $this->assertEquals(
+               "this=max-age; max-age=$t; ",
+            $o->toString()
+        );
+    }
+
     function testPath() {
         $c = new http\Cookie("this=has a path; path=/down; ");
         $this->assertEquals("has a path", $c->getCookie("this"));