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() {
70 return $this->redirectCount;
74 * Gets the final target URL, after any redirects have been followed.
83 * Utility class for doing HTTP client stuff
85 * We make HTTP calls in several places, and we have several different
86 * ways of doing them. This class hides the specifics of what underlying
87 * library (curl or PHP-HTTP or whatever) that's used.
89 * This extends the PEAR HTTP_Request2 package:
90 * - sends StatusNet-specific User-Agent header
91 * - 'follow_redirects' config option, defaulting off
92 * - 'max_redirs' config option, defaulting to 10
93 * - extended response class adds getRedirectCount() and getUrl() methods
94 * - get() and post() convenience functions return body content directly
98 * @author Evan Prodromou <evan@status.net>
99 * @author Brion Vibber <brion@status.net>
100 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
101 * @link http://status.net/
104 class HTTPClient extends HTTP_Request2
107 function __construct($url=null, $method=self::METHOD_GET, $config=array())
109 $this->config['max_redirs'] = 10;
110 $this->config['follow_redirects'] = false;
111 parent::__construct($url, $method, $config);
112 $this->setHeader('User-Agent', $this->userAgent());
116 * Convenience function to run a get request and return the response body.
117 * Use when you don't need to get into details of the response.
119 * @return mixed string on success, false on failure
123 $this->setMethod(self::METHOD_GET);
124 return $this->doRequest();
128 * Convenience function to post form data and return the response body.
129 * Use when you don't need to get into details of the response.
131 * @param array associative array of form data to submit
132 * @return mixed string on success, false on failure
134 public function post($data=array())
136 $this->setMethod(self::METHOD_POST);
138 $this->addPostParameter($data);
140 return $this->doRequest();
144 * @return mixed string on success, false on failure
146 protected function doRequest()
149 $response = $this->send();
150 $code = $response->getStatus();
151 if (($code < 200) || ($code >= 400)) {
154 return $response->getBody();
155 } catch (HTTP_Request2_Exception $e) {
156 $this->log(LOG_ERR, $e->getMessage());
161 protected function log($level, $detail) {
162 $method = $this->getMethod();
163 $url = $this->getUrl();
164 common_log($level, __CLASS__ . ": HTTP $method $url - $detail");
168 * Pulls up StatusNet's customized user-agent string, so services
169 * we hit can track down the responsible software.
173 return "StatusNet/".STATUSNET_VERSION." (".STATUSNET_CODENAME.")";
178 $maxRedirs = intval($this->config['max_redirs']);
179 if (empty($this->config['follow_redirects'])) {
185 $response = parent::send();
186 } catch (HTTP_Request2_Exception $e) {
187 $this->log(LOG_ERR, $e->getMessage());
190 $code = $response->getStatus();
191 if ($code >= 200 && $code < 300) {
192 $reason = $response->getReasonPhrase();
193 $this->log(LOG_INFO, "$code $reason");
194 } elseif ($code >= 300 && $code < 400) {
195 $url = $this->getUrl();
196 $target = $response->getHeader('Location');
198 if (++$redirs >= $maxRedirs) {
199 common_log(LOG_ERR, __CLASS__ . ": Too many redirects: skipping $code redirect from $url to $target");
203 $this->setUrl($target);
204 $this->setHeader('Referer', $url);
205 common_log(LOG_INFO, __CLASS__ . ": Following $code redirect from $url to $target");
207 } catch (HTTP_Request2_Exception $e) {
208 common_log(LOG_ERR, __CLASS__ . ": Invalid $code redirect from $url to $target");
211 $reason = $response->getReasonPhrase();
212 $this->log(LOG_ERR, "$code $reason");
215 } while ($maxRedirs);
216 return new HTTPResponse($response, $this->getUrl(), $redirs);