@@ -11,17 +11,17 @@ |
||
11 | 11 | * @package Requests |
12 | 12 | */ |
13 | 13 | class Requests_Exception_HTTP_406 extends Requests_Exception_HTTP { |
14 | - /** |
|
15 | - * HTTP status code |
|
16 | - * |
|
17 | - * @var integer |
|
18 | - */ |
|
19 | - protected $code = 406; |
|
14 | + /** |
|
15 | + * HTTP status code |
|
16 | + * |
|
17 | + * @var integer |
|
18 | + */ |
|
19 | + protected $code = 406; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Reason phrase |
|
23 | - * |
|
24 | - * @var string |
|
25 | - */ |
|
26 | - protected $reason = 'Not Acceptable'; |
|
21 | + /** |
|
22 | + * Reason phrase |
|
23 | + * |
|
24 | + * @var string |
|
25 | + */ |
|
26 | + protected $reason = 'Not Acceptable'; |
|
27 | 27 | } |
28 | 28 | \ No newline at end of file |
@@ -11,17 +11,17 @@ |
||
11 | 11 | * @package Requests |
12 | 12 | */ |
13 | 13 | class Requests_Exception_HTTP_502 extends Requests_Exception_HTTP { |
14 | - /** |
|
15 | - * HTTP status code |
|
16 | - * |
|
17 | - * @var integer |
|
18 | - */ |
|
19 | - protected $code = 502; |
|
14 | + /** |
|
15 | + * HTTP status code |
|
16 | + * |
|
17 | + * @var integer |
|
18 | + */ |
|
19 | + protected $code = 502; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Reason phrase |
|
23 | - * |
|
24 | - * @var string |
|
25 | - */ |
|
26 | - protected $reason = 'Bad Gateway'; |
|
21 | + /** |
|
22 | + * Reason phrase |
|
23 | + * |
|
24 | + * @var string |
|
25 | + */ |
|
26 | + protected $reason = 'Bad Gateway'; |
|
27 | 27 | } |
28 | 28 | \ No newline at end of file |
@@ -13,17 +13,17 @@ |
||
13 | 13 | * @package Requests |
14 | 14 | */ |
15 | 15 | class Requests_Exception_HTTP_511 extends Requests_Exception_HTTP { |
16 | - /** |
|
17 | - * HTTP status code |
|
18 | - * |
|
19 | - * @var integer |
|
20 | - */ |
|
21 | - protected $code = 511; |
|
16 | + /** |
|
17 | + * HTTP status code |
|
18 | + * |
|
19 | + * @var integer |
|
20 | + */ |
|
21 | + protected $code = 511; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Reason phrase |
|
25 | - * |
|
26 | - * @var string |
|
27 | - */ |
|
28 | - protected $reason = 'Network Authentication Required'; |
|
23 | + /** |
|
24 | + * Reason phrase |
|
25 | + * |
|
26 | + * @var string |
|
27 | + */ |
|
28 | + protected $reason = 'Network Authentication Required'; |
|
29 | 29 | } |
30 | 30 | \ No newline at end of file |
@@ -13,17 +13,17 @@ |
||
13 | 13 | * @package Requests |
14 | 14 | */ |
15 | 15 | class Requests_Exception_HTTP_418 extends Requests_Exception_HTTP { |
16 | - /** |
|
17 | - * HTTP status code |
|
18 | - * |
|
19 | - * @var integer |
|
20 | - */ |
|
21 | - protected $code = 418; |
|
16 | + /** |
|
17 | + * HTTP status code |
|
18 | + * |
|
19 | + * @var integer |
|
20 | + */ |
|
21 | + protected $code = 418; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Reason phrase |
|
25 | - * |
|
26 | - * @var string |
|
27 | - */ |
|
28 | - protected $reason = "I'm A Teapot"; |
|
23 | + /** |
|
24 | + * Reason phrase |
|
25 | + * |
|
26 | + * @var string |
|
27 | + */ |
|
28 | + protected $reason = "I'm A Teapot"; |
|
29 | 29 | } |
30 | 30 | \ No newline at end of file |
@@ -11,17 +11,17 @@ |
||
11 | 11 | * @package Requests |
12 | 12 | */ |
13 | 13 | class Requests_Exception_HTTP_405 extends Requests_Exception_HTTP { |
14 | - /** |
|
15 | - * HTTP status code |
|
16 | - * |
|
17 | - * @var integer |
|
18 | - */ |
|
19 | - protected $code = 405; |
|
14 | + /** |
|
15 | + * HTTP status code |
|
16 | + * |
|
17 | + * @var integer |
|
18 | + */ |
|
19 | + protected $code = 405; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Reason phrase |
|
23 | - * |
|
24 | - * @var string |
|
25 | - */ |
|
26 | - protected $reason = 'Method Not Allowed'; |
|
21 | + /** |
|
22 | + * Reason phrase |
|
23 | + * |
|
24 | + * @var string |
|
25 | + */ |
|
26 | + protected $reason = 'Method Not Allowed'; |
|
27 | 27 | } |
28 | 28 | \ No newline at end of file |
@@ -11,61 +11,61 @@ |
||
11 | 11 | * @package Requests |
12 | 12 | */ |
13 | 13 | class Requests_Exception_HTTP extends Requests_Exception { |
14 | - /** |
|
15 | - * HTTP status code |
|
16 | - * |
|
17 | - * @var integer |
|
18 | - */ |
|
19 | - protected $code = 0; |
|
14 | + /** |
|
15 | + * HTTP status code |
|
16 | + * |
|
17 | + * @var integer |
|
18 | + */ |
|
19 | + protected $code = 0; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Reason phrase |
|
23 | - * |
|
24 | - * @var string |
|
25 | - */ |
|
26 | - protected $reason = 'Unknown'; |
|
21 | + /** |
|
22 | + * Reason phrase |
|
23 | + * |
|
24 | + * @var string |
|
25 | + */ |
|
26 | + protected $reason = 'Unknown'; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Create a new exception |
|
30 | - * |
|
31 | - * There is no mechanism to pass in the status code, as this is set by the |
|
32 | - * subclass used. Reason phrases can vary, however. |
|
33 | - * |
|
34 | - * @param string|null $reason Reason phrase |
|
35 | - * @param mixed $data Associated data |
|
36 | - */ |
|
37 | - public function __construct($reason = null, $data = null) { |
|
38 | - if ($reason !== null) { |
|
39 | - $this->reason = $reason; |
|
40 | - } |
|
28 | + /** |
|
29 | + * Create a new exception |
|
30 | + * |
|
31 | + * There is no mechanism to pass in the status code, as this is set by the |
|
32 | + * subclass used. Reason phrases can vary, however. |
|
33 | + * |
|
34 | + * @param string|null $reason Reason phrase |
|
35 | + * @param mixed $data Associated data |
|
36 | + */ |
|
37 | + public function __construct($reason = null, $data = null) { |
|
38 | + if ($reason !== null) { |
|
39 | + $this->reason = $reason; |
|
40 | + } |
|
41 | 41 | |
42 | - $message = sprintf('%d %s', $this->code, $this->reason); |
|
43 | - parent::__construct($message, 'httpresponse', $data, $this->code); |
|
44 | - } |
|
42 | + $message = sprintf('%d %s', $this->code, $this->reason); |
|
43 | + parent::__construct($message, 'httpresponse', $data, $this->code); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Get the status message |
|
48 | - */ |
|
49 | - public function getReason() { |
|
50 | - return $this->reason; |
|
51 | - } |
|
46 | + /** |
|
47 | + * Get the status message |
|
48 | + */ |
|
49 | + public function getReason() { |
|
50 | + return $this->reason; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Get the correct exception class for a given error code |
|
55 | - * |
|
56 | - * @param int|bool $code HTTP status code, or false if unavailable |
|
57 | - * @return string Exception class name to use |
|
58 | - */ |
|
59 | - public static function get_class($code) { |
|
60 | - if (!$code) { |
|
61 | - return 'Requests_Exception_HTTP_Unknown'; |
|
62 | - } |
|
53 | + /** |
|
54 | + * Get the correct exception class for a given error code |
|
55 | + * |
|
56 | + * @param int|bool $code HTTP status code, or false if unavailable |
|
57 | + * @return string Exception class name to use |
|
58 | + */ |
|
59 | + public static function get_class($code) { |
|
60 | + if (!$code) { |
|
61 | + return 'Requests_Exception_HTTP_Unknown'; |
|
62 | + } |
|
63 | 63 | |
64 | - $class = sprintf('Requests_Exception_HTTP_%d', $code); |
|
65 | - if (class_exists($class)) { |
|
66 | - return $class; |
|
67 | - } |
|
64 | + $class = sprintf('Requests_Exception_HTTP_%d', $code); |
|
65 | + if (class_exists($class)) { |
|
66 | + return $class; |
|
67 | + } |
|
68 | 68 | |
69 | - return 'Requests_Exception_HTTP_Unknown'; |
|
70 | - } |
|
69 | + return 'Requests_Exception_HTTP_Unknown'; |
|
70 | + } |
|
71 | 71 | } |
72 | 72 | \ No newline at end of file |
@@ -2,55 +2,55 @@ |
||
2 | 2 | |
3 | 3 | class Requests_Exception_Transport_cURL extends Requests_Exception_Transport { |
4 | 4 | |
5 | - const EASY = 'cURLEasy'; |
|
6 | - const MULTI = 'cURLMulti'; |
|
7 | - const SHARE = 'cURLShare'; |
|
8 | - |
|
9 | - /** |
|
10 | - * cURL error code |
|
11 | - * |
|
12 | - * @var integer |
|
13 | - */ |
|
14 | - protected $code = -1; |
|
15 | - |
|
16 | - /** |
|
17 | - * Which type of cURL error |
|
18 | - * |
|
19 | - * EASY|MULTI|SHARE |
|
20 | - * |
|
21 | - * @var string |
|
22 | - */ |
|
23 | - protected $type = 'Unknown'; |
|
24 | - |
|
25 | - /** |
|
26 | - * Clear text error message |
|
27 | - * |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - protected $reason = 'Unknown'; |
|
31 | - |
|
32 | - public function __construct($message, $type, $data = null, $code = 0) { |
|
33 | - if ($type !== null) { |
|
34 | - $this->type = $type; |
|
35 | - } |
|
36 | - |
|
37 | - if ($code !== null) { |
|
38 | - $this->code = $code; |
|
39 | - } |
|
40 | - |
|
41 | - if ($message !== null) { |
|
42 | - $this->reason = $message; |
|
43 | - } |
|
44 | - |
|
45 | - $message = sprintf('%d %s', $this->code, $this->reason); |
|
46 | - parent::__construct($message, $this->type, $data, $this->code); |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * Get the error message |
|
51 | - */ |
|
52 | - public function getReason() { |
|
53 | - return $this->reason; |
|
54 | - } |
|
5 | + const EASY = 'cURLEasy'; |
|
6 | + const MULTI = 'cURLMulti'; |
|
7 | + const SHARE = 'cURLShare'; |
|
8 | + |
|
9 | + /** |
|
10 | + * cURL error code |
|
11 | + * |
|
12 | + * @var integer |
|
13 | + */ |
|
14 | + protected $code = -1; |
|
15 | + |
|
16 | + /** |
|
17 | + * Which type of cURL error |
|
18 | + * |
|
19 | + * EASY|MULTI|SHARE |
|
20 | + * |
|
21 | + * @var string |
|
22 | + */ |
|
23 | + protected $type = 'Unknown'; |
|
24 | + |
|
25 | + /** |
|
26 | + * Clear text error message |
|
27 | + * |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + protected $reason = 'Unknown'; |
|
31 | + |
|
32 | + public function __construct($message, $type, $data = null, $code = 0) { |
|
33 | + if ($type !== null) { |
|
34 | + $this->type = $type; |
|
35 | + } |
|
36 | + |
|
37 | + if ($code !== null) { |
|
38 | + $this->code = $code; |
|
39 | + } |
|
40 | + |
|
41 | + if ($message !== null) { |
|
42 | + $this->reason = $message; |
|
43 | + } |
|
44 | + |
|
45 | + $message = sprintf('%d %s', $this->code, $this->reason); |
|
46 | + parent::__construct($message, $this->type, $data, $this->code); |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * Get the error message |
|
51 | + */ |
|
52 | + public function getReason() { |
|
53 | + return $this->reason; |
|
54 | + } |
|
55 | 55 | |
56 | 56 | } |
@@ -17,135 +17,135 @@ |
||
17 | 17 | * @since 1.6 |
18 | 18 | */ |
19 | 19 | class Requests_Proxy_HTTP implements Requests_Proxy { |
20 | - /** |
|
21 | - * Proxy host and port |
|
22 | - * |
|
23 | - * Notation: "host:port" (eg 127.0.0.1:8080 or someproxy.com:3128) |
|
24 | - * |
|
25 | - * @var string |
|
26 | - */ |
|
27 | - public $proxy; |
|
20 | + /** |
|
21 | + * Proxy host and port |
|
22 | + * |
|
23 | + * Notation: "host:port" (eg 127.0.0.1:8080 or someproxy.com:3128) |
|
24 | + * |
|
25 | + * @var string |
|
26 | + */ |
|
27 | + public $proxy; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Username |
|
31 | - * |
|
32 | - * @var string |
|
33 | - */ |
|
34 | - public $user; |
|
29 | + /** |
|
30 | + * Username |
|
31 | + * |
|
32 | + * @var string |
|
33 | + */ |
|
34 | + public $user; |
|
35 | 35 | |
36 | - /** |
|
37 | - * Password |
|
38 | - * |
|
39 | - * @var string |
|
40 | - */ |
|
41 | - public $pass; |
|
36 | + /** |
|
37 | + * Password |
|
38 | + * |
|
39 | + * @var string |
|
40 | + */ |
|
41 | + public $pass; |
|
42 | 42 | |
43 | - /** |
|
44 | - * Do we need to authenticate? (ie username & password have been provided) |
|
45 | - * |
|
46 | - * @var boolean |
|
47 | - */ |
|
48 | - public $use_authentication; |
|
43 | + /** |
|
44 | + * Do we need to authenticate? (ie username & password have been provided) |
|
45 | + * |
|
46 | + * @var boolean |
|
47 | + */ |
|
48 | + public $use_authentication; |
|
49 | 49 | |
50 | - /** |
|
51 | - * Constructor |
|
52 | - * |
|
53 | - * @since 1.6 |
|
54 | - * @throws Requests_Exception On incorrect number of arguments (`authbasicbadargs`) |
|
55 | - * @param array|null $args Array of user and password. Must have exactly two elements |
|
56 | - */ |
|
57 | - public function __construct($args = null) { |
|
58 | - if (is_string($args)) { |
|
59 | - $this->proxy = $args; |
|
60 | - } |
|
61 | - elseif (is_array($args)) { |
|
62 | - if (count($args) == 1) { |
|
63 | - list($this->proxy) = $args; |
|
64 | - } |
|
65 | - elseif (count($args) == 3) { |
|
66 | - list($this->proxy, $this->user, $this->pass) = $args; |
|
67 | - $this->use_authentication = true; |
|
68 | - } |
|
69 | - else { |
|
70 | - throw new Requests_Exception('Invalid number of arguments', 'proxyhttpbadargs'); |
|
71 | - } |
|
72 | - } |
|
73 | - } |
|
50 | + /** |
|
51 | + * Constructor |
|
52 | + * |
|
53 | + * @since 1.6 |
|
54 | + * @throws Requests_Exception On incorrect number of arguments (`authbasicbadargs`) |
|
55 | + * @param array|null $args Array of user and password. Must have exactly two elements |
|
56 | + */ |
|
57 | + public function __construct($args = null) { |
|
58 | + if (is_string($args)) { |
|
59 | + $this->proxy = $args; |
|
60 | + } |
|
61 | + elseif (is_array($args)) { |
|
62 | + if (count($args) == 1) { |
|
63 | + list($this->proxy) = $args; |
|
64 | + } |
|
65 | + elseif (count($args) == 3) { |
|
66 | + list($this->proxy, $this->user, $this->pass) = $args; |
|
67 | + $this->use_authentication = true; |
|
68 | + } |
|
69 | + else { |
|
70 | + throw new Requests_Exception('Invalid number of arguments', 'proxyhttpbadargs'); |
|
71 | + } |
|
72 | + } |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Register the necessary callbacks |
|
77 | - * |
|
78 | - * @since 1.6 |
|
79 | - * @see curl_before_send |
|
80 | - * @see fsockopen_remote_socket |
|
81 | - * @see fsockopen_remote_host_path |
|
82 | - * @see fsockopen_header |
|
83 | - * @param Requests_Hooks $hooks Hook system |
|
84 | - */ |
|
85 | - public function register(Requests_Hooks &$hooks) { |
|
86 | - $hooks->register('curl.before_send', array(&$this, 'curl_before_send')); |
|
75 | + /** |
|
76 | + * Register the necessary callbacks |
|
77 | + * |
|
78 | + * @since 1.6 |
|
79 | + * @see curl_before_send |
|
80 | + * @see fsockopen_remote_socket |
|
81 | + * @see fsockopen_remote_host_path |
|
82 | + * @see fsockopen_header |
|
83 | + * @param Requests_Hooks $hooks Hook system |
|
84 | + */ |
|
85 | + public function register(Requests_Hooks &$hooks) { |
|
86 | + $hooks->register('curl.before_send', array(&$this, 'curl_before_send')); |
|
87 | 87 | |
88 | - $hooks->register('fsockopen.remote_socket', array(&$this, 'fsockopen_remote_socket')); |
|
89 | - $hooks->register('fsockopen.remote_host_path', array(&$this, 'fsockopen_remote_host_path')); |
|
90 | - if ($this->use_authentication) { |
|
91 | - $hooks->register('fsockopen.after_headers', array(&$this, 'fsockopen_header')); |
|
92 | - } |
|
93 | - } |
|
88 | + $hooks->register('fsockopen.remote_socket', array(&$this, 'fsockopen_remote_socket')); |
|
89 | + $hooks->register('fsockopen.remote_host_path', array(&$this, 'fsockopen_remote_host_path')); |
|
90 | + if ($this->use_authentication) { |
|
91 | + $hooks->register('fsockopen.after_headers', array(&$this, 'fsockopen_header')); |
|
92 | + } |
|
93 | + } |
|
94 | 94 | |
95 | - /** |
|
96 | - * Set cURL parameters before the data is sent |
|
97 | - * |
|
98 | - * @since 1.6 |
|
99 | - * @param resource $handle cURL resource |
|
100 | - */ |
|
101 | - public function curl_before_send(&$handle) { |
|
102 | - curl_setopt($handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); |
|
103 | - curl_setopt($handle, CURLOPT_PROXY, $this->proxy); |
|
95 | + /** |
|
96 | + * Set cURL parameters before the data is sent |
|
97 | + * |
|
98 | + * @since 1.6 |
|
99 | + * @param resource $handle cURL resource |
|
100 | + */ |
|
101 | + public function curl_before_send(&$handle) { |
|
102 | + curl_setopt($handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); |
|
103 | + curl_setopt($handle, CURLOPT_PROXY, $this->proxy); |
|
104 | 104 | |
105 | - if ($this->use_authentication) { |
|
106 | - curl_setopt($handle, CURLOPT_PROXYAUTH, CURLAUTH_ANY); |
|
107 | - curl_setopt($handle, CURLOPT_PROXYUSERPWD, $this->get_auth_string()); |
|
108 | - } |
|
109 | - } |
|
105 | + if ($this->use_authentication) { |
|
106 | + curl_setopt($handle, CURLOPT_PROXYAUTH, CURLAUTH_ANY); |
|
107 | + curl_setopt($handle, CURLOPT_PROXYUSERPWD, $this->get_auth_string()); |
|
108 | + } |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * Alter remote socket information before opening socket connection |
|
113 | - * |
|
114 | - * @since 1.6 |
|
115 | - * @param string $remote_socket Socket connection string |
|
116 | - */ |
|
117 | - public function fsockopen_remote_socket(&$remote_socket) { |
|
118 | - $remote_socket = $this->proxy; |
|
119 | - } |
|
111 | + /** |
|
112 | + * Alter remote socket information before opening socket connection |
|
113 | + * |
|
114 | + * @since 1.6 |
|
115 | + * @param string $remote_socket Socket connection string |
|
116 | + */ |
|
117 | + public function fsockopen_remote_socket(&$remote_socket) { |
|
118 | + $remote_socket = $this->proxy; |
|
119 | + } |
|
120 | 120 | |
121 | - /** |
|
122 | - * Alter remote path before getting stream data |
|
123 | - * |
|
124 | - * @since 1.6 |
|
125 | - * @param string $path Path to send in HTTP request string ("GET ...") |
|
126 | - * @param string $url Full URL we're requesting |
|
127 | - */ |
|
128 | - public function fsockopen_remote_host_path(&$path, $url) { |
|
129 | - $path = $url; |
|
130 | - } |
|
121 | + /** |
|
122 | + * Alter remote path before getting stream data |
|
123 | + * |
|
124 | + * @since 1.6 |
|
125 | + * @param string $path Path to send in HTTP request string ("GET ...") |
|
126 | + * @param string $url Full URL we're requesting |
|
127 | + */ |
|
128 | + public function fsockopen_remote_host_path(&$path, $url) { |
|
129 | + $path = $url; |
|
130 | + } |
|
131 | 131 | |
132 | - /** |
|
133 | - * Add extra headers to the request before sending |
|
134 | - * |
|
135 | - * @since 1.6 |
|
136 | - * @param string $out HTTP header string |
|
137 | - */ |
|
138 | - public function fsockopen_header(&$out) { |
|
139 | - $out .= sprintf("Proxy-Authorization: Basic %s\r\n", base64_encode($this->get_auth_string())); |
|
140 | - } |
|
132 | + /** |
|
133 | + * Add extra headers to the request before sending |
|
134 | + * |
|
135 | + * @since 1.6 |
|
136 | + * @param string $out HTTP header string |
|
137 | + */ |
|
138 | + public function fsockopen_header(&$out) { |
|
139 | + $out .= sprintf("Proxy-Authorization: Basic %s\r\n", base64_encode($this->get_auth_string())); |
|
140 | + } |
|
141 | 141 | |
142 | - /** |
|
143 | - * Get the authentication string (user:pass) |
|
144 | - * |
|
145 | - * @since 1.6 |
|
146 | - * @return string |
|
147 | - */ |
|
148 | - public function get_auth_string() { |
|
149 | - return $this->user . ':' . $this->pass; |
|
150 | - } |
|
142 | + /** |
|
143 | + * Get the authentication string (user:pass) |
|
144 | + * |
|
145 | + * @since 1.6 |
|
146 | + * @return string |
|
147 | + */ |
|
148 | + public function get_auth_string() { |
|
149 | + return $this->user . ':' . $this->pass; |
|
150 | + } |
|
151 | 151 | } |
152 | 152 | \ No newline at end of file |
@@ -11,378 +11,378 @@ |
||
11 | 11 | * @see https://tools.ietf.org/html/rfc3492 Punycode/Bootstrap specification |
12 | 12 | */ |
13 | 13 | class Requests_IDNAEncoder { |
14 | - /** |
|
15 | - * ACE prefix used for IDNA |
|
16 | - * |
|
17 | - * @see https://tools.ietf.org/html/rfc3490#section-5 |
|
18 | - * @var string |
|
19 | - */ |
|
20 | - const ACE_PREFIX = 'xn--'; |
|
14 | + /** |
|
15 | + * ACE prefix used for IDNA |
|
16 | + * |
|
17 | + * @see https://tools.ietf.org/html/rfc3490#section-5 |
|
18 | + * @var string |
|
19 | + */ |
|
20 | + const ACE_PREFIX = 'xn--'; |
|
21 | 21 | |
22 | - /**#@+ |
|
22 | + /**#@+ |
|
23 | 23 | * Bootstrap constant for Punycode |
24 | 24 | * |
25 | 25 | * @see https://tools.ietf.org/html/rfc3492#section-5 |
26 | 26 | * @var int |
27 | 27 | */ |
28 | - const BOOTSTRAP_BASE = 36; |
|
29 | - const BOOTSTRAP_TMIN = 1; |
|
30 | - const BOOTSTRAP_TMAX = 26; |
|
31 | - const BOOTSTRAP_SKEW = 38; |
|
32 | - const BOOTSTRAP_DAMP = 700; |
|
33 | - const BOOTSTRAP_INITIAL_BIAS = 72; |
|
34 | - const BOOTSTRAP_INITIAL_N = 128; |
|
35 | - /**#@-*/ |
|
28 | + const BOOTSTRAP_BASE = 36; |
|
29 | + const BOOTSTRAP_TMIN = 1; |
|
30 | + const BOOTSTRAP_TMAX = 26; |
|
31 | + const BOOTSTRAP_SKEW = 38; |
|
32 | + const BOOTSTRAP_DAMP = 700; |
|
33 | + const BOOTSTRAP_INITIAL_BIAS = 72; |
|
34 | + const BOOTSTRAP_INITIAL_N = 128; |
|
35 | + /**#@-*/ |
|
36 | 36 | |
37 | - /** |
|
38 | - * Encode a hostname using Punycode |
|
39 | - * |
|
40 | - * @param string $string Hostname |
|
41 | - * @return string Punycode-encoded hostname |
|
42 | - */ |
|
43 | - public static function encode($string) { |
|
44 | - $parts = explode('.', $string); |
|
45 | - foreach ($parts as &$part) { |
|
46 | - $part = self::to_ascii($part); |
|
47 | - } |
|
48 | - return implode('.', $parts); |
|
49 | - } |
|
37 | + /** |
|
38 | + * Encode a hostname using Punycode |
|
39 | + * |
|
40 | + * @param string $string Hostname |
|
41 | + * @return string Punycode-encoded hostname |
|
42 | + */ |
|
43 | + public static function encode($string) { |
|
44 | + $parts = explode('.', $string); |
|
45 | + foreach ($parts as &$part) { |
|
46 | + $part = self::to_ascii($part); |
|
47 | + } |
|
48 | + return implode('.', $parts); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Convert a UTF-8 string to an ASCII string using Punycode |
|
53 | - * |
|
54 | - * @throws Requests_Exception Provided string longer than 64 ASCII characters (`idna.provided_too_long`) |
|
55 | - * @throws Requests_Exception Prepared string longer than 64 ASCII characters (`idna.prepared_too_long`) |
|
56 | - * @throws Requests_Exception Provided string already begins with xn-- (`idna.provided_is_prefixed`) |
|
57 | - * @throws Requests_Exception Encoded string longer than 64 ASCII characters (`idna.encoded_too_long`) |
|
58 | - * |
|
59 | - * @param string $string ASCII or UTF-8 string (max length 64 characters) |
|
60 | - * @return string ASCII string |
|
61 | - */ |
|
62 | - public static function to_ascii($string) { |
|
63 | - // Step 1: Check if the string is already ASCII |
|
64 | - if (self::is_ascii($string)) { |
|
65 | - // Skip to step 7 |
|
66 | - if (strlen($string) < 64) { |
|
67 | - return $string; |
|
68 | - } |
|
51 | + /** |
|
52 | + * Convert a UTF-8 string to an ASCII string using Punycode |
|
53 | + * |
|
54 | + * @throws Requests_Exception Provided string longer than 64 ASCII characters (`idna.provided_too_long`) |
|
55 | + * @throws Requests_Exception Prepared string longer than 64 ASCII characters (`idna.prepared_too_long`) |
|
56 | + * @throws Requests_Exception Provided string already begins with xn-- (`idna.provided_is_prefixed`) |
|
57 | + * @throws Requests_Exception Encoded string longer than 64 ASCII characters (`idna.encoded_too_long`) |
|
58 | + * |
|
59 | + * @param string $string ASCII or UTF-8 string (max length 64 characters) |
|
60 | + * @return string ASCII string |
|
61 | + */ |
|
62 | + public static function to_ascii($string) { |
|
63 | + // Step 1: Check if the string is already ASCII |
|
64 | + if (self::is_ascii($string)) { |
|
65 | + // Skip to step 7 |
|
66 | + if (strlen($string) < 64) { |
|
67 | + return $string; |
|
68 | + } |
|
69 | 69 | |
70 | - throw new Requests_Exception('Provided string is too long', 'idna.provided_too_long', $string); |
|
71 | - } |
|
70 | + throw new Requests_Exception('Provided string is too long', 'idna.provided_too_long', $string); |
|
71 | + } |
|
72 | 72 | |
73 | - // Step 2: nameprep |
|
74 | - $string = self::nameprep($string); |
|
73 | + // Step 2: nameprep |
|
74 | + $string = self::nameprep($string); |
|
75 | 75 | |
76 | - // Step 3: UseSTD3ASCIIRules is false, continue |
|
77 | - // Step 4: Check if it's ASCII now |
|
78 | - if (self::is_ascii($string)) { |
|
79 | - // Skip to step 7 |
|
80 | - if (strlen($string) < 64) { |
|
81 | - return $string; |
|
82 | - } |
|
76 | + // Step 3: UseSTD3ASCIIRules is false, continue |
|
77 | + // Step 4: Check if it's ASCII now |
|
78 | + if (self::is_ascii($string)) { |
|
79 | + // Skip to step 7 |
|
80 | + if (strlen($string) < 64) { |
|
81 | + return $string; |
|
82 | + } |
|
83 | 83 | |
84 | - throw new Requests_Exception('Prepared string is too long', 'idna.prepared_too_long', $string); |
|
85 | - } |
|
84 | + throw new Requests_Exception('Prepared string is too long', 'idna.prepared_too_long', $string); |
|
85 | + } |
|
86 | 86 | |
87 | - // Step 5: Check ACE prefix |
|
88 | - if (strpos($string, self::ACE_PREFIX) === 0) { |
|
89 | - throw new Requests_Exception('Provided string begins with ACE prefix', 'idna.provided_is_prefixed', $string); |
|
90 | - } |
|
87 | + // Step 5: Check ACE prefix |
|
88 | + if (strpos($string, self::ACE_PREFIX) === 0) { |
|
89 | + throw new Requests_Exception('Provided string begins with ACE prefix', 'idna.provided_is_prefixed', $string); |
|
90 | + } |
|
91 | 91 | |
92 | - // Step 6: Encode with Punycode |
|
93 | - $string = self::punycode_encode($string); |
|
92 | + // Step 6: Encode with Punycode |
|
93 | + $string = self::punycode_encode($string); |
|
94 | 94 | |
95 | - // Step 7: Prepend ACE prefix |
|
96 | - $string = self::ACE_PREFIX . $string; |
|
95 | + // Step 7: Prepend ACE prefix |
|
96 | + $string = self::ACE_PREFIX . $string; |
|
97 | 97 | |
98 | - // Step 8: Check size |
|
99 | - if (strlen($string) < 64) { |
|
100 | - return $string; |
|
101 | - } |
|
98 | + // Step 8: Check size |
|
99 | + if (strlen($string) < 64) { |
|
100 | + return $string; |
|
101 | + } |
|
102 | 102 | |
103 | - throw new Requests_Exception('Encoded string is too long', 'idna.encoded_too_long', $string); |
|
104 | - } |
|
103 | + throw new Requests_Exception('Encoded string is too long', 'idna.encoded_too_long', $string); |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * Check whether a given string contains only ASCII characters |
|
108 | - * |
|
109 | - * @internal (Testing found regex was the fastest implementation) |
|
110 | - * |
|
111 | - * @param string $string |
|
112 | - * @return bool Is the string ASCII-only? |
|
113 | - */ |
|
114 | - protected static function is_ascii($string) { |
|
115 | - return (preg_match('/(?:[^\x00-\x7F])/', $string) !== 1); |
|
116 | - } |
|
106 | + /** |
|
107 | + * Check whether a given string contains only ASCII characters |
|
108 | + * |
|
109 | + * @internal (Testing found regex was the fastest implementation) |
|
110 | + * |
|
111 | + * @param string $string |
|
112 | + * @return bool Is the string ASCII-only? |
|
113 | + */ |
|
114 | + protected static function is_ascii($string) { |
|
115 | + return (preg_match('/(?:[^\x00-\x7F])/', $string) !== 1); |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * Prepare a string for use as an IDNA name |
|
120 | - * |
|
121 | - * @todo Implement this based on RFC 3491 and the newer 5891 |
|
122 | - * @param string $string |
|
123 | - * @return string Prepared string |
|
124 | - */ |
|
125 | - protected static function nameprep($string) { |
|
126 | - return $string; |
|
127 | - } |
|
118 | + /** |
|
119 | + * Prepare a string for use as an IDNA name |
|
120 | + * |
|
121 | + * @todo Implement this based on RFC 3491 and the newer 5891 |
|
122 | + * @param string $string |
|
123 | + * @return string Prepared string |
|
124 | + */ |
|
125 | + protected static function nameprep($string) { |
|
126 | + return $string; |
|
127 | + } |
|
128 | 128 | |
129 | - /** |
|
130 | - * Convert a UTF-8 string to a UCS-4 codepoint array |
|
131 | - * |
|
132 | - * Based on Requests_IRI::replace_invalid_with_pct_encoding() |
|
133 | - * |
|
134 | - * @throws Requests_Exception Invalid UTF-8 codepoint (`idna.invalidcodepoint`) |
|
135 | - * @param string $input |
|
136 | - * @return array Unicode code points |
|
137 | - */ |
|
138 | - protected static function utf8_to_codepoints($input) { |
|
139 | - $codepoints = array(); |
|
129 | + /** |
|
130 | + * Convert a UTF-8 string to a UCS-4 codepoint array |
|
131 | + * |
|
132 | + * Based on Requests_IRI::replace_invalid_with_pct_encoding() |
|
133 | + * |
|
134 | + * @throws Requests_Exception Invalid UTF-8 codepoint (`idna.invalidcodepoint`) |
|
135 | + * @param string $input |
|
136 | + * @return array Unicode code points |
|
137 | + */ |
|
138 | + protected static function utf8_to_codepoints($input) { |
|
139 | + $codepoints = array(); |
|
140 | 140 | |
141 | - // Get number of bytes |
|
142 | - $strlen = strlen($input); |
|
141 | + // Get number of bytes |
|
142 | + $strlen = strlen($input); |
|
143 | 143 | |
144 | - for ($position = 0; $position < $strlen; $position++) { |
|
145 | - $value = ord($input[$position]); |
|
144 | + for ($position = 0; $position < $strlen; $position++) { |
|
145 | + $value = ord($input[$position]); |
|
146 | 146 | |
147 | - // One byte sequence: |
|
148 | - if ((~$value & 0x80) === 0x80) { |
|
149 | - $character = $value; |
|
150 | - $length = 1; |
|
151 | - $remaining = 0; |
|
152 | - } |
|
153 | - // Two byte sequence: |
|
154 | - elseif (($value & 0xE0) === 0xC0) { |
|
155 | - $character = ($value & 0x1F) << 6; |
|
156 | - $length = 2; |
|
157 | - $remaining = 1; |
|
158 | - } |
|
159 | - // Three byte sequence: |
|
160 | - elseif (($value & 0xF0) === 0xE0) { |
|
161 | - $character = ($value & 0x0F) << 12; |
|
162 | - $length = 3; |
|
163 | - $remaining = 2; |
|
164 | - } |
|
165 | - // Four byte sequence: |
|
166 | - elseif (($value & 0xF8) === 0xF0) { |
|
167 | - $character = ($value & 0x07) << 18; |
|
168 | - $length = 4; |
|
169 | - $remaining = 3; |
|
170 | - } |
|
171 | - // Invalid byte: |
|
172 | - else { |
|
173 | - throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $value); |
|
174 | - } |
|
147 | + // One byte sequence: |
|
148 | + if ((~$value & 0x80) === 0x80) { |
|
149 | + $character = $value; |
|
150 | + $length = 1; |
|
151 | + $remaining = 0; |
|
152 | + } |
|
153 | + // Two byte sequence: |
|
154 | + elseif (($value & 0xE0) === 0xC0) { |
|
155 | + $character = ($value & 0x1F) << 6; |
|
156 | + $length = 2; |
|
157 | + $remaining = 1; |
|
158 | + } |
|
159 | + // Three byte sequence: |
|
160 | + elseif (($value & 0xF0) === 0xE0) { |
|
161 | + $character = ($value & 0x0F) << 12; |
|
162 | + $length = 3; |
|
163 | + $remaining = 2; |
|
164 | + } |
|
165 | + // Four byte sequence: |
|
166 | + elseif (($value & 0xF8) === 0xF0) { |
|
167 | + $character = ($value & 0x07) << 18; |
|
168 | + $length = 4; |
|
169 | + $remaining = 3; |
|
170 | + } |
|
171 | + // Invalid byte: |
|
172 | + else { |
|
173 | + throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $value); |
|
174 | + } |
|
175 | 175 | |
176 | - if ($remaining > 0) { |
|
177 | - if ($position + $length > $strlen) { |
|
178 | - throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character); |
|
179 | - } |
|
180 | - for ($position++; $remaining > 0; $position++) { |
|
181 | - $value = ord($input[$position]); |
|
176 | + if ($remaining > 0) { |
|
177 | + if ($position + $length > $strlen) { |
|
178 | + throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character); |
|
179 | + } |
|
180 | + for ($position++; $remaining > 0; $position++) { |
|
181 | + $value = ord($input[$position]); |
|
182 | 182 | |
183 | - // If it is invalid, count the sequence as invalid and reprocess the current byte: |
|
184 | - if (($value & 0xC0) !== 0x80) { |
|
185 | - throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character); |
|
186 | - } |
|
183 | + // If it is invalid, count the sequence as invalid and reprocess the current byte: |
|
184 | + if (($value & 0xC0) !== 0x80) { |
|
185 | + throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character); |
|
186 | + } |
|
187 | 187 | |
188 | - $character |= ($value & 0x3F) << (--$remaining * 6); |
|
189 | - } |
|
190 | - $position--; |
|
191 | - } |
|
188 | + $character |= ($value & 0x3F) << (--$remaining * 6); |
|
189 | + } |
|
190 | + $position--; |
|
191 | + } |
|
192 | 192 | |
193 | - if ( |
|
194 | - // Non-shortest form sequences are invalid |
|
195 | - $length > 1 && $character <= 0x7F |
|
196 | - || $length > 2 && $character <= 0x7FF |
|
197 | - || $length > 3 && $character <= 0xFFFF |
|
198 | - // Outside of range of ucschar codepoints |
|
199 | - // Noncharacters |
|
200 | - || ($character & 0xFFFE) === 0xFFFE |
|
201 | - || $character >= 0xFDD0 && $character <= 0xFDEF |
|
202 | - || ( |
|
203 | - // Everything else not in ucschar |
|
204 | - $character > 0xD7FF && $character < 0xF900 |
|
205 | - || $character < 0x20 |
|
206 | - || $character > 0x7E && $character < 0xA0 |
|
207 | - || $character > 0xEFFFD |
|
208 | - ) |
|
209 | - ) { |
|
210 | - throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character); |
|
211 | - } |
|
193 | + if ( |
|
194 | + // Non-shortest form sequences are invalid |
|
195 | + $length > 1 && $character <= 0x7F |
|
196 | + || $length > 2 && $character <= 0x7FF |
|
197 | + || $length > 3 && $character <= 0xFFFF |
|
198 | + // Outside of range of ucschar codepoints |
|
199 | + // Noncharacters |
|
200 | + || ($character & 0xFFFE) === 0xFFFE |
|
201 | + || $character >= 0xFDD0 && $character <= 0xFDEF |
|
202 | + || ( |
|
203 | + // Everything else not in ucschar |
|
204 | + $character > 0xD7FF && $character < 0xF900 |
|
205 | + || $character < 0x20 |
|
206 | + || $character > 0x7E && $character < 0xA0 |
|
207 | + || $character > 0xEFFFD |
|
208 | + ) |
|
209 | + ) { |
|
210 | + throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character); |
|
211 | + } |
|
212 | 212 | |
213 | - $codepoints[] = $character; |
|
214 | - } |
|
213 | + $codepoints[] = $character; |
|
214 | + } |
|
215 | 215 | |
216 | - return $codepoints; |
|
217 | - } |
|
216 | + return $codepoints; |
|
217 | + } |
|
218 | 218 | |
219 | - /** |
|
220 | - * RFC3492-compliant encoder |
|
221 | - * |
|
222 | - * @internal Pseudo-code from Section 6.3 is commented with "#" next to relevant code |
|
223 | - * @throws Requests_Exception On character outside of the domain (never happens with Punycode) (`idna.character_outside_domain`) |
|
224 | - * |
|
225 | - * @param string $input UTF-8 encoded string to encode |
|
226 | - * @return string Punycode-encoded string |
|
227 | - */ |
|
228 | - public static function punycode_encode($input) { |
|
229 | - $output = ''; |
|
219 | + /** |
|
220 | + * RFC3492-compliant encoder |
|
221 | + * |
|
222 | + * @internal Pseudo-code from Section 6.3 is commented with "#" next to relevant code |
|
223 | + * @throws Requests_Exception On character outside of the domain (never happens with Punycode) (`idna.character_outside_domain`) |
|
224 | + * |
|
225 | + * @param string $input UTF-8 encoded string to encode |
|
226 | + * @return string Punycode-encoded string |
|
227 | + */ |
|
228 | + public static function punycode_encode($input) { |
|
229 | + $output = ''; |
|
230 | 230 | # let n = initial_n |
231 | - $n = self::BOOTSTRAP_INITIAL_N; |
|
231 | + $n = self::BOOTSTRAP_INITIAL_N; |
|
232 | 232 | # let delta = 0 |
233 | - $delta = 0; |
|
233 | + $delta = 0; |
|
234 | 234 | # let bias = initial_bias |
235 | - $bias = self::BOOTSTRAP_INITIAL_BIAS; |
|
235 | + $bias = self::BOOTSTRAP_INITIAL_BIAS; |
|
236 | 236 | # let h = b = the number of basic code points in the input |
237 | - $h = $b = 0; // see loop |
|
237 | + $h = $b = 0; // see loop |
|
238 | 238 | # copy them to the output in order |
239 | - $codepoints = self::utf8_to_codepoints($input); |
|
240 | - $extended = array(); |
|
239 | + $codepoints = self::utf8_to_codepoints($input); |
|
240 | + $extended = array(); |
|
241 | 241 | |
242 | - foreach ($codepoints as $char) { |
|
243 | - if ($char < 128) { |
|
244 | - // Character is valid ASCII |
|
245 | - // TODO: this should also check if it's valid for a URL |
|
246 | - $output .= chr($char); |
|
247 | - $h++; |
|
248 | - } |
|
249 | - // Check if the character is non-ASCII, but below initial n |
|
250 | - // This never occurs for Punycode, so ignore in coverage |
|
251 | - // @codeCoverageIgnoreStart |
|
252 | - elseif ($char < $n) { |
|
253 | - throw new Requests_Exception('Invalid character', 'idna.character_outside_domain', $char); |
|
254 | - } |
|
255 | - // @codeCoverageIgnoreEnd |
|
256 | - else { |
|
257 | - $extended[$char] = true; |
|
258 | - } |
|
259 | - } |
|
260 | - $extended = array_keys($extended); |
|
261 | - sort($extended); |
|
262 | - $b = $h; |
|
242 | + foreach ($codepoints as $char) { |
|
243 | + if ($char < 128) { |
|
244 | + // Character is valid ASCII |
|
245 | + // TODO: this should also check if it's valid for a URL |
|
246 | + $output .= chr($char); |
|
247 | + $h++; |
|
248 | + } |
|
249 | + // Check if the character is non-ASCII, but below initial n |
|
250 | + // This never occurs for Punycode, so ignore in coverage |
|
251 | + // @codeCoverageIgnoreStart |
|
252 | + elseif ($char < $n) { |
|
253 | + throw new Requests_Exception('Invalid character', 'idna.character_outside_domain', $char); |
|
254 | + } |
|
255 | + // @codeCoverageIgnoreEnd |
|
256 | + else { |
|
257 | + $extended[$char] = true; |
|
258 | + } |
|
259 | + } |
|
260 | + $extended = array_keys($extended); |
|
261 | + sort($extended); |
|
262 | + $b = $h; |
|
263 | 263 | # [copy them] followed by a delimiter if b > 0 |
264 | - if (strlen($output) > 0) { |
|
265 | - $output .= '-'; |
|
266 | - } |
|
264 | + if (strlen($output) > 0) { |
|
265 | + $output .= '-'; |
|
266 | + } |
|
267 | 267 | # {if the input contains a non-basic code point < n then fail} |
268 | 268 | # while h < length(input) do begin |
269 | - while ($h < count($codepoints)) { |
|
269 | + while ($h < count($codepoints)) { |
|
270 | 270 | # let m = the minimum code point >= n in the input |
271 | - $m = array_shift($extended); |
|
272 | - //printf('next code point to insert is %s' . PHP_EOL, dechex($m)); |
|
271 | + $m = array_shift($extended); |
|
272 | + //printf('next code point to insert is %s' . PHP_EOL, dechex($m)); |
|
273 | 273 | # let delta = delta + (m - n) * (h + 1), fail on overflow |
274 | - $delta += ($m - $n) * ($h + 1); |
|
274 | + $delta += ($m - $n) * ($h + 1); |
|
275 | 275 | # let n = m |
276 | - $n = $m; |
|
276 | + $n = $m; |
|
277 | 277 | # for each code point c in the input (in order) do begin |
278 | - for ($num = 0; $num < count($codepoints); $num++) { |
|
279 | - $c = $codepoints[$num]; |
|
278 | + for ($num = 0; $num < count($codepoints); $num++) { |
|
279 | + $c = $codepoints[$num]; |
|
280 | 280 | # if c < n then increment delta, fail on overflow |
281 | - if ($c < $n) { |
|
282 | - $delta++; |
|
283 | - } |
|
281 | + if ($c < $n) { |
|
282 | + $delta++; |
|
283 | + } |
|
284 | 284 | # if c == n then begin |
285 | - elseif ($c === $n) { |
|
285 | + elseif ($c === $n) { |
|
286 | 286 | # let q = delta |
287 | - $q = $delta; |
|
287 | + $q = $delta; |
|
288 | 288 | # for k = base to infinity in steps of base do begin |
289 | - for ($k = self::BOOTSTRAP_BASE; ; $k += self::BOOTSTRAP_BASE) { |
|
289 | + for ($k = self::BOOTSTRAP_BASE; ; $k += self::BOOTSTRAP_BASE) { |
|
290 | 290 | # let t = tmin if k <= bias {+ tmin}, or |
291 | 291 | # tmax if k >= bias + tmax, or k - bias otherwise |
292 | - if ($k <= ($bias + self::BOOTSTRAP_TMIN)) { |
|
293 | - $t = self::BOOTSTRAP_TMIN; |
|
294 | - } |
|
295 | - elseif ($k >= ($bias + self::BOOTSTRAP_TMAX)) { |
|
296 | - $t = self::BOOTSTRAP_TMAX; |
|
297 | - } |
|
298 | - else { |
|
299 | - $t = $k - $bias; |
|
300 | - } |
|
292 | + if ($k <= ($bias + self::BOOTSTRAP_TMIN)) { |
|
293 | + $t = self::BOOTSTRAP_TMIN; |
|
294 | + } |
|
295 | + elseif ($k >= ($bias + self::BOOTSTRAP_TMAX)) { |
|
296 | + $t = self::BOOTSTRAP_TMAX; |
|
297 | + } |
|
298 | + else { |
|
299 | + $t = $k - $bias; |
|
300 | + } |
|
301 | 301 | # if q < t then break |
302 | - if ($q < $t) { |
|
303 | - break; |
|
304 | - } |
|
302 | + if ($q < $t) { |
|
303 | + break; |
|
304 | + } |
|
305 | 305 | # output the code point for digit t + ((q - t) mod (base - t)) |
306 | - $digit = $t + (($q - $t) % (self::BOOTSTRAP_BASE - $t)); |
|
307 | - $output .= self::digit_to_char($digit); |
|
306 | + $digit = $t + (($q - $t) % (self::BOOTSTRAP_BASE - $t)); |
|
307 | + $output .= self::digit_to_char($digit); |
|
308 | 308 | # let q = (q - t) div (base - t) |
309 | - $q = floor(($q - $t) / (self::BOOTSTRAP_BASE - $t)); |
|
309 | + $q = floor(($q - $t) / (self::BOOTSTRAP_BASE - $t)); |
|
310 | 310 | # end |
311 | - } |
|
311 | + } |
|
312 | 312 | # output the code point for digit q |
313 | - $output .= self::digit_to_char($q); |
|
313 | + $output .= self::digit_to_char($q); |
|
314 | 314 | # let bias = adapt(delta, h + 1, test h equals b?) |
315 | - $bias = self::adapt($delta, $h + 1, $h === $b); |
|
315 | + $bias = self::adapt($delta, $h + 1, $h === $b); |
|
316 | 316 | # let delta = 0 |
317 | - $delta = 0; |
|
317 | + $delta = 0; |
|
318 | 318 | # increment h |
319 | - $h++; |
|
319 | + $h++; |
|
320 | 320 | # end |
321 | - } |
|
321 | + } |
|
322 | 322 | # end |
323 | - } |
|
323 | + } |
|
324 | 324 | # increment delta and n |
325 | - $delta++; |
|
326 | - $n++; |
|
325 | + $delta++; |
|
326 | + $n++; |
|
327 | 327 | # end |
328 | - } |
|
328 | + } |
|
329 | 329 | |
330 | - return $output; |
|
331 | - } |
|
330 | + return $output; |
|
331 | + } |
|
332 | 332 | |
333 | - /** |
|
334 | - * Convert a digit to its respective character |
|
335 | - * |
|
336 | - * @see https://tools.ietf.org/html/rfc3492#section-5 |
|
337 | - * @throws Requests_Exception On invalid digit (`idna.invalid_digit`) |
|
338 | - * |
|
339 | - * @param int $digit Digit in the range 0-35 |
|
340 | - * @return string Single character corresponding to digit |
|
341 | - */ |
|
342 | - protected static function digit_to_char($digit) { |
|
343 | - // @codeCoverageIgnoreStart |
|
344 | - // As far as I know, this never happens, but still good to be sure. |
|
345 | - if ($digit < 0 || $digit > 35) { |
|
346 | - throw new Requests_Exception(sprintf('Invalid digit %d', $digit), 'idna.invalid_digit', $digit); |
|
347 | - } |
|
348 | - // @codeCoverageIgnoreEnd |
|
349 | - $digits = 'abcdefghijklmnopqrstuvwxyz0123456789'; |
|
350 | - return substr($digits, $digit, 1); |
|
351 | - } |
|
333 | + /** |
|
334 | + * Convert a digit to its respective character |
|
335 | + * |
|
336 | + * @see https://tools.ietf.org/html/rfc3492#section-5 |
|
337 | + * @throws Requests_Exception On invalid digit (`idna.invalid_digit`) |
|
338 | + * |
|
339 | + * @param int $digit Digit in the range 0-35 |
|
340 | + * @return string Single character corresponding to digit |
|
341 | + */ |
|
342 | + protected static function digit_to_char($digit) { |
|
343 | + // @codeCoverageIgnoreStart |
|
344 | + // As far as I know, this never happens, but still good to be sure. |
|
345 | + if ($digit < 0 || $digit > 35) { |
|
346 | + throw new Requests_Exception(sprintf('Invalid digit %d', $digit), 'idna.invalid_digit', $digit); |
|
347 | + } |
|
348 | + // @codeCoverageIgnoreEnd |
|
349 | + $digits = 'abcdefghijklmnopqrstuvwxyz0123456789'; |
|
350 | + return substr($digits, $digit, 1); |
|
351 | + } |
|
352 | 352 | |
353 | - /** |
|
354 | - * Adapt the bias |
|
355 | - * |
|
356 | - * @see https://tools.ietf.org/html/rfc3492#section-6.1 |
|
357 | - * @param int $delta |
|
358 | - * @param int $numpoints |
|
359 | - * @param bool $firsttime |
|
360 | - * @return int New bias |
|
361 | - */ |
|
362 | - protected static function adapt($delta, $numpoints, $firsttime) { |
|
353 | + /** |
|
354 | + * Adapt the bias |
|
355 | + * |
|
356 | + * @see https://tools.ietf.org/html/rfc3492#section-6.1 |
|
357 | + * @param int $delta |
|
358 | + * @param int $numpoints |
|
359 | + * @param bool $firsttime |
|
360 | + * @return int New bias |
|
361 | + */ |
|
362 | + protected static function adapt($delta, $numpoints, $firsttime) { |
|
363 | 363 | # function adapt(delta,numpoints,firsttime): |
364 | 364 | # if firsttime then let delta = delta div damp |
365 | - if ($firsttime) { |
|
366 | - $delta = floor($delta / self::BOOTSTRAP_DAMP); |
|
367 | - } |
|
365 | + if ($firsttime) { |
|
366 | + $delta = floor($delta / self::BOOTSTRAP_DAMP); |
|
367 | + } |
|
368 | 368 | # else let delta = delta div 2 |
369 | - else { |
|
370 | - $delta = floor($delta / 2); |
|
371 | - } |
|
369 | + else { |
|
370 | + $delta = floor($delta / 2); |
|
371 | + } |
|
372 | 372 | # let delta = delta + (delta div numpoints) |
373 | - $delta += floor($delta / $numpoints); |
|
373 | + $delta += floor($delta / $numpoints); |
|
374 | 374 | # let k = 0 |
375 | - $k = 0; |
|
375 | + $k = 0; |
|
376 | 376 | # while delta > ((base - tmin) * tmax) div 2 do begin |
377 | - $max = floor(((self::BOOTSTRAP_BASE - self::BOOTSTRAP_TMIN) * self::BOOTSTRAP_TMAX) / 2); |
|
378 | - while ($delta > $max) { |
|
377 | + $max = floor(((self::BOOTSTRAP_BASE - self::BOOTSTRAP_TMIN) * self::BOOTSTRAP_TMAX) / 2); |
|
378 | + while ($delta > $max) { |
|
379 | 379 | # let delta = delta div (base - tmin) |
380 | - $delta = floor($delta / (self::BOOTSTRAP_BASE - self::BOOTSTRAP_TMIN)); |
|
380 | + $delta = floor($delta / (self::BOOTSTRAP_BASE - self::BOOTSTRAP_TMIN)); |
|
381 | 381 | # let k = k + base |
382 | - $k += self::BOOTSTRAP_BASE; |
|
382 | + $k += self::BOOTSTRAP_BASE; |
|
383 | 383 | # end |
384 | - } |
|
384 | + } |
|
385 | 385 | # return k + (((base - tmin + 1) * delta) div (delta + skew)) |
386 | - return $k + floor(((self::BOOTSTRAP_BASE - self::BOOTSTRAP_TMIN + 1) * $delta) / ($delta + self::BOOTSTRAP_SKEW)); |
|
387 | - } |
|
386 | + return $k + floor(((self::BOOTSTRAP_BASE - self::BOOTSTRAP_TMIN + 1) * $delta) / ($delta + self::BOOTSTRAP_SKEW)); |
|
387 | + } |
|
388 | 388 | } |
389 | 389 | \ No newline at end of file |