|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* DronePHP (http://www.dronephp.com) |
|
4
|
|
|
* |
|
5
|
|
|
* @link http://github.com/Pleets/DronePHP |
|
6
|
|
|
* @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org) |
|
7
|
|
|
* @license http://www.dronephp.com/license |
|
8
|
|
|
* @author Darío Rivera <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Drone\Network; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Http class |
|
15
|
|
|
* |
|
16
|
|
|
* Helper class to send http headers |
|
17
|
|
|
*/ |
|
18
|
|
|
class Http |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Status codes |
|
22
|
|
|
* As per http://php.net/manual/en/function.header.php, See the » HTTP/1.1 specification |
|
23
|
|
|
* |
|
24
|
|
|
* @var integer |
|
25
|
|
|
* @link http://www.faqs.org/rfcs/rfc2616.html |
|
26
|
|
|
*/ |
|
27
|
|
|
const HTTP_CONTINUE = 100; |
|
28
|
|
|
const HTTP_SWITCHING_PROTOCOLS = 101; |
|
29
|
|
|
const HTTP_OK = 200; |
|
30
|
|
|
const HTTP_CREATED = 201; |
|
31
|
|
|
const HTTP_ACCEPTED = 202; |
|
32
|
|
|
const HTTP_NON_AUTHORITATIVE_INFORMATION = 203; |
|
33
|
|
|
const HTTP_NO_CONTENT = 204; |
|
34
|
|
|
const HTTP_RESET_CONTENT = 205; |
|
35
|
|
|
const HTTP_PARTIAL_CONTENT = 206; |
|
36
|
|
|
const HTTP_MULTIPLE_CHOICES = 300; |
|
37
|
|
|
const HTTP_MOVED_PERMANENTLY = 301; |
|
38
|
|
|
const HTTP_FOUND = 302; |
|
39
|
|
|
const HTTP_SEE_OTHER = 303; |
|
40
|
|
|
const HTTP_NOT_MODIFIED = 304; |
|
41
|
|
|
const HTTP_USE_PROXY = 305; |
|
42
|
|
|
const HTTP_RESERVED = 306; |
|
43
|
|
|
const HTTP_TEMPORARY_REDIRECT = 307; |
|
44
|
|
|
const HTTP_BAD_REQUEST = 400; |
|
45
|
|
|
const HTTP_UNAUTHORIZED = 401; |
|
46
|
|
|
const HTTP_PAYMENT_REQUIRED = 402; |
|
47
|
|
|
const HTTP_FORBIDDEN = 403; |
|
48
|
|
|
const HTTP_NOT_FOUND = 404; |
|
49
|
|
|
const HTTP_METHOD_NOT_ALLOWED = 405; |
|
50
|
|
|
const HTTP_NOT_ACCEPTABLE = 406; |
|
51
|
|
|
const HTTP_PROXY_AUTHENTICATION_REQUIRED = 407; |
|
52
|
|
|
const HTTP_REQUEST_TIMEOUT = 408; |
|
53
|
|
|
const HTTP_CONFLICT = 409; |
|
54
|
|
|
const HTTP_GONE = 410; |
|
55
|
|
|
const HTTP_LENGTH_REQUIRED = 411; |
|
56
|
|
|
const HTTP_PRECONDITION_FAILED = 412; |
|
57
|
|
|
const HTTP_REQUEST_ENTITY_TOO_LARGE = 413; |
|
58
|
|
|
const HTTP_REQUEST_URI_TOO_LONG = 414; |
|
59
|
|
|
const HTTP_UNSUPPORTED_MEDIA_TYPE = 415; |
|
60
|
|
|
const HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416; |
|
61
|
|
|
const HTTP_EXPECTATION_FAILED = 417; |
|
62
|
|
|
const HTTP_INTERNAL_SERVER_ERROR = 500; |
|
63
|
|
|
const HTTP_NOT_IMPLEMENTED = 501; |
|
64
|
|
|
const HTTP_BAD_GATEWAY = 502; |
|
65
|
|
|
const HTTP_SERVICE_UNAVAILABLE = 503; |
|
66
|
|
|
const HTTP_GATEWAY_TIMEOUT = 504; |
|
67
|
|
|
const HTTP_VERSION_NOT_SUPPORTED = 505; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Status codes and their respective description |
|
71
|
|
|
* |
|
72
|
|
|
* @var array |
|
73
|
|
|
*/ |
|
74
|
|
|
protected $httpStatusCodes = [ |
|
75
|
|
|
self::HTTP_CONTINUE => 'Continue', |
|
76
|
|
|
self::HTTP_SWITCHING_PROTOCOLS => 'Switching Protocols', |
|
77
|
|
|
self::HTTP_OK => 'OK', |
|
78
|
|
|
self::HTTP_CREATED => 'Created', |
|
79
|
|
|
self::HTTP_ACCEPTED => 'Accepted', |
|
80
|
|
|
self::HTTP_NON_AUTHORITATIVE_INFORMATION => 'Non-Authoritative Information', |
|
81
|
|
|
self::HTTP_NO_CONTENT => 'No Content', |
|
82
|
|
|
self::HTTP_RESET_CONTENT => 'Reset Content', |
|
83
|
|
|
self::HTTP_PARTIAL_CONTENT => 'Partial Content', |
|
84
|
|
|
self::HTTP_MULTIPLE_CHOICES => 'Multiple Choices', |
|
85
|
|
|
self::HTTP_MOVED_PERMANENTLY => 'Moved Permanently', |
|
86
|
|
|
self::HTTP_FOUND => 'Found', |
|
87
|
|
|
self::HTTP_SEE_OTHER => 'See Other', |
|
88
|
|
|
self::HTTP_NOT_MODIFIED => 'Not Modified', |
|
89
|
|
|
self::HTTP_USE_PROXY => 'Use Proxy', |
|
90
|
|
|
self::HTTP_TEMPORARY_REDIRECT => 'Temporary Redirect', |
|
91
|
|
|
self::HTTP_BAD_REQUEST => 'Bad Request', |
|
92
|
|
|
self::HTTP_UNAUTHORIZED => 'Unauthorized', |
|
93
|
|
|
self::HTTP_PAYMENT_REQUIRED => 'Payment Required', |
|
94
|
|
|
self::HTTP_FORBIDDEN => 'Forbidden', |
|
95
|
|
|
self::HTTP_NOT_FOUND => 'Not Found', |
|
96
|
|
|
self::HTTP_METHOD_NOT_ALLOWED => 'Method Not Allowed', |
|
97
|
|
|
self::HTTP_NOT_ACCEPTABLE => 'Not Acceptable', |
|
98
|
|
|
self::HTTP_PROXY_AUTHENTICATION_REQUIRED => 'Proxy Authentication Required', |
|
99
|
|
|
self::HTTP_REQUEST_TIMEOUT => 'Request Time-out', |
|
100
|
|
|
self::HTTP_CONFLICT => 'Conflict', |
|
101
|
|
|
self::HTTP_GONE => 'Gone', |
|
102
|
|
|
self::HTTP_LENGTH_REQUIRED => 'Length Required', |
|
103
|
|
|
self::HTTP_PRECONDITION_FAILED => 'Precondition Failed', |
|
104
|
|
|
self::HTTP_REQUEST_ENTITY_TOO_LARGE => 'Request Entity Too Large', |
|
105
|
|
|
self::HTTP_REQUEST_URI_TOO_LONG => 'Request-URI Too Large', |
|
106
|
|
|
self::HTTP_UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type', |
|
107
|
|
|
self::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE => 'Requested range not satisfiable', |
|
108
|
|
|
self::HTTP_EXPECTATION_FAILED => 'Expectation Failed', |
|
109
|
|
|
self::HTTP_INTERNAL_SERVER_ERROR => 'Internal Server Error', |
|
110
|
|
|
self::HTTP_NOT_IMPLEMENTED => 'Not Implemented', |
|
111
|
|
|
self::HTTP_BAD_GATEWAY => 'Bad Gateway', |
|
112
|
|
|
self::HTTP_SERVICE_UNAVAILABLE => 'Service Unavailable', |
|
113
|
|
|
self::HTTP_GATEWAY_TIMEOUT => 'Gateway Time-out', |
|
114
|
|
|
self::HTTP_VERSION_NOT_SUPPORTED => 'HTTP Version not supported', |
|
115
|
|
|
]; |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Gets the HTTP Status description from the code |
|
119
|
|
|
* |
|
120
|
|
|
* @param integer $code |
|
121
|
|
|
* |
|
122
|
|
|
* @throws RuntimeException |
|
123
|
|
|
* |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
3 |
|
public function getStatusText($code) |
|
127
|
|
|
{ |
|
128
|
3 |
|
$codes = $this->httpStatusCodes; |
|
129
|
|
|
|
|
130
|
3 |
|
if (!in_array($code, array_keys($codes))) { |
|
131
|
1 |
|
throw new \RuntimeException("Status code not supported"); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
2 |
|
return $this->httpStatusCodes[$code]; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Sets the HTTP Status Header |
|
139
|
|
|
* |
|
140
|
|
|
* @param integer $code |
|
141
|
|
|
* |
|
142
|
|
|
* @return string |
|
143
|
|
|
*/ |
|
144
|
1 |
|
public function writeStatus($code) |
|
145
|
|
|
{ |
|
146
|
1 |
|
$description = $this->getStatusText($code); |
|
147
|
1 |
|
$protocol = array_key_exists('SERVER_PROTOCOL', $_SERVER) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0'; |
|
148
|
1 |
|
$header = $protocol . " $code $description"; |
|
149
|
|
|
|
|
150
|
1 |
|
header($header); |
|
151
|
|
|
|
|
152
|
1 |
|
return $header; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|