|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace alkemann\h2l\util; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Http |
|
7
|
|
|
* |
|
8
|
|
|
* Holds HTTP Spec constants, |
|
9
|
|
|
* |
|
10
|
|
|
* @package alkemann\h2l\util |
|
11
|
|
|
*/ |
|
12
|
|
|
class Http |
|
13
|
|
|
{ |
|
14
|
|
|
const TRACE = 'TRACE'; |
|
15
|
|
|
const HEAD = 'HEAD'; |
|
16
|
|
|
const POST = 'POST'; |
|
17
|
|
|
const CONNECT = 'CONNECT'; |
|
18
|
|
|
const OPTIONS = 'OPTIONS'; |
|
19
|
|
|
const PUT = 'PUT'; |
|
20
|
|
|
const PATCH = 'PATCH'; |
|
21
|
|
|
const DELETE = 'DELETE'; |
|
22
|
|
|
const GET = 'GET'; |
|
23
|
|
|
|
|
24
|
|
|
const CONTENT_HTML = 'text/html'; |
|
25
|
|
|
const CONTENT_TEXT_XML = 'text/xml'; |
|
26
|
|
|
const CONTENT_XML = 'application/xml'; |
|
27
|
|
|
const CONTENT_TEXT = 'text/plain'; |
|
28
|
|
|
const CONTENT_FORM = 'application/x-www-form-urlencoded'; |
|
29
|
|
|
const CONTENT_JSON = 'application/json'; |
|
30
|
|
|
|
|
31
|
|
|
const CODE_CONTINUE = 100; |
|
32
|
|
|
const CODE_SWITCHING_PROTOCOLS = 101; |
|
33
|
|
|
|
|
34
|
|
|
const CODE_OK = 200; |
|
35
|
|
|
const CODE_CREATED = 201; |
|
36
|
|
|
const CODE_ACCEPTED = 202; |
|
37
|
|
|
const CODE_NON_AUTHORITATIVE_INFORMATION = 203; |
|
38
|
|
|
const CODE_NO_CONTENT = 204; |
|
39
|
|
|
const CODE_RESET_CONTENT = 205; |
|
40
|
|
|
const CODE_PARTIAL_CONTENT = 206; |
|
41
|
|
|
|
|
42
|
|
|
const CODE_MULTIPLE_CHOICES = 300; |
|
43
|
|
|
const CODE_MOVED_PERMANENTLY = 301; |
|
44
|
|
|
const CODE_FOUND = 302; |
|
45
|
|
|
const CODE_SEE_OTHER = 303; |
|
46
|
|
|
const CODE_NOT_MODIFIED = 304; |
|
47
|
|
|
const CODE_USE_PROXY = 305; |
|
48
|
|
|
const CODE_TEMPORARY_REDIRECT = 307; |
|
49
|
|
|
|
|
50
|
|
|
const CODE_BAD_REQUEST = 400; |
|
51
|
|
|
const CODE_UNAUTHORIZED = 401; |
|
52
|
|
|
const CODE_PAYMENT_REQUIRED = 402; |
|
53
|
|
|
const CODE_FORBIDDEN = 403; |
|
54
|
|
|
const CODE_NOT_FOUND = 404; |
|
55
|
|
|
const CODE_METHOD_NOT_ALLOWED = 405; |
|
56
|
|
|
const CODE_NOT_ACCEPTABLE = 406; |
|
57
|
|
|
const CODE_PROXY_AUTHENTICATION_REQUIRED = 407; |
|
58
|
|
|
const CODE_REQUEST_TIMEOUT = 408; |
|
59
|
|
|
const CODE_CONFLICT = 409; |
|
60
|
|
|
const CODE_GONE = 410; |
|
61
|
|
|
const CODE_LENGTH_REQUIRED = 411; |
|
62
|
|
|
const CODE_PRECONDITION_FAILED = 412; |
|
63
|
|
|
const CODE_REQUEST_ENTITY_TOO_LARGE = 413; |
|
64
|
|
|
const CODE_REQUEST_URI_TOO_LONG = 414; |
|
65
|
|
|
const CODE_UNSUPPORTED_MEDIA_TYPE = 415; |
|
66
|
|
|
const CODE_REQUESTED_RANGE_NOT_SATISFIABLE = 416; |
|
67
|
|
|
const CODE_EXPECTATION_FAILED = 417; |
|
68
|
|
|
|
|
69
|
|
|
const CODE_INTERNAL_SERVER_ERROR = 500; |
|
70
|
|
|
const CODE_NOT_IMPLEMENTED = 501; |
|
71
|
|
|
const CODE_BAD_GATEWAY = 502; |
|
72
|
|
|
const CODE_SERVICE_UNAVAILABLE = 503; |
|
73
|
|
|
const CODE_GATEWAY_TIMEOUT = 504; |
|
74
|
|
|
const CODE_HTTP_VERSION_NOT_SUPPORTED = 505; |
|
75
|
|
|
|
|
76
|
|
|
private static $code_to_message = [ |
|
77
|
|
|
// Informational 1xx |
|
78
|
|
|
100 => 'Continue', |
|
79
|
|
|
101 => 'Switching Protocols', |
|
80
|
|
|
// Successful 2xx |
|
81
|
|
|
200 => 'OK', |
|
82
|
|
|
201 => 'Created', |
|
83
|
|
|
202 => 'Accepted', |
|
84
|
|
|
203 => 'Non-Authoritative Information', |
|
85
|
|
|
204 => 'No Content', |
|
86
|
|
|
205 => 'Reset Content', |
|
87
|
|
|
206 => 'Partial Content', |
|
88
|
|
|
// Redirection 3xx |
|
89
|
|
|
300 => 'Multiple Choices', |
|
90
|
|
|
301 => 'Moved Permanently', |
|
91
|
|
|
302 => 'Found', |
|
92
|
|
|
303 => 'See Other', |
|
93
|
|
|
304 => 'Not Modified', |
|
94
|
|
|
305 => 'Use Proxy', |
|
95
|
|
|
307 => 'Temporary Redirect', |
|
96
|
|
|
// Client Error 4xx |
|
97
|
|
|
400 => 'Bad Request', |
|
98
|
|
|
401 => 'Unauthorized', |
|
99
|
|
|
402 => 'Payment Required', |
|
100
|
|
|
403 => 'Forbidden', |
|
101
|
|
|
404 => 'Not Found', |
|
102
|
|
|
405 => 'Method Not Allowed', |
|
103
|
|
|
406 => 'Not Acceptable', |
|
104
|
|
|
407 => 'Proxy Authentication Required', |
|
105
|
|
|
408 => 'Request Timeout', |
|
106
|
|
|
409 => 'Conflict', |
|
107
|
|
|
410 => 'Gone', |
|
108
|
|
|
411 => 'Length Required', |
|
109
|
|
|
412 => 'Precondition Failed', |
|
110
|
|
|
413 => 'Request Entity Too Large', |
|
111
|
|
|
414 => 'Request-URI Too Long', |
|
112
|
|
|
415 => 'Unsupported Media Type', |
|
113
|
|
|
416 => 'Requested Range Not Satisfiable', |
|
114
|
|
|
417 => 'Expectation Failed', |
|
115
|
|
|
// Server Error 5xx |
|
116
|
|
|
500 => 'Internal Server Error', |
|
117
|
|
|
501 => 'Not Implemented', |
|
118
|
|
|
502 => 'Bad Gateway', |
|
119
|
|
|
503 => 'Service Unavailable', |
|
120
|
|
|
504 => 'Gateway Timeout', |
|
121
|
|
|
505 => 'HTTP Version Not Supported', |
|
122
|
|
|
]; |
|
123
|
|
|
|
|
124
|
|
|
public static function httpCodeToMessage(int $code): string |
|
125
|
|
|
{ |
|
126
|
|
|
return self::$code_to_message[$code] ?? "Unknown"; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public static function getRequestHeadersFromServerArray(array $server_array) |
|
130
|
|
|
{ |
|
131
|
|
|
$out = []; |
|
132
|
|
|
foreach ($server_array as $name => $value) { |
|
133
|
|
|
if (substr($name, 0, 5) == "HTTP_") { |
|
134
|
|
|
$name = str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($name, 5))))); |
|
135
|
|
|
$out[$name] = $value; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
if (array_key_exists("CONTENT_TYPE", $server_array)) { |
|
139
|
|
|
$out["Content-Type"] = $server_array['CONTENT_TYPE']; |
|
140
|
|
|
} |
|
141
|
|
|
if (array_key_exists("CONTENT_LENGTH", $server_array)) { |
|
142
|
|
|
$out["Content-Length"] = $server_array['CONTENT_LENGTH']; |
|
143
|
|
|
} |
|
144
|
|
|
return $out; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|