From: Michael Wallner Date: Thu, 10 Jan 2013 17:24:04 +0000 (+0000) Subject: add max-age to http\Cookie X-Git-Tag: RELEASE_2_1_0_RC3~10^2^2~42 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=8df0e5b02cd04b43f19045c0f4f69c4076840d27;p=m6w6%2Fext-http add max-age to http\Cookie --- diff --git a/php_http_cookie.c b/php_http_cookie.c index 8dfc62c..008c76d 100644 --- a/php_http_cookie.c +++ b/php_http_cookie.c @@ -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()) { diff --git a/php_http_cookie.h b/php_http_cookie.h index cda5b84..7ff0b7c 100644 --- a/php_http_cookie.h +++ b/php_http_cookie.h @@ -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); diff --git a/phpunit/CookieTest.php b/phpunit/CookieTest.php index a1a75db..affe798 100644 --- a/phpunit/CookieTest.php +++ b/phpunit/CookieTest.php @@ -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"));