| 1 |
<?php |
| 2 |
/** |
| 3 |
* HTTP handling |
| 4 |
* |
| 5 |
* Handles all HTTP operations and detection. |
| 6 |
* |
| 7 |
* @package System |
| 8 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Decalog\System; |
| 13 |
|
| 14 |
/** |
| 15 |
* Define the HTTP functionality. |
| 16 |
* |
| 17 |
* Handles all HTTP operations and detection. |
| 18 |
* |
| 19 |
* @package System |
| 20 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 21 |
* @since 1.0.0 |
| 22 |
*/ |
| 23 |
class Http { |
| 24 |
|
| 25 |
/** |
| 26 |
* The list of available verbs. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
* @var array $verbs Maintains the verbs list. |
| 30 |
*/ |
| 31 |
public static $verbs = [ 'get', 'head', 'post', 'put', 'delete', 'connect', 'options', 'trace', 'patch', 'unknown' ]; |
| 32 |
|
| 33 |
/** |
| 34 |
* The list of available contexts. |
| 35 |
* |
| 36 |
* @since 1.0.0 |
| 37 |
* @var array $contexts Maintains the contexts list. |
| 38 |
*/ |
| 39 |
public static $contexts = [ 'inbound', 'outbound', 'unknown' ]; |
| 40 |
|
| 41 |
/** |
| 42 |
* The list of available schemes. |
| 43 |
* |
| 44 |
* @since 1.0.0 |
| 45 |
* @var array $schemes Maintains the schemes list. |
| 46 |
*/ |
| 47 |
public static $schemes = [ 'http', 'https', 'unknown' ]; |
| 48 |
|
| 49 |
/** |
| 50 |
* The list of HTTP codes meaning success. |
| 51 |
* |
| 52 |
* @since 1.0.0 |
| 53 |
* @var array $http_success_codes Maintains the success codes list. |
| 54 |
*/ |
| 55 |
public static $http_success_codes = [ 100, 101, 102, 200, 201, 202, 203, 204, 205, 206, 207, 208, 226, 300, 301, 302, 303, 304, 305, 306, 307, 308 ]; |
| 56 |
|
| 57 |
/** |
| 58 |
* The list of HTTP codes meaning effective pass. |
| 59 |
* |
| 60 |
* @since 1.0.0 |
| 61 |
* @var array $http_effective_pass_codes Maintains the effective pass codes list. |
| 62 |
*/ |
| 63 |
public static $http_effective_pass_codes = [ 200, 201, 202, 203, 204, 205, 226 ]; |
| 64 |
|
| 65 |
/** |
| 66 |
* The list of HTTP codes meaning error. |
| 67 |
* |
| 68 |
* @since 1.0.0 |
| 69 |
* @var array $http_error_codes Maintains the error codes list. |
| 70 |
*/ |
| 71 |
public static $http_error_codes = [ 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 422, 423, 424, 425, 426, 428, 429, 431, 444, 449, 450, 451, 494, 495, 496, 497, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 598, 599 ]; |
| 72 |
|
| 73 |
/** |
| 74 |
* The list of HTTP codes meaning global failure. |
| 75 |
* |
| 76 |
* @since 1.0.0 |
| 77 |
* @var array $http_failure_codes Maintains the failure codes list. |
| 78 |
*/ |
| 79 |
public static $http_failure_codes = [ 0 ]; |
| 80 |
|
| 81 |
/** |
| 82 |
* The list of HTTP codes meaning quota problems. |
| 83 |
* |
| 84 |
* @since 1.0.0 |
| 85 |
* @var array $http_quota_codes Maintains the quota problems codes list. |
| 86 |
*/ |
| 87 |
public static $http_quota_codes = [ 429, 509, 999 ]; |
| 88 |
|
| 89 |
/** |
| 90 |
* The list of possible "not" sub TLDs. |
| 91 |
* |
| 92 |
* @since 1.0.0 |
| 93 |
* @var array $not_sub_tlds Maintains the possible "not" sub TLDs list. |
| 94 |
*/ |
| 95 |
public static $not_sub_tlds = [ 'wp' ]; |
| 96 |
|
| 97 |
/** |
| 98 |
* The list of HTTP status. |
| 99 |
* |
| 100 |
* @since 1.0.0 |
| 101 |
* @var array $http_status_codes Maintains the status list. |
| 102 |
*/ |
| 103 |
public static $http_status_codes = [ |
| 104 |
000 => 'Global timeout', |
| 105 |
100 => 'Continue', |
| 106 |
101 => 'Switching Protocols', |
| 107 |
102 => 'Processing', |
| 108 |
200 => 'OK', |
| 109 |
201 => 'Created', |
| 110 |
202 => 'Accepted', |
| 111 |
203 => 'Non-Authoritative Information', |
| 112 |
204 => 'No Content', |
| 113 |
205 => 'Reset Content', |
| 114 |
206 => 'Partial Content', |
| 115 |
207 => 'Multi-Status', |
| 116 |
208 => 'Already Reported', |
| 117 |
226 => 'IM Used', |
| 118 |
300 => 'Multiple Choices', |
| 119 |
301 => 'Moved Permanently', |
| 120 |
302 => 'Found', |
| 121 |
303 => 'See Other', |
| 122 |
304 => 'Not Modified', |
| 123 |
305 => 'Use Proxy', |
| 124 |
306 => '(Unused)', |
| 125 |
307 => 'Temporary Redirect', |
| 126 |
308 => 'Permanent Redirect', |
| 127 |
400 => 'Bad Request', |
| 128 |
401 => 'Unauthorized', |
| 129 |
402 => 'Payment Required', |
| 130 |
403 => 'Forbidden', |
| 131 |
404 => 'Not Found', |
| 132 |
405 => 'Method Not Allowed', |
| 133 |
406 => 'Not Acceptable', |
| 134 |
407 => 'Proxy Authentication Required', |
| 135 |
408 => 'Request Timeout', |
| 136 |
409 => 'Conflict', |
| 137 |
410 => 'Gone', |
| 138 |
411 => 'Length Required', |
| 139 |
412 => 'Precondition Failed', |
| 140 |
413 => 'Request Entity Too Large', |
| 141 |
414 => 'Request-URI Too Long', |
| 142 |
415 => 'Unsupported Media Type', |
| 143 |
416 => 'Requested Range Not Satisfiable', |
| 144 |
417 => 'Expectation Failed', |
| 145 |
418 => 'I\'m a teapot', |
| 146 |
419 => 'Authentication Timeout', |
| 147 |
420 => 'Enhance Your Calm', |
| 148 |
422 => 'Unprocessable Entity', |
| 149 |
423 => 'Locked', |
| 150 |
424 => 'Method Failure', |
| 151 |
425 => 'Unordered Collection', |
| 152 |
426 => 'Upgrade Required', |
| 153 |
428 => 'Precondition Required', |
| 154 |
429 => 'Too Many Requests', |
| 155 |
431 => 'Request Header Fields Too Large', |
| 156 |
444 => 'No Response', |
| 157 |
449 => 'Retry With', |
| 158 |
450 => 'Blocked by Windows Parental Controls', |
| 159 |
451 => 'Unavailable For Legal Reasons', |
| 160 |
494 => 'Request Header Too Large', |
| 161 |
495 => 'Cert Error', |
| 162 |
496 => 'No Cert', |
| 163 |
497 => 'HTTP to HTTPS', |
| 164 |
499 => 'Client Closed Request', |
| 165 |
500 => 'Internal Server Error', |
| 166 |
501 => 'Not Implemented', |
| 167 |
502 => 'Bad Gateway', |
| 168 |
503 => 'Service Unavailable', |
| 169 |
504 => 'Gateway Timeout', |
| 170 |
505 => 'HTTP Version Not Supported', |
| 171 |
506 => 'Variant Also Negotiates', |
| 172 |
507 => 'Insufficient Storage', |
| 173 |
508 => 'Loop Detected', |
| 174 |
509 => 'Bandwidth Limit Exceeded', |
| 175 |
510 => 'Not Extended', |
| 176 |
511 => 'Network Authentication Required', |
| 177 |
598 => 'Network read timeout error', |
| 178 |
599 => 'Network connect timeout error', |
| 179 |
999 => 'Forbidden by quota manager', |
| 180 |
]; |
| 181 |
|
| 182 |
/** |
| 183 |
* Set the plugin user agent. |
| 184 |
* |
| 185 |
* @return string The user agent to use. |
| 186 |
* @since 1.0.0 |
| 187 |
*/ |
| 188 |
public static function user_agent() { |
| 189 |
return 'PerfOps One - ' . DECALOG_PRODUCT_NAME . ' / ' . DECALOG_VERSION . ' (https://perfops.one/' . DECALOG_SLUG . ')'; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Get the top domain of a full fqdn host. |
| 194 |
* |
| 195 |
* @param string $host The host. |
| 196 |
* @param boolean $resolve Optional. Try to resolve the host name. |
| 197 |
* @return string The top domain. |
| 198 |
* @since 1.0.0 |
| 199 |
*/ |
| 200 |
public static function top_domain( $host, $resolve = true ) { |
| 201 |
if ( filter_var( $host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 | FILTER_FLAG_IPV4 ) ) { |
| 202 |
if ( $resolve ) { |
| 203 |
if ( filter_var( $host, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE | FILTER_FLAG_NO_PRIV_RANGE ) ) { |
| 204 |
return self::top_domain( gethostbyaddr( $host ), false ); |
| 205 |
} else { |
| 206 |
return $host; |
| 207 |
} |
| 208 |
} else { |
| 209 |
return $host; |
| 210 |
} |
| 211 |
} |
| 212 |
$parts = explode( '.', $host ); |
| 213 |
$middle_part = array_slice( $parts, -2, 1 ); |
| 214 |
$mid = $middle_part[0]; |
| 215 |
$subtld = ( strlen( $mid ) <= 3 ); |
| 216 |
if ( in_array( $mid, self::$not_sub_tlds, true ) ) { |
| 217 |
$subtld = false; |
| 218 |
} |
| 219 |
$slice = ( $subtld && ( count( $parts ) > 2 ) ) ? 3 : 2; |
| 220 |
$result = implode( '.', array_slice( $parts, ( 0 - $slice ), $slice ) ); |
| 221 |
return $result; |
| 222 |
} |
| 223 |
} |
| 224 |
|