PluginProbe
ManageWP Worker / 4.9.25
ManageWP Worker v4.9.25
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / src / MWP / Http / Response.php

Response.php in ManageWP Worker 4.9.25, at src/MWP/Http/Response.php

176 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * This file is part of the ManageWP Worker plugin.
4 *
5 * (c) ManageWP LLC <contact@managewp.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11 class MWP_Http_Response implements MWP_Http_ResponseInterface
12 {
13
14 /**
15 * Status codes translation table.
16 *
17 * The list of codes is complete according to the
18 * {@link http://www.iana.org/assignments/http-status-codes/ Hypertext Transfer Protocol (HTTP) Status Code Registry}
19 * (last updated 2012-02-13).
20 *
21 * Unless otherwise noted, the status code is defined in RFC2616.
22 *
23 * @var array
24 */
25 public static $statusTexts = array(
26 100 => 'Continue',
27 101 => 'Switching Protocols',
28 102 => 'Processing', // RFC2518
29 200 => 'OK',
30 201 => 'Created',
31 202 => 'Accepted',
32 203 => 'Non-Authoritative Information',
33 204 => 'No Content',
34 205 => 'Reset Content',
35 206 => 'Partial Content',
36 207 => 'Multi-Status', // RFC4918
37 208 => 'Already Reported', // RFC5842
38 226 => 'IM Used', // RFC3229
39 300 => 'Multiple Choices',
40 301 => 'Moved Permanently',
41 302 => 'Found',
42 303 => 'See Other',
43 304 => 'Not Modified',
44 305 => 'Use Proxy',
45 306 => 'Reserved',
46 307 => 'Temporary Redirect',
47 308 => 'Permanent Redirect', // RFC7238
48 400 => 'Bad Request',
49 401 => 'Unauthorized',
50 402 => 'Payment Required',
51 403 => 'Forbidden',
52 404 => 'Not Found',
53 405 => 'Method Not Allowed',
54 406 => 'Not Acceptable',
55 407 => 'Proxy Authentication Required',
56 408 => 'Request Timeout',
57 409 => 'Conflict',
58 410 => 'Gone',
59 411 => 'Length Required',
60 412 => 'Precondition Failed',
61 413 => 'Request Entity Too Large',
62 414 => 'Request-URI Too Long',
63 415 => 'Unsupported Media Type',
64 416 => 'Requested Range Not Satisfiable',
65 417 => 'Expectation Failed',
66 418 => 'I\'m a teapot', // RFC2324
67 422 => 'Unprocessable Entity', // RFC4918
68 423 => 'Locked', // RFC4918
69 424 => 'Failed Dependency', // RFC4918
70 425 => 'Reserved for WebDAV advanced collections expired proposal', // RFC2817
71 426 => 'Upgrade Required', // RFC2817
72 428 => 'Precondition Required', // RFC6585
73 429 => 'Too Many Requests', // RFC6585
74 431 => 'Request Header Fields Too Large', // RFC6585
75 500 => 'Internal Server Error',
76 501 => 'Not Implemented',
77 502 => 'Bad Gateway',
78 503 => 'Service Unavailable',
79 504 => 'Gateway Timeout',
80 505 => 'HTTP Version Not Supported',
81 506 => 'Variant Also Negotiates (Experimental)', // RFC2295
82 507 => 'Insufficient Storage', // RFC4918
83 508 => 'Loop Detected', // RFC5842
84 510 => 'Not Extended', // RFC2774
85 511 => 'Network Authentication Required', // RFC6585
86 );
87
88 protected $content;
89
90 protected $statusCode;
91
92 protected $headers = array();
93
94 public function __construct($content, $statusCode = 200, array $headers = array())
95 {
96 $this->content = $content;
97 $this->statusCode = $statusCode;
98 $this->headers = array_change_key_case($headers, CASE_LOWER);
99 }
100
101 public function getContentAsString()
102 {
103 return $this->content;
104 }
105
106 /**
107 * @param mixed $content
108 *
109 * @return mixed
110 */
111 public function setContent($content)
112 {
113 $this->content = $content;
114 }
115
116 /**
117 * @return mixed
118 */
119 public function getContent()
120 {
121 return $this->content;
122 }
123
124 /**
125 * @return void
126 */
127 public function send()
128 {
129 $this->sendHeaders();
130
131 // Some plugins leave open buffers and when it happens the returned output is blank
132 $bufferCount = count(ob_list_handlers());
133 while ($bufferCount) {
134 ob_end_clean();
135 --$bufferCount;
136 }
137
138 if ($this instanceof MWP_Http_StreamingResponseInterface) {
139 $stream = $this->createResponseStream();
140
141 ini_set('display_errors', false);
142
143 while (!$stream->eof()) {
144 print $stream->read(1048576);
145 }
146 } else {
147 print $this->getContentAsString();
148 }
149 }
150
151 protected function sendHeaders()
152 {
153 if (headers_sent()) {
154 return;
155 }
156
157 if (function_exists('header_remove')) {
158 /** @handled function */
159 header_remove('Location');
160 }
161
162 $protocol = 'HTTP/1.1';
163 if (isset($_SERVER['SERVER_PROTOCOL']) && $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.0') {
164 $protocol = 'HTTP/1.0';
165 }
166
167 if (isset(self::$statusTexts[$this->statusCode])) {
168 header(sprintf('%s %s %s', $protocol, $this->statusCode, self::$statusTexts[$this->statusCode]), true, $this->statusCode);
169 }
170
171 foreach ($this->headers as $headerName => $headerValue) {
172 header(sprintf('%s: %s', $headerName, $headerValue), false, $this->statusCode);
173 }
174 }
175 }
176