list->path = NULL;
list->domain = NULL;
- list->expires = 0;
+ list->expires = -1;
list->flags = 0;
TSRMLS_SET_CTX(list->ts);
zend_symtable_update(&list->extras, name, name_len + 1, (void *) &cookie_value, sizeof(zval *), NULL);
}
+#define _KEY_IS(s) (key->len == sizeof(s) && !strncasecmp(key->str, (s), key->len))
+static void add_entry(php_http_cookie_list_t *list, char **allowed_extras, long flags, php_http_array_hashkey_t *key, zval *val)
+{
+ zval *arg = php_http_zsep(1, IS_STRING, val);
+
+ if (!(flags & PHP_HTTP_COOKIE_PARSE_RAW)) {
+ Z_STRLEN_P(arg) = php_raw_url_decode(Z_STRVAL_P(arg), Z_STRLEN_P(arg));
+ }
+
+ if _KEY_IS("path") {
+ STR_SET(list->path, estrndup(Z_STRVAL_P(arg), Z_STRLEN_P(arg)));
+ } else if _KEY_IS("domain") {
+ STR_SET(list->domain, estrndup(Z_STRVAL_P(arg), Z_STRLEN_P(arg)));
+ } else if _KEY_IS("expires") {
+ char *date = estrndup(Z_STRVAL_P(arg), Z_STRLEN_P(arg));
+ list->expires = php_parse_date(date, NULL);
+ efree(date);
+ } else if _KEY_IS("secure") {
+ list->flags |= PHP_HTTP_COOKIE_SECURE;
+ } else if _KEY_IS("httpOnly") {
+ list->flags |= PHP_HTTP_COOKIE_HTTPONLY;
+ } else {
+ char buf[0x20], *key_str;
+ int key_len;
+
+ if (key->type == HASH_KEY_IS_LONG) {
+ key_len = slprintf(buf, sizeof(buf) - 1, "%ld", key->num) + 1;
+ key_str = &buf[0];
+ } else {
+ key_len = key->len;
+ key_str = key->str;
+ }
+ /* check for extra */
+ if (allowed_extras) {
+ char **ae = allowed_extras;
+
+ for (; *ae; ++ae) {
+ if (!strncasecmp(key_str, *ae, key_len)) {
+ if (key->type == HASH_KEY_IS_LONG) {
+ zend_hash_index_update(&list->extras, key->num, (void *) &arg, sizeof(zval *), NULL);
+ } else {
+ zend_hash_update(&list->extras, key->str, key->len, (void *) &arg, sizeof(zval *), NULL);
+ }
+ return;
+ }
+ }
+ }
+
+ /* cookie */
+ if (key->type == HASH_KEY_IS_LONG) {
+ zend_hash_index_update(&list->cookies, key->num, (void *) &arg, sizeof(zval *), NULL);
+ } else {
+ zend_hash_update(&list->cookies, key->str, key->len, (void *) &arg, sizeof(zval *), NULL);
+ }
+ }
+}
+
PHP_HTTP_API php_http_cookie_list_t *php_http_cookie_list_parse(php_http_cookie_list_t *list, const char *str, size_t len, long flags, char **allowed_extras TSRMLS_DC)
{
php_http_params_opts_t opts;
FOREACH_HASH_KEYVAL(pos1, ¶ms, key, param) {
if (Z_TYPE_PP(param) == IS_ARRAY) {
if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(param), ZEND_STRS("value"), (void *) &val)) {
- Z_ADDREF_PP(val);
- if (key.type == HASH_KEY_IS_STRING) {
- zend_hash_update(&list->cookies, key.str, key.len, (void *) val, sizeof(zval *), NULL);
- } else {
- zend_hash_index_update(&list->cookies, key.num, (void *) val, sizeof(zval *), NULL);
- }
+ add_entry(list, NULL, flags, &key, *val);
}
if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(param), ZEND_STRS("arguments"), (void *) &args) && Z_TYPE_PP(args) == IS_ARRAY) {
FOREACH_KEYVAL(pos2, *args, key, arg) {
- if (key.type == HASH_KEY_IS_STRING) {
- zval *tmp = php_http_ztyp(IS_STRING, *arg);
- char *arg_str = Z_STRVAL_P(tmp);
- size_t arg_len = Z_STRLEN_P(tmp);
-#define _KEY_IS(s) (key.len == sizeof(s) && !strncasecmp(key.str, (s), key.len))
- if _KEY_IS("path") {
- STR_SET(list->path, estrndup(arg_str, arg_len));
- } else if _KEY_IS("domain") {
- STR_SET(list->domain, estrndup(arg_str, arg_len));
- } else if _KEY_IS("expires") {
- char *date = estrndup(arg_str, arg_len);
- list->expires = php_parse_date(date, NULL);
- efree(date);
- } else if _KEY_IS("secure") {
- list->flags |= PHP_HTTP_COOKIE_SECURE;
- } else if _KEY_IS("httpOnly") {
- list->flags |= PHP_HTTP_COOKIE_HTTPONLY;
- } else {
- /* check for extra */
- if (allowed_extras) {
- char **ae = allowed_extras;
-
- for (; *ae; ++ae) {
- if (!strncasecmp(key.str, *ae, key.len)) {
- if (flags & PHP_HTTP_COOKIE_PARSE_RAW) {
- php_http_cookie_list_add_extra(list, key.str, key.len, arg_str, arg_len);
- } else {
- char *dec = estrndup(arg_str, arg_len);
- int declen = php_url_decode(dec, arg_len);
-
- php_http_cookie_list_add_extra(list, key.str, key.len, dec, declen);
- efree(dec);
- }
- continue;
- }
- }
- }
- /* new cookie */
- if (flags & PHP_HTTP_COOKIE_PARSE_RAW) {
- php_http_cookie_list_add_cookie(list, key.str, key.len, arg_str, arg_len);
- } else {
- char *dec = estrndup(arg_str, arg_len);
- int declen = php_url_decode(dec, arg_len);
-
- php_http_cookie_list_add_cookie(list, key.str, key.len, dec, declen);
- efree(dec);
- }
- }
- zval_ptr_dtor(&tmp);
- }
+ add_entry(list, allowed_extras, flags, &key, *arg);
}
}
}
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 = php_http_ztyp(IS_LONG, *tmp);
- list->flags = Z_LVAL_P(cpy);
- zval_ptr_dtor(&cpy);
- break;
- default:
- break;
- }
+ cpy = php_http_ztyp(IS_LONG, *tmp);
+ list->flags = Z_LVAL_P(cpy);
+ zval_ptr_dtor(&cpy);
}
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 = php_http_ztyp(IS_LONG, *tmp);
- if (Z_LVAL_P(cpy)) {
- list->expires = Z_LVAL_P(cpy);
- } else {
- time_t expires = php_parse_date(Z_STRVAL_PP(tmp), NULL);
- if (expires > 0) {
- list->expires = expires;
- }
- }
- zval_ptr_dtor(&cpy);
- break;
- default:
- break;
+ if (Z_TYPE_PP(tmp) == IS_LONG) {
+ list->expires = 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->expires = lval;
+ } else {
+ list->expires = php_parse_date(Z_STRVAL_P(cpy), NULL);
+ }
+
+ zval_ptr_dtor(&cpy);
}
}
if (SUCCESS == zend_hash_find(ht, "path", sizeof("path"), (void *) &tmp) && Z_TYPE_PP(tmp) == IS_STRING) {
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]);
+ enc_str[0] = php_raw_url_encode(key, key_len, &enc_len[0]);
+ enc_str[1] = php_raw_url_encode(val, val_len, &enc_len[1]);
php_http_buffer_append(buf, enc_str[0], enc_len[0]);
php_http_buffer_appends(buf, "=");
php_http_buffer_init(&buf);
FOREACH_HASH_KEYVAL(pos, &list->cookies, key, val) {
+ zval *tmp = php_http_ztyp(IS_STRING, *val);
if (key.type == HASH_KEY_IS_STRING && key.len) {
- zval *tmp = php_http_ztyp(IS_STRING, *val);
append_encoded(&buf, key.str, key.len-1, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
- zval_ptr_dtor(&tmp);
+ } else if (key.type == HASH_KEY_IS_LONG) {
+ int enc_len;
+ char *enc_str = php_raw_url_encode(Z_STRVAL_P(tmp), Z_STRLEN_P(tmp), &enc_len);
+
+ php_http_buffer_appendf(&buf, "%ld=%.*s; ", key.num, enc_len, enc_str);
+ efree(enc_str);
}
+ zval_ptr_dtor(&tmp);
}
if (list->domain && *list->domain) {
if (list->path && *list->path) {
php_http_buffer_appendf(&buf, "path=%s; ", list->path);
}
- if (list->expires) {
+ if (list->expires >= 0) {
char *date = php_format_date(ZEND_STRL(PHP_HTTP_DATE_FORMAT), list->expires, 0 TSRMLS_CC);
php_http_buffer_appendf(&buf, "expires=%s; ", date);
efree(date);
}
FOREACH_HASH_KEYVAL(pos, &list->extras, key, val) {
+ zval *tmp = php_http_ztyp(IS_STRING, *val);
if (key.type == HASH_KEY_IS_STRING && key.len) {
- zval *tmp = php_http_ztyp(IS_STRING, *val);
append_encoded(&buf, key.str, key.len-1, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
- zval_ptr_dtor(&tmp);
+ } else if (key.type == HASH_KEY_IS_LONG) {
+ int enc_len;
+ char *enc_str = php_raw_url_encode(Z_STRVAL_P(tmp), Z_STRLEN_P(tmp), &enc_len);
+
+ php_http_buffer_appendf(&buf, "%ld=%.*s; ", key.num, enc_len, enc_str);
+ efree(enc_str);
}
+ zval_ptr_dtor(&tmp);
}
if (list->flags & PHP_HTTP_COOKIE_SECURE) {
}
switch (Z_TYPE_P(zcookie)) {
- case IS_ARRAY:
case IS_OBJECT:
+ if (instanceof_function(Z_OBJCE_P(zcookie), php_http_cookie_class_entry TSRMLS_CC)) {
+ php_http_cookie_object_t *zco = zend_object_store_get_object(zcookie TSRMLS_CC);
+
+ if (zco->list) {
+ obj->list = php_http_cookie_list_copy(zco->list, NULL);
+ }
+ break;
+ }
+ /* no break */
+ case IS_ARRAY:
obj->list = php_http_cookie_list_from_struct(obj->list, zcookie TSRMLS_CC);
break;
default: {
PHP_METHOD(HttpCookie, setCookie)
{
- char *name_str, *value_str;
- int name_len, value_len;
+ char *name_str, *value_str = NULL;
+ int name_len, value_len = 0;
if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s!", &name_str, &name_len, &value_str, &value_len)) {
php_http_cookie_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
PHP_METHOD(HttpCookie, setExtra)
{
- char *name_str, *value_str;
- int name_len, value_len;
+ char *name_str, *value_str = NULL;
+ int name_len, value_len = 0;
if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s!", &name_str, &name_len, &value_str, &value_len)) {
php_http_cookie_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
PHP_METHOD(HttpCookie, setExpires)
{
- long ts = 0;
+ 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);
--- /dev/null
+<?php
+
+class CookieTest extends PHPUnit_Framework_TestCase {
+ function testEmpty() {
+ $c = new http\Cookie;
+ $o = clone $c;
+ $a = array(
+ "cookies" => array(),
+ "extras" => array(),
+ "flags" => 0,
+ "expires" => -1,
+ "path" => "",
+ "domain" => ""
+ );
+ $this->assertEquals($a, $c->toArray());
+ $this->assertEquals($a, $o->toArray());
+ }
+
+ function testExpiresAsDate() {
+ $d = new DateTime;
+ $c = new http\Cookie(array("expires" => $d->format(DateTime::RFC1123)));
+ $this->assertEquals($d->format("U"), $c->getExpires());
+ }
+
+ function testNumeric() {
+ $c = new http\Cookie("1=%20; 2=%22; 3=%5D", 0, array(2));
+ $this->assertEquals("1=%20; 3=%5D; 2=%22; ", (string) $c);
+ }
+
+ function testRaw() {
+ $c = new http\Cookie("1=%20; 2=%22; e3=%5D", http\Cookie::PARSE_RAW, array(2));
+ $this->assertEquals("1=%2520; e3=%255D; 2=%2522; ", (string) $c);
+ }
+
+ function testSimple() {
+ $orig = new http\Cookie("key=value");
+ $copy = clone $orig;
+ $same = new http\Cookie($copy);
+ $even = new http\Cookie($same->toArray());
+ foreach (array($orig, $copy) as $c) {
+ $this->assertEquals("value", $c->getCookie("key"));
+ $this->assertEquals(-1, $c->getExpires());
+ $this->assertEquals(0, $c->getFlags());
+ $this->assertEquals(null, $c->getPath());
+ $this->assertEquals(null, $c->getDomain());
+ $this->assertEquals(array(), $c->getExtras());
+ $this->assertEquals(array("key" => "value"), $c->getCookies());
+ $this->assertEquals("key=value; ", $c->toString());
+ $this->assertEquals(
+ array (
+ "cookies" =>
+ array (
+ "key" => "value",
+ ),
+ "extras" =>
+ array (
+ ),
+ "flags" => 0,
+ "expires" => -1,
+ "path" => "",
+ "domain" => "",
+ ),
+ $c->toArray()
+ );
+ }
+ }
+
+ function testExpires() {
+ $c = new http\Cookie("this=expires; expires=Tue, 24 Jan 2012 10:35:32 +0100");
+ $this->assertEquals("expires", $c->getCookie("this"));
+ $this->assertEquals(1327397732, $c->getExpires());
+ $o = clone $c;
+ $t = time();
+ $o->setExpires();
+ $this->assertEquals(-1, $o->getExpires());
+ $this->assertNotEquals(-1, $c->getExpires());
+ $o->setExpires($t);
+ $this->assertEquals($t, $o->getExpires());
+ $this->assertNotEquals($t, $c->getExpires());
+ $this->assertEquals(
+ sprintf(
+ "this=expires; expires=%s; ",
+ date_create("@$t")->format("D, d M Y H:i:s \\G\\M\\T")
+ ),
+ $o->toString()
+ );
+ }
+
+ function testPath() {
+ $c = new http\Cookie("this=has a path; path=/down; ");
+ $this->assertEquals("has a path", $c->getCookie("this"));
+ $this->assertEquals("this=has%20a%20path; path=/down; ", (string)$c);
+ $this->assertEquals("/down", $c->getPath());
+ $o = clone $c;
+ $p = "/up";
+ $o->setPath();
+ $this->assertEquals(null, $o->getPath());
+ $this->assertNotEquals(null, $c->getPath());
+ $o->setPath($p);
+ $this->assertEquals($p, $o->getPath());
+ $this->assertNotEquals($p, $c->getPath());
+ $this->assertEquals("this=has%20a%20path; path=$p; ", $o->toString());
+ }
+
+ function testDomain() {
+ $c = new http\Cookie("this=has a domain; domain=.example.com; ");
+ $this->assertEquals("has a domain", $c->getCookie("this"));
+ $this->assertEquals("this=has%20a%20domain; domain=.example.com; ", (string)$c);
+ $this->assertEquals(".example.com", $c->getDomain());
+ $o = clone $c;
+ $d = "sub.example.com";
+ $o->setDomain();
+ $this->assertEquals(null, $o->getDomain());
+ $this->assertNotEquals(null, $c->getDomain());
+ $o->setDomain($d);
+ $this->assertEquals($d, $o->getDomain());
+ $this->assertNotEquals($d, $c->getDomain());
+ $this->assertEquals("this=has%20a%20domain; domain=$d; ", $o->toString());
+ }
+
+ function testFlags() {
+ $c = new http\Cookie("icanhas=flags; secure; httpOnly");
+ $this->assertEquals(http\Cookie::SECURE, $c->getFlags() & http\Cookie::SECURE, "secure");
+ $this->assertEquals(http\Cookie::HTTPONLY, $c->getFlags() & http\Cookie::HTTPONLY, "httpOnly");
+ $c->setFlags($c->getFlags() ^ http\Cookie::SECURE);
+ $this->assertEquals(0, $c->getFlags() & http\Cookie::SECURE, "secure");
+ $this->assertEquals(http\Cookie::HTTPONLY, $c->getFlags() & http\Cookie::HTTPONLY, "httpOnly");
+ $c->setFlags($c->getFlags() ^ http\Cookie::HTTPONLY);
+ $this->assertEquals(0, $c->getFlags() & http\Cookie::SECURE, "secure");
+ $this->assertEquals(0, $c->getFlags() & http\Cookie::HTTPONLY, "httpOnly");
+ $this->assertEquals("icanhas=flags; ", $c->toString());
+ $c->setFlags(http\Cookie::SECURE|http\Cookie::HTTPONLY);
+ $this->assertEquals("icanhas=flags; secure; httpOnly; ", $c->toString());
+ }
+
+ function testExtras() {
+ $c = new http\Cookie("c1=v1; e0=1; e2=2; c2=v2", 0, array("e0", "e1", "e2"));
+ $this->assertEquals(array("c1"=>"v1", "c2"=>"v2"), $c->getCookies());
+ $this->assertEquals(array("e0"=>"1", "e2"=>"2"), $c->getExtras());
+ $c->addExtra("e1", 1);
+ $c->setExtra("e0");
+ $c->setExtra("e3", 123);
+ $this->assertEquals(123, $c->getExtra("e3"));
+ $c->setExtra("e3");
+ $this->assertEquals(array("e2"=>"2", "e1"=>1), $c->getExtras());
+ $this->assertEquals("c1=v1; c2=v2; e2=2; e1=1; ", $c->toString());
+ $c->addExtras(array("e3"=>3, "e4"=>4));
+ $this->assertEquals(array("e2"=>"2", "e1"=>1, "e3"=>3, "e4"=>4), $c->getExtras());
+ $this->assertEquals("c1=v1; c2=v2; e2=2; e1=1; e3=3; e4=4; ", $c->toString());
+ $c->setExtras(array("e"=>"x"));
+ $this->assertEquals(array("e"=>"x"), $c->getExtras());
+ $this->assertEquals("c1=v1; c2=v2; e=x; ", $c->toString());
+ $c->setExtras();
+ $this->assertEquals(array(), $c->getExtras());
+ $this->assertEquals("c1=v1; c2=v2; ", $c->toString());
+ }
+
+ function testCookies() {
+ $c = new http\Cookie("e0=1; c1=v1; e2=2; c2=v2", 0, array("c0", "c1", "c2"));
+ $this->assertEquals(array("c1"=>"v1", "c2"=>"v2"), $c->getExtras());
+ $this->assertEquals(array("e0"=>"1", "e2"=>"2"), $c->getCookies());
+ $c->addCookie("e1", 1);
+ $c->setCookie("e0");
+ $c->setCookie("e3", 123);
+ $this->assertEquals(123, $c->getCookie("e3"));
+ $c->setCookie("e3");
+ $this->assertEquals(array("e2"=>"2", "e1"=>1), $c->getCookies());
+ $this->assertEquals("e2=2; e1=1; c1=v1; c2=v2; ", $c->toString());
+ $c->addCookies(array("e3"=>3, "e4"=>4));
+ $this->assertEquals(array("e2"=>"2", "e1"=>1, "e3"=>3, "e4"=>4), $c->getCookies());
+ $this->assertEquals("e2=2; e1=1; e3=3; e4=4; c1=v1; c2=v2; ", $c->toString());
+ $c->setCookies(array("e"=>"x"));
+ $this->assertEquals(array("e"=>"x"), $c->getCookies());
+ $this->assertEquals("e=x; c1=v1; c2=v2; ", $c->toString());
+ $c->setCookies();
+ $this->assertEquals(array(), $c->getCookies());
+ $this->assertEquals("c1=v1; c2=v2; ", $c->toString());
+ }
+}