/** * Send a HTTP request to a URI using HTTP extension. * * Does not support non-blocking. * * @param string $url The URL to handle. * @param str|array $args Optional. Override the defaults. * * @return array 'headers', 'body', 'cookies' and 'response' keys. */ protected function _doExecute($url, $args) { switch ($args[org_tubepress_impl_http_HttpClientChain::ARGS_METHOD]) { case org_tubepress_api_http_HttpClient::HTTP_METHOD_POST: $args[org_tubepress_impl_http_HttpClientChain::ARGS_METHOD] = HTTP_METH_POST; break; case org_tubepress_api_http_HttpClient::HTTP_METHOD_PUT: $args[org_tubepress_impl_http_HttpClientChain::ARGS_METHOD] = HTTP_METH_PUT; break; case org_tubepress_api_http_HttpClient::HTTP_METHOD_GET: default: $args[org_tubepress_impl_http_HttpClientChain::ARGS_METHOD] = HTTP_METH_GET; } $urlAsArray = parse_url($url); if ('http' != $urlAsArray['scheme'] && 'https' != $urlAsArray['scheme']) { $url = preg_replace('|^' . preg_quote($urlAsArray['scheme'], '|') . '|', 'http', $url); } $sslVerify = isset($args[org_tubepress_impl_http_HttpClientChain::ARGS_SSL_VERIFY]) && $args[org_tubepress_impl_http_HttpClientChain::ARGS_SSL_VERIFY]; $args[org_tubepress_impl_http_HttpClientChain::ARGS_TIMEOUT] = (int) ceil($args[org_tubepress_impl_http_HttpClientChain::ARGS_TIMEOUT]); $options = array('timeout' => $args[org_tubepress_impl_http_HttpClientChain::ARGS_TIMEOUT], 'connecttimeout' => $args[org_tubepress_impl_http_HttpClientChain::ARGS_TIMEOUT], 'redirect' => 5, 'useragent' => $args[org_tubepress_impl_http_HttpClientChain::ARGS_USER_AGENT], 'headers' => $args[org_tubepress_impl_http_HttpClientChain::ARGS_HEADERS], 'ssl' => array('verifypeer' => $sslVerify, 'verifyhost' => $sslVerify)); $strResponse = @http_request($args[org_tubepress_impl_http_HttpClientChain::ARGS_METHOD], $url, $args[org_tubepress_impl_http_HttpClientChain::ARGS_BODY], $options, $info); // Error may still be set, Response may return headers or partial document, and error // contains a reason the request was aborted, eg, timeout expired or max-redirects reached. if (false === $strResponse || !empty($info['error'])) { throw new Exception($info['response_code'] . ': ' . $info['error']); } $headersBody = self::_breakRawStringResponseIntoHeaderAndBody($strResponse); $theHeaders = $headersBody[org_tubepress_impl_http_HttpClientChain::ARGS_HEADERS]; $theBody = $headersBody[org_tubepress_impl_http_HttpClientChain::ARGS_BODY]; unset($headersBody); $theHeaders = self::_getProcessedHeaders($theHeaders); if (!empty($theBody) && isset($theHeaders['headers']['transfer-encoding']) && 'chunked' == $theHeaders['headers']['transfer-encoding']) { $theBody = @http_chunked_decode($theBody); } if (true === $args['decompress'] && true === org_tubepress_impl_http_clientimpl_Encoding::shouldDecode($theHeaders['headers'])) { $theBody = http_inflate($theBody); } return array('headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $theHeaders['response'], 'cookies' => $theHeaders['cookies']); }
/** * Send a HTTP request to a URI using cURL extension. * * @param string $url The URL to handle. * @param str|array $r Optional. Override the defaults. * * @return array 'headers', 'body', 'cookies' and 'response' keys. */ protected function _doExecute($url, $r) { $handle = curl_init(); $sslVerify = isset($r[org_tubepress_impl_http_HttpClientChain::ARGS_SSL_VERIFY]) && $r[org_tubepress_impl_http_HttpClientChain::ARGS_SSL_VERIFY]; // CURLOPT_TIMEOUT and CURLOPT_CONNECTTIMEOUT expect integers. Have to use ceil since // a value of 0 will allow an ulimited timeout. $timeout = (int) ceil($r[org_tubepress_impl_http_HttpClientChain::ARGS_TIMEOUT]); curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($handle, CURLOPT_TIMEOUT, $timeout); curl_setopt($handle, CURLOPT_URL, $url); curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, $sslVerify); curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, $sslVerify); curl_setopt($handle, CURLOPT_USERAGENT, $r[org_tubepress_impl_http_HttpClientChain::ARGS_USER_AGENT]); curl_setopt($handle, CURLOPT_MAXREDIRS, 5); switch ($r[org_tubepress_impl_http_HttpClientChain::ARGS_METHOD]) { case 'POST': curl_setopt($handle, CURLOPT_POST, true); curl_setopt($handle, CURLOPT_POSTFIELDS, $r['body']); break; case 'PUT': curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($handle, CURLOPT_POSTFIELDS, $r['body']); break; } curl_setopt($handle, CURLOPT_HEADER, true); // The option doesn't work with safe mode or when open_basedir is set. // Disable HEAD when making HEAD requests. if (!ini_get('safe_mode') && !ini_get('open_basedir')) { curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true); } if (!empty($r[org_tubepress_impl_http_HttpClientChain::ARGS_HEADERS])) { // cURL expects full header strings in each element $headers = array(); foreach ($r[org_tubepress_impl_http_HttpClientChain::ARGS_HEADERS] as $name => $value) { $headers[] = "{$name}: {$value}"; } curl_setopt($handle, CURLOPT_HTTPHEADER, $headers); } if ($r[org_tubepress_impl_http_HttpClientChain::ARGS_HTTP_VERSION] == '1.0') { curl_setopt($handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); } else { curl_setopt($handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); } $theResponse = curl_exec($handle); if (!empty($theResponse)) { $headerLength = curl_getinfo($handle, CURLINFO_HEADER_SIZE); $theHeaders = trim(substr($theResponse, 0, $headerLength)); if (strlen($theResponse) > $headerLength) { $theBody = substr($theResponse, $headerLength); } else { $theBody = ''; } if (false !== strrpos($theHeaders, "\r\n\r\n")) { $headerParts = explode("\r\n\r\n", $theHeaders); $theHeaders = $headerParts[count($headerParts) - 1]; } $theHeaders = self::_getProcessedHeaders($theHeaders); } else { if ($curlError = curl_error($handle)) { throw new Exception($curlError); } if (in_array(curl_getinfo($handle, CURLINFO_HTTP_CODE), array(301, 302))) { throw new Exception('Too many redirects.'); } $theHeaders = array('headers' => array(), 'cookies' => array()); $theBody = ''; } $response = array(); $response['code'] = curl_getinfo($handle, CURLINFO_HTTP_CODE); $response['message'] = $response['code']; curl_close($handle); if (true === $r[org_tubepress_impl_http_HttpClientChain::ARGS_DECOMPRESS] && true === org_tubepress_impl_http_clientimpl_Encoding::shouldDecode($theHeaders['headers'])) { $theBody = org_tubepress_impl_http_clientimpl_Encoding::decompress($theBody); } return array('headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $response, 'cookies' => $theHeaders['cookies']); }
/** * Send a HTTP request to a URI using fsockopen(). * * Does not support non-blocking mode. * * @param string $url URI resource. * @param array $r Optional. Override the defaults. * * @return array 'headers', 'body', 'cookies' and 'response' keys. */ protected function _doExecute($url, $r) { $iError = null; // Store error number $strError = null; // Store error string $arrURL = parse_url($url); $fsockopen_host = $arrURL['host']; $secure_transport = false; if (!isset($arrURL['port'])) { if (($arrURL['scheme'] == 'ssl' || $arrURL['scheme'] == 'https') && extension_loaded('openssl')) { $fsockopen_host = "ssl://{$fsockopen_host}"; $arrURL['port'] = 443; $secure_transport = true; } else { $arrURL['port'] = 80; } } //fsockopen has issues with 'localhost' with IPv6 with certain versions of PHP, It attempts to connect to ::1, // which fails when the server is not set up for it. For compatibility, always connect to the IPv4 address. if ('localhost' == strtolower($fsockopen_host)) { $fsockopen_host = '127.0.0.1'; } // There are issues with the HTTPS and SSL protocols that cause errors that can be safely // ignored and should be ignored. if (true === $secure_transport) { $error_reporting = error_reporting(0); } $handle = @fsockopen($fsockopen_host, $arrURL['port'], $iError, $strError, $r[org_tubepress_impl_http_HttpClientChain::ARGS_TIMEOUT]); if (false === $handle) { throw new Exception($iError . ': ' . $strError); } $timeout = (int) floor($r['timeout']); $utimeout = $timeout == $r['timeout'] ? 0 : 1000000 * $r['timeout'] % 1000000; stream_set_timeout($handle, $timeout, $utimeout); $requestPath = $arrURL['path'] . (isset($arrURL['query']) ? '?' . $arrURL['query'] : ''); if (empty($requestPath)) { $requestPath .= '/'; } $strHeaders = strtoupper($r[org_tubepress_impl_http_HttpClientChain::ARGS_METHOD]) . ' ' . $requestPath . ' HTTP/' . $r[org_tubepress_impl_http_HttpClientChain::ARGS_HTTP_VERSION] . "\r\n"; $strHeaders .= 'Host: ' . $arrURL['host'] . "\r\n"; if (isset($r[org_tubepress_impl_http_HttpClientChain::ARGS_USER_AGENT])) { $strHeaders .= org_tubepress_api_http_HttpClient::HTTP_HEADER_USER_AGENT . ': ' . $r[org_tubepress_impl_http_HttpClientChain::ARGS_USER_AGENT] . "\r\n"; } if (is_array($r[org_tubepress_impl_http_HttpClientChain::ARGS_HEADERS])) { foreach ((array) $r[org_tubepress_impl_http_HttpClientChain::ARGS_HEADERS] as $header => $headerValue) { $strHeaders .= $header . ': ' . $headerValue . "\r\n"; } } else { $strHeaders .= $r[org_tubepress_impl_http_HttpClientChain::ARGS_HEADERS]; } $strHeaders .= "\r\n"; if (!is_null($r[org_tubepress_impl_http_HttpClientChain::ARGS_BODY])) { $strHeaders .= $r[org_tubepress_impl_http_HttpClientChain::ARGS_BODY]; } fwrite($handle, $strHeaders); $strResponse = ''; while (!feof($handle)) { $strResponse .= fread($handle, 4096); } fclose($handle); if (true === $secure_transport) { error_reporting($error_reporting); } $process = self::_breakRawStringResponseIntoHeaderAndBody($strResponse); $arrHeaders = self::_getProcessedHeaders($process['headers']); // If location is found, then assume redirect and redirect to location. if (isset($arrHeaders['headers']['location'])) { if ($this->_canRedirect()) { return $this->_doExecute($arrHeaders['headers']['location'], $r); } else { throw new Exception('Too many redirects.'); } } // If the body was chunk encoded, then decode it. if (!empty($process['body']) && isset($arrHeaders['headers']['transfer-encoding']) && 'chunked' == $arrHeaders['headers']['transfer-encoding']) { $process['body'] = org_tubepress_impl_http_clientimpl_Encoding::chunkTransferDecode($process['body']); } if (true === $r[org_tubepress_impl_http_HttpClientChain::ARGS_DECOMPRESS] && true === org_tubepress_impl_http_clientimpl_Encoding::shouldDecode($arrHeaders['headers'])) { $process['body'] = org_tubepress_impl_http_clientimpl_Encoding::decompress($process['body']); } return array('headers' => $arrHeaders['headers'], 'body' => $process['body'], 'response' => $arrHeaders['response'], 'cookies' => $arrHeaders['cookies']); }
/** * Send a HTTP request to a URI using fopen(). * * This transport does not support sending of headers and body, therefore should not be used in * the instances, where there is a body and headers. * * @param string $url URI resource. * @param array $args Optional. Override the defaults. * * @return array 'headers', 'body', 'cookies' and 'response' keys. */ protected function _doExecute($url, $r) { $arrURL = parse_url($url); if ('http' != $arrURL['scheme'] && 'https' != $arrURL['scheme']) { $url = str_replace($arrURL['scheme'], 'http', $url); } $initialUserAgent = ini_get(self::INI_USER_AGENT); if (!empty($r[org_tubepress_impl_http_HttpClientChain::ARGS_HEADERS]) && is_array($r[org_tubepress_impl_http_HttpClientChain::ARGS_HEADERS])) { $user_agent_extra_headers = ''; foreach ($r[org_tubepress_impl_http_HttpClientChain::ARGS_HEADERS] as $header => $value) { $user_agent_extra_headers .= "\r\n{$header}: {$value}"; } @ini_set(self::INI_USER_AGENT, $r[org_tubepress_impl_http_HttpClientChain::ARGS_HEADERS] . $user_agent_extra_headers); } else { @ini_set(self::INI_USER_AGENT, $r[org_tubepress_impl_http_HttpClientChain::ARGS_HEADERS]); } $handle = @fopen($url, 'r'); if (!$handle) { throw new Exception(sprintf('Could not open handle for fopen() to %s'), $url); } $timeout = (int) floor($r[org_tubepress_impl_http_HttpClientChain::ARGS_TIMEOUT]); $utimeout = $timeout == $r[org_tubepress_impl_http_HttpClientChain::ARGS_TIMEOUT] ? 0 : 1000000 * $r['timeout'] % 1000000; stream_set_timeout($handle, $timeout, $utimeout); $strResponse = ''; while (!feof($handle)) { $strResponse .= fread($handle, 4096); } if (function_exists('stream_get_meta_data')) { $meta = stream_get_meta_data($handle); $theHeaders = $meta['wrapper_data']; if (isset($meta['wrapper_data']['headers'])) { $theHeaders = $meta['wrapper_data']['headers']; } } else { //$http_response_header is a PHP reserved variable which is set in the current-scope when using the HTTP Wrapper //see http://php.oregonstate.edu/manual/en/reserved.variables.httpresponseheader.php $theHeaders = $http_response_header; } fclose($handle); @ini_set(self::INI_USER_AGENT, $initialUserAgent); //Clean up any extra headers added $processedHeaders = self::_getProcessedHeaders($theHeaders); if (!empty($strResponse) && isset($processedHeaders['headers']['transfer-encoding']) && 'chunked' == $processedHeaders['headers']['transfer-encoding']) { $strResponse = org_tubepress_impl_http_clientimpl_Encoding::chunkTransferDecode($strResponse); } if (true === $r['decompress'] && true === org_tubepress_impl_http_clientimpl_Encoding::shouldDecode($processedHeaders['headers'])) { $strResponse = org_tubepress_impl_http_clientimpl_Encoding::decompress($strResponse); } return array('headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response'], 'cookies' => $processedHeaders['cookies']); }
/** * Send a HTTP request to a URI. * * The body and headers are part of the arguments. The 'body' argument is for the body and will * accept either a string or an array. The 'headers' argument should be an array, but a string * is acceptable. * * The only URI that are supported in the HTTP Transport implementation are the HTTP and HTTPS * protocols. HTTP and HTTPS are assumed so the server might not know how to handle the send * headers. Other protocols are unsupported and most likely will fail. * * @param string $url URI resource. * @param array $args Generic HTTP options. * * @return array containing 'headers', 'body', 'response', 'cookies' */ private function _request($url, $args) { $defaults = array(self::ARGS_METHOD => org_tubepress_api_http_HttpClient::HTTP_METHOD_GET, self::ARGS_TIMEOUT => 5, self::ARGS_HTTP_VERSION => '1.0', self::ARGS_USER_AGENT => 'TubePress; http://tubepress.org', self::ARGS_HEADERS => array(), self::ARGS_COOKIES => array(), self::ARGS_BODY => null, self::ARGS_COMPRESS => false, self::ARGS_DECOMPRESS => true, self::ARGS_SSL_VERIFY => true); $r = array_merge($defaults, $args); $arrURL = parse_url($url); if (empty($url) || empty($arrURL['scheme'])) { throw new Exception('A valid URL was not provided.'); } org_tubepress_impl_log_Log::log(self::LOG_PREFIX, 'Will perform %s to <a href="%s">%s</a>', $r[self::ARGS_METHOD], $url, $url); // Determine if this is a https call and pass that on to the transport functions // so that we can blacklist the transports that do not support ssl verification $r[self::ARGS_IS_SSL] = $arrURL['scheme'] == 'https' || $arrURL['scheme'] == 'ssl'; // Determine if this request is local $r['local'] = 'localhost' == $arrURL['host']; if (org_tubepress_impl_http_clientimpl_Encoding::isCompressionAvailable()) { org_tubepress_impl_log_Log::log(self::LOG_PREFIX, 'HTTP compression is available. Yay!'); $r[self::ARGS_HEADERS][org_tubepress_api_http_HttpClient::HTTP_HEADER_ACCEPT_ENCODING] = org_tubepress_impl_http_clientimpl_Encoding::getAcceptEncodingString(); } else { org_tubepress_impl_log_Log::log(self::LOG_PREFIX, 'HTTP compression is NOT available. Boo.'); } if (empty($r[self::ARGS_BODY])) { $r[self::ARGS_BODY] = null; // Some servers fail when sending content without the content-length header being set. // Also, to fix another bug, we only send when doing POST and PUT and the content-length // header isn't already set. if ($r[self::ARGS_METHOD] == org_tubepress_api_http_HttpClient::HTTP_METHOD_POST || $r[self::ARGS_METHOD] == org_tubepress_api_http_HttpClient::HTTP_METHOD_PUT) { $r[self::ARGS_HEADERS][org_tubepress_api_http_HttpClient::HTTP_HEADER_CONTENT_LENGTH] = 0; } } else { $r[self::ARGS_HEADERS][org_tubepress_api_http_HttpClient::HTTP_HEADER_CONTENT_LENGTH] = strlen($r[self::ARGS_BODY]); } $ioc = org_tubepress_impl_ioc_IocContainer::getInstance(); $commands = self::_getTransportCommands($ioc); $sm = $ioc->get('org_tubepress_api_patterns_cor_Chain'); $context = $sm->createContextInstance(); $context->url = $url; $context->args = $r; $status = $sm->execute($context, $commands); if ($status === false) { throw new Exception("Could not retrieve {$url}"); } return $context->returnValue; }