Mercurial > hg > cmmr2012-drupal-site
comparison vendor/zendframework/zend-feed/src/PubSubHubbub/HttpResponse.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | 5311817fb629 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 /** | |
3 * Zend Framework (http://framework.zend.com/) | |
4 * | |
5 * @link http://github.com/zendframework/zf2 for the canonical source repository | |
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | |
7 * @license http://framework.zend.com/license/new-bsd New BSD License | |
8 */ | |
9 | |
10 namespace Zend\Feed\PubSubHubbub; | |
11 | |
12 class HttpResponse | |
13 { | |
14 /** | |
15 * The body of any response to the current callback request | |
16 * | |
17 * @var string | |
18 */ | |
19 protected $content = ''; | |
20 | |
21 /** | |
22 * Array of headers. Each header is an array with keys 'name' and 'value' | |
23 * | |
24 * @var array | |
25 */ | |
26 protected $headers = []; | |
27 | |
28 /** | |
29 * HTTP response code to use in headers | |
30 * | |
31 * @var int | |
32 */ | |
33 protected $statusCode = 200; | |
34 | |
35 /** | |
36 * Send the response, including all headers | |
37 * | |
38 * @return void | |
39 */ | |
40 public function send() | |
41 { | |
42 $this->sendHeaders(); | |
43 echo $this->getContent(); | |
44 } | |
45 | |
46 /** | |
47 * Send all headers | |
48 * | |
49 * Sends any headers specified. If an {@link setHttpResponseCode() HTTP response code} | |
50 * has been specified, it is sent with the first header. | |
51 * | |
52 * @return void | |
53 */ | |
54 public function sendHeaders() | |
55 { | |
56 if (count($this->headers) || (200 != $this->statusCode)) { | |
57 $this->canSendHeaders(true); | |
58 } elseif (200 == $this->statusCode) { | |
59 return; | |
60 } | |
61 $httpCodeSent = false; | |
62 foreach ($this->headers as $header) { | |
63 if (!$httpCodeSent && $this->statusCode) { | |
64 header($header['name'] . ': ' . $header['value'], $header['replace'], $this->statusCode); | |
65 $httpCodeSent = true; | |
66 } else { | |
67 header($header['name'] . ': ' . $header['value'], $header['replace']); | |
68 } | |
69 } | |
70 if (!$httpCodeSent) { | |
71 header('HTTP/1.1 ' . $this->statusCode); | |
72 } | |
73 } | |
74 | |
75 /** | |
76 * Set a header | |
77 * | |
78 * If $replace is true, replaces any headers already defined with that | |
79 * $name. | |
80 * | |
81 * @param string $name | |
82 * @param string $value | |
83 * @param bool $replace | |
84 * @return \Zend\Feed\PubSubHubbub\HttpResponse | |
85 */ | |
86 public function setHeader($name, $value, $replace = false) | |
87 { | |
88 $name = $this->_normalizeHeader($name); | |
89 $value = (string) $value; | |
90 if ($replace) { | |
91 foreach ($this->headers as $key => $header) { | |
92 if ($name == $header['name']) { | |
93 unset($this->headers[$key]); | |
94 } | |
95 } | |
96 } | |
97 $this->headers[] = [ | |
98 'name' => $name, | |
99 'value' => $value, | |
100 'replace' => $replace, | |
101 ]; | |
102 | |
103 return $this; | |
104 } | |
105 | |
106 /** | |
107 * Check if a specific Header is set and return its value | |
108 * | |
109 * @param string $name | |
110 * @return string|null | |
111 */ | |
112 public function getHeader($name) | |
113 { | |
114 $name = $this->_normalizeHeader($name); | |
115 foreach ($this->headers as $header) { | |
116 if ($header['name'] == $name) { | |
117 return $header['value']; | |
118 } | |
119 } | |
120 } | |
121 | |
122 /** | |
123 * Return array of headers; see {@link $headers} for format | |
124 * | |
125 * @return array | |
126 */ | |
127 public function getHeaders() | |
128 { | |
129 return $this->headers; | |
130 } | |
131 | |
132 /** | |
133 * Can we send headers? | |
134 * | |
135 * @param bool $throw Whether or not to throw an exception if headers have been sent; defaults to false | |
136 * @return HttpResponse | |
137 * @throws Exception\RuntimeException | |
138 */ | |
139 public function canSendHeaders($throw = false) | |
140 { | |
141 $ok = headers_sent($file, $line); | |
142 if ($ok && $throw) { | |
143 throw new Exception\RuntimeException('Cannot send headers; headers already sent in ' . $file . ', line ' . $line); | |
144 } | |
145 return !$ok; | |
146 } | |
147 | |
148 /** | |
149 * Set HTTP response code to use with headers | |
150 * | |
151 * @param int $code | |
152 * @return HttpResponse | |
153 * @throws Exception\InvalidArgumentException | |
154 */ | |
155 public function setStatusCode($code) | |
156 { | |
157 if (!is_int($code) || (100 > $code) || (599 < $code)) { | |
158 throw new Exception\InvalidArgumentException('Invalid HTTP response' | |
159 . ' code:' . $code); | |
160 } | |
161 $this->statusCode = $code; | |
162 return $this; | |
163 } | |
164 | |
165 /** | |
166 * Retrieve HTTP response code | |
167 * | |
168 * @return int | |
169 */ | |
170 public function getStatusCode() | |
171 { | |
172 return $this->statusCode; | |
173 } | |
174 | |
175 /** | |
176 * Set body content | |
177 * | |
178 * @param string $content | |
179 * @return \Zend\Feed\PubSubHubbub\HttpResponse | |
180 */ | |
181 public function setContent($content) | |
182 { | |
183 $this->content = (string) $content; | |
184 $this->setHeader('content-length', strlen($content)); | |
185 return $this; | |
186 } | |
187 | |
188 /** | |
189 * Return the body content | |
190 * | |
191 * @return string | |
192 */ | |
193 public function getContent() | |
194 { | |
195 return $this->content; | |
196 } | |
197 | |
198 /** | |
199 * Normalizes a header name to X-Capitalized-Names | |
200 * | |
201 * @param string $name | |
202 * @return string | |
203 */ | |
204 protected function _normalizeHeader($name) | |
205 { | |
206 $filtered = str_replace(['-', '_'], ' ', (string) $name); | |
207 $filtered = ucwords(strtolower($filtered)); | |
208 $filtered = str_replace(' ', '-', $filtered); | |
209 return $filtered; | |
210 } | |
211 } |