Skip to content

Commit f148d0a

Browse files
committed
Set cookie name and value pursuant to rfc2616 and rfc6265
1 parent 60594d5 commit f148d0a

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

src/Curl/Curl.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,9 +541,45 @@ public function setDigestAuthentication($username, $password = '')
541541
*/
542542
public function setCookie($key, $value)
543543
{
544-
$this->cookies[$key] = $value;
544+
$name_chars = array();
545+
foreach (str_split($key) as $name_char) {
546+
if (!in_array($name_char, array(
547+
// RFC2616: "any CHAR except CTLs or separators".
548+
'!', '#', '$', '%', '&', "'", '*', '+', '-', '.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
549+
'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
550+
'W', 'X', 'Y', 'Z', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
551+
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '|', '~',), true)) {
552+
$name_chars[] = rawurlencode($name_char);
553+
} else {
554+
$name_chars[] = $name_char;
555+
}
556+
}
557+
558+
$value_chars = array();
559+
foreach (str_split($value) as $value_char) {
560+
if (!in_array($value_char, array(
561+
// RFC6265: "US-ASCII characters excluding CTLs, whitespace DQUOTE, comma, semicolon, and backslash".
562+
// %x21
563+
'!',
564+
// %x23-2B
565+
'#', '$', '%', '&', "'", '(', ')', '*', '+',
566+
// %x2D-3A
567+
'-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':',
568+
// %x3C-5B
569+
'<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
570+
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[',
571+
// %x5D-7E
572+
']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
573+
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~',), true)) {
574+
$value_chars[] = rawurlencode($value_char);
575+
} else {
576+
$value_chars[] = $value_char;
577+
}
578+
}
579+
580+
$this->cookies[implode('', $name_chars)] = implode('', $value_chars);
545581
$this->setOpt(CURLOPT_COOKIE, implode('; ', array_map(function($k, $v) {
546-
return $k . '=' . str_replace(' ', '%20', $v);
582+
return $k . '=' . $v;
547583
}, array_keys($this->cookies), array_values($this->cookies))));
548584
}
549585

0 commit comments

Comments
 (0)