Skip to content

Commit d30a625

Browse files
committed
Refactoring Curl::setCookie() by using static RFC symbols arrays from abstract class CurlCookieConst
1 parent 5cb062e commit d30a625

File tree

1 file changed

+44
-20
lines changed

1 file changed

+44
-20
lines changed

src/Curl/Curl.php

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,48 @@
22

33
namespace Curl;
44

5+
abstract class CurlCookieConst
6+
{
7+
private static $RFC2616 = array();
8+
private static $RFC6265 = array();
9+
10+
public static function Init() {
11+
self::$RFC2616 = array_fill_keys(array(
12+
// RFC2616: "any CHAR except CTLs or separators".
13+
'!', '#', '$', '%', '&', "'", '*', '+', '-', '.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
14+
'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
15+
'W', 'X', 'Y', 'Z', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
16+
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '|', '~',
17+
), true);
18+
19+
self::$RFC6265 = array_fill_keys(array(
20+
// RFC6265: "US-ASCII characters excluding CTLs, whitespace DQUOTE, comma, semicolon, and backslash".
21+
// %x21
22+
'!',
23+
// %x23-2B
24+
'#', '$', '%', '&', "'", '(', ')', '*', '+',
25+
// %x2D-3A
26+
'-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':',
27+
// %x3C-5B
28+
'<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
29+
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[',
30+
// %x5D-7E
31+
']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
32+
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~',
33+
), true);
34+
}
35+
36+
public static function RFC2616() {
37+
return self::$RFC2616;
38+
}
39+
40+
public static function RFC6265() {
41+
return self::$RFC6265;
42+
}
43+
}
44+
45+
CurlCookieConst::Init();
46+
547
class Curl
648
{
749
const VERSION = '4.8.1';
@@ -543,12 +585,7 @@ public function setCookie($key, $value)
543585
{
544586
$name_chars = array();
545587
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)) {
588+
if (!array_key_exists($name_char, CurlCookieConst::RFC2616)) {
552589
$name_chars[] = rawurlencode($name_char);
553590
} else {
554591
$name_chars[] = $name_char;
@@ -557,20 +594,7 @@ public function setCookie($key, $value)
557594

558595
$value_chars = array();
559596
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)) {
597+
if (!array_key_exists($value_char, CurlCookieConst::RFC6265)) {
574598
$value_chars[] = rawurlencode($value_char);
575599
} else {
576600
$value_chars[] = $value_char;

0 commit comments

Comments
 (0)