3 * StatusNet, the distributed open-source microblogging tool
5 * Utility for doing HTTP-related things
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Evan Prodromou <evan@status.net>
25 * @copyright 2009 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('STATUSNET')) {
34 require_once 'HTTP/Request2.php';
35 require_once 'HTTP/Request2/Response.php';
38 * Useful structure for HTTP responses
40 * We make HTTP calls in several places, and we have several different
41 * ways of doing them. This class hides the specifics of what underlying
42 * library (curl or PHP-HTTP or whatever) that's used.
44 * This extends the HTTP_Request2_Response class with methods to get info
45 * about any followed redirects.
49 * @author Evan Prodromou <evan@status.net>
50 * @author Brion Vibber <brion@status.net>
51 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
52 * @link http://status.net/
54 class HTTPResponse extends HTTP_Request2_Response
56 function __construct(HTTP_Request2_Response $response, $url, $redirects=0)
58 foreach (get_object_vars($response) as $key => $val) {
61 $this->url = strval($url);
62 $this->redirectCount = intval($redirects);
66 * Get the count of redirects that have been followed, if any.
69 function getRedirectCount()
71 return $this->redirectCount;
75 * Gets the final target URL, after any redirects have been followed.
84 * Check if the response is OK, generally a 200 or other 2xx status code.
89 $status = $this->getStatus();
90 return ($status >= 200 && $status < 300);
95 * Utility class for doing HTTP client stuff
97 * We make HTTP calls in several places, and we have several different
98 * ways of doing them. This class hides the specifics of what underlying
99 * library (curl or PHP-HTTP or whatever) that's used.
101 * This extends the PEAR HTTP_Request2 package:
102 * - sends StatusNet-specific User-Agent header
103 * - 'follow_redirects' config option, defaulting off
104 * - 'max_redirs' config option, defaulting to 10
105 * - extended response class adds getRedirectCount() and getUrl() methods
106 * - get() and post() convenience functions return body content directly
110 * @author Evan Prodromou <evan@status.net>
111 * @author Brion Vibber <brion@status.net>
112 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
113 * @link http://status.net/
116 class HTTPClient extends HTTP_Request2
119 function __construct($url=null, $method=self::METHOD_GET, $config=array())
121 $this->config['max_redirs'] = 10;
122 $this->config['follow_redirects'] = true;
124 // We've had some issues with keepalive breaking with
125 // HEAD requests, such as to youtube which seems to be
126 // emitting chunked encoding info for an empty body
127 // instead of not emitting anything. This may be a
128 // bug on YouTube's end, but the upstream libray
129 // ought to be investigated to see if we can handle
130 // it gracefully in that case as well.
131 $this->config['protocol_version'] = '1.0';
133 parent::__construct($url, $method, $config);
134 $this->setHeader('User-Agent', $this->userAgent());
138 * Convenience/back-compat instantiator
141 public static function start()
143 return new HTTPClient();
147 * Convenience function to run a GET request.
149 * @return HTTPResponse
150 * @throws HTTP_Request2_Exception
152 public function get($url, $headers=array())
154 return $this->doRequest($url, self::METHOD_GET, $headers);
158 * Convenience function to run a HEAD request.
160 * @return HTTPResponse
161 * @throws HTTP_Request2_Exception
163 public function head($url, $headers=array())
165 return $this->doRequest($url, self::METHOD_HEAD, $headers);
169 * Convenience function to POST form data.
172 * @param array $headers optional associative array of HTTP headers
173 * @param array $data optional associative array or blob of form data to submit
174 * @return HTTPResponse
175 * @throws HTTP_Request2_Exception
177 public function post($url, $headers=array(), $data=array())
180 $this->addPostParameter($data);
182 return $this->doRequest($url, self::METHOD_POST, $headers);
186 * @return HTTPResponse
187 * @throws HTTP_Request2_Exception
189 protected function doRequest($url, $method, $headers)
192 $this->setMethod($method);
194 foreach ($headers as $header) {
195 $this->setHeader($header);
198 $response = $this->send();
202 protected function log($level, $detail) {
203 $method = $this->getMethod();
204 $url = $this->getUrl();
205 common_log($level, __CLASS__ . ": HTTP $method $url - $detail");
209 * Pulls up StatusNet's customized user-agent string, so services
210 * we hit can track down the responsible software.
216 return "StatusNet/".STATUSNET_VERSION." (".STATUSNET_CODENAME.")";
220 * Actually performs the HTTP request and returns an HTTPResponse object
221 * with response body and header info.
223 * Wraps around parent send() to add logging and redirection processing.
225 * @return HTTPResponse
226 * @throw HTTP_Request2_Exception
228 public function send()
230 $maxRedirs = intval($this->config['max_redirs']);
231 if (empty($this->config['follow_redirects'])) {
237 $response = parent::send();
238 } catch (HTTP_Request2_Exception $e) {
239 $this->log(LOG_ERR, $e->getMessage());
242 $code = $response->getStatus();
243 if ($code >= 200 && $code < 300) {
244 $reason = $response->getReasonPhrase();
245 $this->log(LOG_INFO, "$code $reason");
246 } elseif ($code >= 300 && $code < 400) {
247 $url = $this->getUrl();
248 $target = $response->getHeader('Location');
250 if (++$redirs >= $maxRedirs) {
251 common_log(LOG_ERR, __CLASS__ . ": Too many redirects: skipping $code redirect from $url to $target");
255 $this->setUrl($target);
256 $this->setHeader('Referer', $url);
257 common_log(LOG_INFO, __CLASS__ . ": Following $code redirect from $url to $target");
259 } catch (HTTP_Request2_Exception $e) {
260 common_log(LOG_ERR, __CLASS__ . ": Invalid $code redirect from $url to $target");
263 $reason = $response->getReasonPhrase();
264 $this->log(LOG_ERR, "$code $reason");
267 } while ($maxRedirs);
268 return new HTTPResponse($response, $this->getUrl(), $redirs);