+
+ /**
+ * @return GNUsocial_HTTPResponse
+ * @throws HTTP_Request2_Exception
+ */
+ protected function doRequest($url, $method, $headers)
+ {
+ $this->setUrl($url);
+
+ // Workaround for HTTP_Request2 not setting up SNI in socket contexts;
+ // This fixes cert validation for SSL virtual hosts using SNI.
+ // Requires PHP 5.3.2 or later and OpenSSL with SNI support.
+ if ($this->url->getScheme() == 'https' && defined('OPENSSL_TLSEXT_SERVER_NAME')) {
+ $this->config['ssl_SNI_enabled'] = true;
+ $this->config['ssl_SNI_server_name'] = $this->url->getHost();
+ }
+
+ $this->setMethod($method);
+ if ($headers) {
+ foreach ($headers as $header) {
+ $this->setHeader($header);
+ }
+ }
+ $response = $this->send();
+ return $response;
+ }
+
+ protected function log($level, $detail) {
+ $method = $this->getMethod();
+ $url = $this->getUrl();
+ common_log($level, __CLASS__ . ": HTTP $method $url - $detail");
+ }
+
+ /**
+ * Pulls up GNU Social's customized user-agent string, so services
+ * we hit can track down the responsible software.
+ *
+ * @return string
+ */
+ static public function userAgent()
+ {
+ return GNUSOCIAL_ENGINE . '/' . GNUSOCIAL_VERSION
+ . ' (' . GNUSOCIAL_CODENAME . ')';
+ }
+
+ /**
+ * Actually performs the HTTP request and returns a
+ * GNUsocial_HTTPResponse object with response body and header info.
+ *
+ * Wraps around parent send() to add logging and redirection processing.
+ *
+ * @return GNUsocial_HTTPResponse
+ * @throw HTTP_Request2_Exception
+ */
+ public function send()
+ {
+ $maxRedirs = intval($this->config['max_redirs']);
+ if (empty($this->config['follow_redirects'])) {
+ $maxRedirs = 0;
+ }
+ $redirs = 0;
+ do {
+ try {
+ $response = parent::send();
+ } catch (HTTP_Request2_Exception $e) {
+ $this->log(LOG_ERR, $e->getMessage());
+ throw $e;
+ }
+ $code = $response->getStatus();
+ if ($code >= 200 && $code < 300) {
+ $reason = $response->getReasonPhrase();
+ $this->log(LOG_INFO, "$code $reason");
+ } elseif ($code >= 300 && $code < 400) {
+ $url = $this->getUrl();
+ $target = $response->getHeader('Location');
+
+ if (++$redirs >= $maxRedirs) {
+ common_log(LOG_ERR, __CLASS__ . ": Too many redirects: skipping $code redirect from $url to $target");
+ break;
+ }
+ try {
+ $this->setUrl($target);
+ $this->setHeader('Referer', $url);
+ common_log(LOG_INFO, __CLASS__ . ": Following $code redirect from $url to $target");
+ continue;
+ } catch (HTTP_Request2_Exception $e) {
+ common_log(LOG_ERR, __CLASS__ . ": Invalid $code redirect from $url to $target");
+ }
+ } else {
+ $reason = $response->getReasonPhrase();
+ $this->log(LOG_ERR, "$code $reason");
+ }
+ break;
+ } while ($maxRedirs);
+ return new GNUsocial_HTTPResponse($response, $this->getUrl(), $redirs);
+ }
+}
\ No newline at end of file