public function testClearHeadersCharset() { $header = "HTTP/1.1 200 OK" . "\r\n" . "Content-Type: text/html; charset=Shift-JIS"; $response = Response::fromString("{$header}\r\n\r\nABC"); $c = Filter::clearHeadersCharset($response->getHeaders()->toArray()); $this->assertEquals('text/html', $c['Content-Type']); }
public function testResponseFactoryFromStringCreatesValidResponse() { $string = 'HTTP/1.0 200 OK' . "\r\n\r\n" . 'Foo Bar'; $response = Response::fromString($string); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals('Foo Bar', $response->getContent()); }
public function testGetAdapterWithConfig() { $httptest = new HttpClientTest(); // Nirvanix adapter $nirvanixConfig = ConfigFactory::fromFile(realpath(dirname(__FILE__) . '/_files/config/nirvanix.ini'), true); $nirvanixConfig = $nirvanixConfig->toArray(); $nirvanixConfig[Nirvanix::HTTP_ADAPTER] = $httptest; $doc = new \DOMDocument('1.0', 'utf-8'); $root = $doc->createElement('Response'); $responseCode = $doc->createElement('ResponseCode', 0); $sessionTok = $doc->createElement('SessionToken', '54592180-7060-4D4B-BC74-2566F4B2F943'); $root->appendChild($responseCode); $root->appendChild($sessionTok); $doc->appendChild($root); $body = $doc->saveXML(); $resp = HttpResponse::fromString("HTTP/1.1 200 OK\nContent-type: text/xml;charset=UTF-8\nDate: 0\n\n" . $body); $httptest->setResponse($resp); $nirvanixAdapter = Factory::getAdapter($nirvanixConfig); $this->assertEquals('Zend\\Cloud\\StorageService\\Adapter\\Nirvanix', get_class($nirvanixAdapter)); // S3 adapter $s3Config = ConfigFactory::fromFile(realpath(dirname(__FILE__) . '/_files/config/s3.ini'), true); $s3Adapter = Factory::getAdapter($s3Config); $this->assertEquals('Zend\\Cloud\\StorageService\\Adapter\\S3', get_class($s3Adapter)); // file system adapter $fileSystemConfig = ConfigFactory::fromFile(realpath(dirname(__FILE__) . '/_files/config/filesystem.ini'), true); $fileSystemAdapter = Factory::getAdapter($fileSystemConfig); $this->assertEquals('Zend\\Cloud\\StorageService\\Adapter\\FileSystem', get_class($fileSystemAdapter)); // Azure adapter /* $azureConfig = ConfigFactory::fromFile(realpath(dirname(__FILE__) . '/_files/config/windowsazure.ini'), true); $azureConfig = $azureConfig->toArray(); $azureContainer = $azureConfig[WindowsAzure::CONTAINER]; $azureConfig[WindowsAzure::HTTP_ADAPTER] = $httptest; $q = "?"; $doc = new \DOMDocument('1.0', 'utf-8'); $root = $doc->createElement('EnumerationResults'); $acctName = $doc->createAttribute('AccountName'); $acctName->value = 'http://myaccount.blob.core.windows.net'; $root->appendChild($acctName); $maxResults = $doc->createElement('MaxResults', 1); $containers = $doc->createElement('Containers'); $container = $doc->createElement('Container'); $containerName = $doc->createElement('Name', $azureContainer); $container->appendChild($containerName); $containers->appendChild($container); $root->appendChild($maxResults); $root->appendChild($containers); $doc->appendChild($root); $body = $doc->saveXML(); $resp = HttpResponse::fromString("HTTP/1.1 200 OK\nContent-type: text/xml;charset=UTF-8\nx-ms-request-id: 0\n\n".$body); $httptest->setResponse($resp); $azureAdapter = Factory::getAdapter($azureConfig); $this->assertEquals('Zend\Cloud\StorageService\Adapter\WindowsAzure', get_class($azureAdapter)); * */ }
/** * Mock the actual rest call */ public function restGet($path, array $query = null) { /** * We could simulate a connection failure by supplyng * $responseText = file_get_contents(ROOT_PATH . "/test/HotelTest/connectionError.txt"); * or simulate invalid json coming from API ... etc * But its getting late .. :) */ $responseText = file_get_contents(ROOT_PATH . "/test/HotelTest/hotels.txt"); return Response::fromString($responseText); }
public function testZF2() { $header = "HTTP/1.1 200 OK" . "\r\n" . "Content-Type: text/html; charset=Shift-JIS"; $html = <<<EOF <html lang="ja" xml:lang="ja" xmlns="http://www.w3.org/1999/xhtml"> <head><meta content="text/html; charset=Shift-JIS" http-equiv="Content-Type" /></head> <body><!--① ㈱① ㈱-->ああ</body> EOF; $sjis = mb_convert_encoding($html, 'SJIS-win', 'UTF-8'); $response = \Zend\Http\Response::fromString("{$header}\r\n\r\n{$sjis}"); $response = WrapperFactory::factory($response); $this->assertInstanceOf('Zend\\Http\\Response', $response); $this->assertInstanceOf('Diggin\\Http\\Charset\\Wrapper\\ZF2Wrapper', $response); $this->assertEquals($html, $response->getBody()); }
public function testAcceptEncodingHeaderWorksProperly() { $method = new \ReflectionMethod('\\Zend\\Http\\Client', 'prepareHeaders'); $method->setAccessible(true); $requestString = "GET http://www.domain.com/index.php HTTP/1.1\r\nHost: domain.com\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:16.0) Gecko/20100101 Firefox/16.0\r\nAccept: */*\r\nAccept-Language: en-US,en;q=0.5\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive\r\n"; $request = Request::fromString($requestString); $adapter = new \Zend\Http\Client\Adapter\Test(); $client = new \Zend\Http\Client('http://www.domain.com/'); $client->setAdapter($adapter); $client->setRequest($request); $rawHeaders = "HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\nContent-Encoding: gzip, deflate\r\nContent-Type: application/javascript\r\nDate: Sun, 18 Nov 2012 16:16:08 GMT\r\nServer: nginx/1.1.19\r\nVary: Accept-Encoding\r\nX-Powered-By: PHP/5.3.10-1ubuntu3.4\r\nConnection: keep-alive\r\n"; $response = Response::fromString($rawHeaders); $client->getAdapter()->setResponse($response); $headers = $method->invoke($client, $requestString, $client->getUri()); $this->assertEquals('gzip, deflate', $headers['Accept-Encoding']); }
/** * Create connector with fixture file. * * @param string $fixture Fixture file * * @return Connector * * @throws InvalidArgumentException Fixture file does not exist */ protected function createConnector($fixture = null) { $adapter = new TestAdapter(); if ($fixture) { $file = realpath(__DIR__ . '/../../../../../../tests/fixtures/resolver/response/' . $fixture); if (!is_string($file) || !file_exists($file) || !is_readable($file)) { throw new InvalidArgumentException(sprintf('Unable to load fixture file: %s ', $file)); } $response = file_get_contents($file); $responseObj = HttpResponse::fromString($response); $adapter->setResponse($responseObj); } $client = new \Zend\Http\Client(); $client->setAdapter($adapter); $conn = new Redi($this->openUrlConfig['OpenURL']['url'], $client); return $conn; }
/** * Do request proxy method. * * @param CommonClient $client Actual SOAP client. * @param string $request The request body. * @param string $location The SOAP URI. * @param string $action The SOAP action to call. * @param integer $version The SOAP version to use. * @param integer $one_way (Optional) The number 1 if a response is not expected. * @return string The XML SOAP response. */ public function _doRequest(CommonClient $client, $request, $location, $action, $version, $one_way = null) { if (!$this->useNtlm) { return parent::_doRequest($client, $request, $location, $action, $version, $one_way); } $curlClient = $this->getCurlClient(); $headers = array('Content-Type' => 'text/xml; charset=utf-8', 'Method' => 'POST', 'SOAPAction' => '"' . $action . '"', 'User-Agent' => 'PHP-SOAP-CURL'); $uri = new HttpUri($location); $curlClient->setCurlOption(CURLOPT_HTTPAUTH, CURLAUTH_NTLM)->setCurlOption(CURLOPT_SSL_VERIFYHOST, false)->setCurlOption(CURLOPT_SSL_VERIFYPEER, false)->setCurlOption(CURLOPT_USERPWD, $this->options['login'] . ':' . $this->options['password']); // Perform the cURL request and get the response $curlClient->connect($uri->getHost(), $uri->getPort()); $curlClient->write('POST', $uri, 1.1, $headers, $request); $response = HttpResponse::fromString($curlClient->read()); $curlClient->close(); // Save headers $this->lastRequestHeaders = $this->flattenHeaders($headers); $this->lastResponseHeaders = $response->getHeaders()->toString(); // Return only the XML body return $response->getBody(); }
public function init(ModuleManager $moduleManager) { $config = $this->getConfig(); $cache = StorageFactory::factory($config['page-cache']['cache']); if (filter_input(INPUT_GET, 'clear-cache')) { if ($cache instanceof ClearByNamespaceInterface) { $cache->clearByNameSpace($config['page-cache']['cache']['adapter']['options']['namespace']); } } if (filter_input(INPUT_GET, 'clear-expired')) { if ($cache instanceof ClearExpiredInterface) { $cache->clearExpired(); } } $responseStr = $cache->getItem($this->getKey(), $success); if ($success) { $response = Response::fromString($responseStr); foreach ($response->getHeaders() as $header) { header($header->toString()); } echo $response->getBody(); exit; } }
/** * Create connector with fixture file. * * @param string $fixture Fixture file * * @return Connector * * @throws InvalidArgumentException Fixture file does not exist */ protected function createConnector($fixture = null) { $adapter = new TestAdapter(); if ($fixture) { $file = realpath(__DIR__ . '/../../../../../../tests/fixtures/daia/response/' . $fixture); if (!is_string($file) || !file_exists($file) || !is_readable($file)) { throw new InvalidArgumentException(sprintf('Unable to load fixture file: %s ', $file)); } $response = file_get_contents($file); $responseObj = HttpResponse::fromString($response); $adapter->setResponse($responseObj); } $service = new \VuFindHttp\HttpService(); $service->setDefaultAdapter($adapter); $conn = new DAIA(new \VuFind\Date\Converter()); $conn->setHttpService($service); return $conn; }
/** * Test that parsing a multibyte-encoded chunked response works. * * This can potentially fail on different PHP environments - for example * when mbstring.func_overload is set to overload strlen(). * */ public function testMultibyteChunkedResponse() { $this->markTestSkipped('Looks like the headers are split with \\n and the body with \\r\\n'); $md5 = 'ab952f1617d0e28724932401f2d3c6ae'; $response = Response::fromString($this->readResponse('response_multibyte_body')); $this->assertEquals($md5, md5($response->getBody())); }
/** * Create connector with fixture file. * * @param string $fixture Fixture file * * @return Connector * * @throws InvalidArgumentException Fixture file does not exist */ protected function createMockConnector($fixture = null) { $adapter = new TestAdapter(); if ($fixture) { $file = realpath(__DIR__ . '/../../../../../../tests/fixtures/paia/response/' . $fixture); if (!is_string($file) || !file_exists($file) || !is_readable($file)) { throw new InvalidArgumentException(sprintf('Unable to load fixture file: %s ', $file)); } $response = file_get_contents($file); $responseObj = HttpResponse::fromString($response); $adapter->setResponse($responseObj); } $service = new \VuFindHttp\HttpService(); $service->setDefaultAdapter($adapter); $conn = $this->getMockBuilder('VuFind\\ILS\\Driver\\PAIA')->setConstructorArgs([new \VuFind\Date\Converter(), new \Zend\Session\SessionManager()])->setMethods(['getScope'])->getMock(); $conn->expects($this->any())->method('getScope')->will($this->returnValue(['write_items'])); $conn->setHttpService($service); $conn->setConfig($this->validConfig); $conn->init(); return $conn; }
/** * Load a SOLR response as fixture. * * @param string $fixture Fixture file * * @return Zend\Http\Response * * @throws InvalidArgumentException Fixture files does not exist */ protected function loadResponse($fixture) { $file = realpath(sprintf('%s/solr/response/%s', PHPUNIT_SEARCH_FIXTURES, $fixture)); if (!is_string($file) || !file_exists($file) || !is_readable($file)) { throw new InvalidArgumentException(sprintf('Unable to load fixture file: %s', $file)); } return Response::fromString(file_get_contents($file)); }
/** * Convert a PSR-7 response in a Zend\Http\Response * * @param ResponseInterface $psr7Response * @return ZendResponse */ public static function toZend(ResponseInterface $psr7Response) { $response = sprintf("HTTP/%s %d %s\r\n%s\r\n%s", $psr7Response->getProtocolVersion(), $psr7Response->getStatusCode(), $psr7Response->getReasonPhrase(), self::psr7HeadersToString($psr7Response), (string) $psr7Response->getBody()); return ZendResponse::fromString($response); }
private function readHandshakeResponse($data) { $response = Response::fromString($data); $this->setResponse($response); $handshake = new Handshake($this->request, $response); $this->emit("handshake", array($handshake)); if ($handshake->isAborted()) { $this->close(); return ''; } $this->connected = true; $this->emit("connect"); return $response->getContent(); }
public function makeHttpResponseFor($nativeVars) { $response = $this->getServerResponseFor($nativeVars); return \Zend\Http\Response::fromString($response); }
/** * Return response fixture * * @param string $name Name of fixture * * @return Response Response */ protected function createResponse($name) { $file = realpath(\VUFIND_PHPUNIT_MODULE_PATH . '/fixtures/response/solr/' . $name); if (!$file) { throw new RuntimeException(sprintf('Unable to resolve fixture to fixture file: %s', $name)); } $response = Response::fromString(file_get_contents($file)); return $response; }
use React\SocketClient\Connector; use Zend\Http\Response; require "vendor/autoload.php"; $loop = React\EventLoop\Factory::create(); $dnsResolverFactory = new React\Dns\Resolver\Factory(); $dnsResolver = $dnsResolverFactory->createCached('127.0.0.1', $loop); $connector = new Connector($loop, $dnsResolver); $connectorRepeater = new \ConnectionManager\Extra\ConnectionManagerTimeout($connector, $loop, 1); for ($i = 0; $i < 3; $i++) { $connectorRepeater->create('dev.akond.net', 80)->then(function ($stream) { $stream->on('data', function ($data, $stream) { if (empty($data)) { return; } $r = Response::fromString($data); echo $r->getContent(); }); if (0) { $stream->on('close', function ($stream) { echo 'closed'; }); } $stream->write(<<<'txt' GET /react-php/micro-service.php HTTP/1.0 Host: dev.akond.net txt ); // $stream->end();
/** * Preform handshaking with HTTPS proxy using CONNECT method * * @param string $host * @param int $port * @param string $httpVer * @param array $headers * @throws AdapterException\RuntimeException */ protected function connectHandshake($host, $port = 443, $httpVer = '1.1', array &$headers = []) { $request = "CONNECT {$host}:{$port} HTTP/{$httpVer}\r\n" . "Host: " . $host . "\r\n"; // Add the user-agent header if (isset($this->config['useragent'])) { $request .= "User-agent: " . $this->config['useragent'] . "\r\n"; } // If the proxy-authorization header is set, send it to proxy but remove // it from headers sent to target host if (isset($headers['proxy-authorization'])) { $request .= "Proxy-authorization: " . $headers['proxy-authorization'] . "\r\n"; unset($headers['proxy-authorization']); } $request .= "\r\n"; // Send the request ErrorHandler::start(); $test = fwrite($this->socket, $request); $error = ErrorHandler::stop(); if (!$test) { throw new AdapterException\RuntimeException("Error writing request to proxy server", 0, $error); } // Read response headers only $response = ''; $gotStatus = false; ErrorHandler::start(); while ($line = fgets($this->socket)) { $gotStatus = $gotStatus || strpos($line, 'HTTP') !== false; if ($gotStatus) { $response .= $line; if (!rtrim($line)) { break; } } } ErrorHandler::stop(); // Check that the response from the proxy is 200 if (Response::fromString($response)->getStatusCode() != 200) { throw new AdapterException\RuntimeException("Unable to connect to HTTPS proxy. Server response: " . $response); } // If all is good, switch socket to secure mode. We have to fall back // through the different modes $modes = [STREAM_CRYPTO_METHOD_TLS_CLIENT, STREAM_CRYPTO_METHOD_SSLv3_CLIENT, STREAM_CRYPTO_METHOD_SSLv23_CLIENT, STREAM_CRYPTO_METHOD_SSLv2_CLIENT]; $success = false; foreach ($modes as $mode) { $success = stream_socket_enable_crypto($this->socket, true, $mode); if ($success) { break; } } if (!$success) { throw new AdapterException\RuntimeException("Unable to connect to" . " HTTPS server through proxy: could not negotiate secure connection."); } }
/** * Test that parsing a multibyte-encoded chunked response works. * * This can potentially fail on different PHP environments - for example * when mbstring.func_overload is set to overload strlen(). * */ public function testMultibyteChunkedResponse() { $md5 = 'ab952f1617d0e28724932401f2d3c6ae'; $response = Response::fromString($this->readResponse('response_multibyte_body')); $this->assertEquals($md5, md5($response->getBody())); }
/** * Get response via mixed pattern * * @param mixed */ protected function getResponse($resource) { //psuedo reponse if (is_array($resource) and !isset($resource['body'])) { $resource['body'] = $resource['0']; if (!array_key_exists('header', $resource)) { $resource['header'] = "HTTP/1.1 200 OK\r\nContent-type: text/html"; } $responseStr = $resource['header'] . "\r\n\r\n" . $resource['body']; $resource = HttpResponse::fromString($responseStr); } // if set uri if (!$resource instanceof HttpResponse) { $resource = $this->_makeRequest($resource); } return $resource; }
/** * Retrieve entities from table * * @param string|TableEntityQuery $tableName Table name -or- TableEntityQuery instance * @param string $filter Filter condition (not applied when $tableName is a TableEntityQuery instance) * @param string $entityClass Entity class name * @param string $nextPartitionKey Next partition key, used for listing entities when total amount of entities is > 1000. * @param string $nextRowKey Next row key, used for listing entities when total amount of entities is > 1000. * @throws RuntimeException * @throws InvalidArgumentException * @return array Array of TableEntity */ public function retrieveEntities($tableName, $filter = '', $entityClass = 'DynamicTableEntity', $nextPartitionKey = null, $nextRowKey = null) { if ($tableName === '') { throw new InvalidArgumentException('Table name is not specified.'); } if ($entityClass === '') { throw new InvalidArgumentException('Entity class is not specified.'); } // Convenience... if (class_exists($filter)) { $entityClass = $filter; $filter = ''; } // Query string $queryString = ''; // Determine query if (is_string($tableName)) { // Option 1: $tableName is a string // Append parentheses $tableName .= '()'; // Build query $query = array(); // Filter? if ($filter !== '') { $query[] = '$filter=' . rawurlencode($filter); } // Build queryString if (count($query) > 0) { $queryString = '?' . implode('&', $query); } } elseif (get_class($tableName) == 'TableEntityQuery') { // Option 2: $tableName is a TableEntityQuery instance // Build queryString $queryString = $tableName->assembleQueryString(true); // Change $tableName $tableName = $tableName->assembleFrom(true); } else { throw new InvalidArgumentException('Invalid argument: $tableName'); } // Add continuation querystring parameters? if ($nextPartitionKey !== null && $nextRowKey !== null) { if ($queryString !== '') { $queryString .= '&'; } $queryString .= '&NextPartitionKey=' . rawurlencode($nextPartitionKey) . '&NextRowKey=' . rawurlencode($nextRowKey); } // Perform request $response = null; if ($this->isInBatch() && $this->getCurrentBatch()->getOperationCount() == 0) { $this->getCurrentBatch()->enlistOperation($tableName, $queryString, Request::METHOD_GET, array(), true, null); $response = $this->getCurrentBatch()->commit(); // Get inner response (multipart) $innerResponse = $response->getBody(); $innerResponse = substr($innerResponse, strpos($innerResponse, 'HTTP/1.1 200 OK')); $innerResponse = substr($innerResponse, 0, strpos($innerResponse, '--batchresponse')); $response = Response::fromString($innerResponse); } else { $response = $this->_performRequest($tableName, $queryString, Request::METHOD_GET, array(), true, null); } if ($response->isSuccess()) { // Parse result $result = $this->_parseResponse($response); if (!$result) { return array(); } $entries = null; if ($result->entry) { if (count($result->entry) > 1) { $entries = $result->entry; } else { $entries = array($result->entry); } } else { // This one is tricky... If we have properties defined, we have an entity. $properties = $result->xpath('//m:properties'); if ($properties) { $entries = array($result); } else { return array(); } } // Create return value $returnValue = array(); foreach ($entries as $entry) { // Parse properties $properties = $entry->xpath('.//m:properties'); $properties = $properties[0]->children('http://schemas.microsoft.com/ado/2007/08/dataservices'); // Create entity $entity = new $entityClass('', ''); $entity->setAzureValues((array) $properties, true); // If we have a DynamicTableEntity, make sure all property types are OK if ($entity instanceof DynamicTableEntity) { foreach ($properties as $key => $value) { $attributes = $value->attributes('http://schemas.microsoft.com/ado/2007/08/dataservices/metadata'); $type = (string) $attributes['type']; if ($type !== '') { $entity->setAzurePropertyType($key, $type); } } } // Update etag $etag = $entry->attributes('http://schemas.microsoft.com/ado/2007/08/dataservices/metadata'); $etag = (string) $etag['etag']; $entity->setEtag($etag); // Add to result $returnValue[] = $entity; } // More entities? if ($response->getHeaders()->get('x-ms-continuation-NextPartitionKey') !== null && $response->getHeaders()->get('x-ms-continuation-NextRowKey') !== null) { if (strpos($queryString, '$top') === false) { $returnValue = array_merge($returnValue, $this->retrieveEntities($tableName, $filter, $entityClass, $response->getHeaders()->get('x-ms-continuation-NextPartitionKey'), $response->getHeaders()->get('x-ms-continuation-NextRowKey'))); } } // Return return $returnValue; } else { throw new RuntimeException($this->_getErrorMessage($response, 'Resource could not be accessed.')); } }
/** * Test we can build a new object from a response object (multiple cookie headers) */ public function testFromResponseMultiHeader() { $res_str = file_get_contents(dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'response_with_cookies'); $response = Response::fromString($res_str); $jar = Http\CookieJar::fromResponse($response, 'http://www.example.com'); $this->assertTrue($jar instanceof Http\CookieJar, '$jar is not an instance of CookieJar as expected'); $this->assertEquals(3, count($jar->getAllCookies()), 'CookieJar expected to contain 3 cookies'); }
/** * Send HTTP request * * @param Request $request * @return Response * @throws Exception\RuntimeException * @throws Client\Exception\RuntimeException */ public function send(Request $request = null) { if ($request !== null) { $this->setRequest($request); } $this->redirectCounter = 0; $response = null; // Make sure the adapter is loaded if ($this->adapter == null) { $this->setAdapter($this->config['adapter']); } // Send the first request. If redirected, continue. do { // uri $uri = $this->getUri(); // query $query = $this->getRequest()->getQuery(); if (!empty($query)) { $queryArray = $query->toArray(); if (!empty($queryArray)) { $newUri = $uri->toString(); $queryString = http_build_query($query, null, $this->getArgSeparator()); if ($this->config['rfc3986strict']) { $queryString = str_replace('+', '%20', $queryString); } if (strpos($newUri, '?') !== false) { $newUri .= $this->getArgSeparator() . $queryString; } else { $newUri .= '?' . $queryString; } $uri = new Http($newUri); } } // If we have no ports, set the defaults if (!$uri->getPort()) { $uri->setPort($uri->getScheme() == 'https' ? 443 : 80); } // method $method = $this->getRequest()->getMethod(); // body $body = $this->prepareBody(); // headers $headers = $this->prepareHeaders($body, $uri); $secure = $uri->getScheme() == 'https'; // cookies $cookie = $this->prepareCookies($uri->getHost(), $uri->getPath(), $secure); if ($cookie->getFieldValue()) { $headers['Cookie'] = $cookie->getFieldValue(); } // check that adapter supports streaming before using it if (is_resource($body) && !$this->adapter instanceof Client\Adapter\StreamInterface) { throw new Client\Exception\RuntimeException('Adapter does not support streaming'); } // calling protected method to allow extending classes // to wrap the interaction with the adapter $response = $this->doRequest($uri, $method, $secure, $headers, $body); if (!$response) { throw new Exception\RuntimeException('Unable to read response, or response is empty'); } if ($this->config['storeresponse']) { $this->lastRawResponse = $response; } else { $this->lastRawResponse = null; } if ($this->config['outputstream']) { $stream = $this->getStream(); if (!is_resource($stream) && is_string($stream)) { $stream = fopen($stream, 'r'); } $streamMetaData = stream_get_meta_data($stream); if ($streamMetaData['seekable']) { rewind($stream); } // cleanup the adapter $this->adapter->setOutputStream(null); $response = Response\Stream::fromStream($response, $stream); $response->setStreamName($this->streamName); if (!is_string($this->config['outputstream'])) { // we used temp name, will need to clean up $response->setCleanup(true); } } else { $response = Response::fromString($response); } // Get the cookies from response (if any) $setCookies = $response->getCookie(); if (!empty($setCookies)) { $this->addCookie($setCookies); } // If we got redirected, look for the Location header if ($response->isRedirect() && $response->getHeaders()->has('Location')) { // Avoid problems with buggy servers that add whitespace at the // end of some headers $location = trim($response->getHeaders()->get('Location')->getFieldValue()); // Check whether we send the exact same request again, or drop the parameters // and send a GET request if ($response->getStatusCode() == 303 || !$this->config['strictredirects'] && ($response->getStatusCode() == 302 || $response->getStatusCode() == 301)) { $this->resetParameters(); $this->setMethod(Request::METHOD_GET); } // If we got a well formed absolute URI if (($scheme = substr($location, 0, 6)) && ($scheme == 'http:/' || $scheme == 'https:')) { $this->setUri($location); } else { // Split into path and query and set the query if (strpos($location, '?') !== false) { list($location, $query) = explode('?', $location, 2); } else { $query = ''; } $this->getUri()->setQuery($query); // Else, if we got just an absolute path, set it if (strpos($location, '/') === 0) { $this->getUri()->setPath($location); // Else, assume we have a relative path } else { // Get the current path directory, removing any trailing slashes $path = $this->getUri()->getPath(); $path = rtrim(substr($path, 0, strrpos($path, '/')), "/"); $this->getUri()->setPath($path . '/' . $location); } } ++$this->redirectCounter; } else { // If we didn't get any location, stop redirecting break; } } while ($this->redirectCounter < $this->config['maxredirects']); $this->response = $response; return $response; }
/** * Send the HTTP request and return an HTTP response object * * @param string $method * @return \Zend\Http\Response * @throws \Zend\Http\Client\Exception */ public function request($method = null) { if (!$this->uri instanceof Uri\Url) { throw new Client\Exception('No valid URI has been passed to the client'); } if ($method) { $this->setMethod($method); } $this->redirectCounter = 0; $response = null; // Make sure the adapter is loaded if ($this->adapter == null) { $this->setAdapter($this->config['adapter']); } // Send the first request. If redirected, continue. do { // Clone the URI and add the additional GET parameters to it $uri = clone $this->uri; if (!empty($this->paramsGet)) { $query = $uri->getQuery(); if (!empty($query)) { $query .= '&'; } $query .= http_build_query($this->paramsGet, null, '&'); $uri->setQuery($query); } $body = $this->_prepareBody(); $headers = $this->_prepareHeaders(); // check that adapter supports streaming before using it if (is_resource($body) && !$this->adapter instanceof Client\Adapter\Stream) { throw new Client\Exception('Adapter does not support streaming'); } // Open the connection, send the request and read the response $this->adapter->connect($uri->getHost(), $uri->getPort(), $uri->getScheme() == 'https' ? true : false); if ($this->config['output_stream']) { if ($this->adapter instanceof Client\Adapter\Stream) { $stream = $this->_openTempStream(); $this->adapter->setOutputStream($stream); } else { throw new Client\Exception('Adapter does not support streaming'); } } $this->last_request = $this->adapter->write($this->method, $uri, $this->config['httpversion'], $headers, $body); $response = $this->adapter->read(); if (!$response) { throw new Client\Exception('Unable to read response, or response is empty'); } if ($this->config['output_stream']) { rewind($stream); // cleanup the adapter $this->adapter->setOutputStream(null); $response = Response\Stream::fromStream($response, $stream); $response->setStreamName($this->_stream_name); if (!is_string($this->config['output_stream'])) { // we used temp name, will need to clean up $response->setCleanup(true); } } else { $response = Response::fromString($response); } if ($this->config['storeresponse']) { $this->last_response = $response; } // Load cookies into cookie jar if (isset($this->cookiejar)) { $this->cookiejar->addCookiesFromResponse($response, $uri); } // If we got redirected, look for the Location header if ($response->isRedirect() && ($location = $response->getHeader('location'))) { // Check whether we send the exact same request again, or drop the parameters // and send a GET request if ($response->getStatus() == 303 || !$this->config['strictredirects'] && ($response->getStatus() == 302 || $response->getStatus() == 301)) { $this->resetParameters(); $this->setMethod(self::GET); } // If we got a well formed absolute URI $url = new Uri\Url($location); if ($url->isValid()) { $this->setHeaders('host', null); $this->setUri($location); } else { // Split into path and query and set the query if (strpos($location, '?') !== false) { list($location, $query) = explode('?', $location, 2); } else { $query = ''; } $this->uri->setQuery($query); // Else, if we got just an absolute path, set it if (strpos($location, '/') === 0) { $this->uri->setPath($location); // Else, assume we have a relative path } else { // Get the current path directory, removing any trailing slashes $path = $this->uri->getPath(); $path = rtrim(substr($path, 0, strrpos($path, '/')), "/"); $this->uri->setPath($path . '/' . $location); } } ++$this->redirectCounter; } else { // If we didn't get any location, stop redirecting break; } } while ($this->redirectCounter < $this->config['maxredirects']); return $response; }
/** * Test that responses could be added as objects (ZF-7009) * * @link http://framework.zend.com/issues/browse/ZF-7009 * @dataProvider validHttpResponseProvider */ public function testAddResponseAsObject($testResponse) { $this->adapter->read(); // pop out first response $respObj = \Zend\Http\Response::fromString($testResponse); $this->adapter->addResponse($respObj); $this->assertEquals($testResponse, $this->adapter->read()); }
/** * Parse fetched remote content to response * * @param string $content * * @return bool|array|string */ protected function parseResponse($content = '') { try { $response = Response::fromString($content); } catch (\Exception $e) { $response = false; trigger_error('Response error: ' . $e->getMessage(), E_USER_WARNING); } if ($response && $response->isOk()) { $result = $response->getBody(); $contentType = $response->getHeaders()->get('Content-Type'); $isJson = false; if ($contentType) { $value = $contentType->getFieldValue(); $isJson = false !== stripos($value, 'application/json'); } if ($isJson) { $result = json_decode($result, true); } } else { $result = false; } return $result; }
/** * Read response from server * * @throws AdapterException\RuntimeException * @return string */ public function read() { // First, read headers only $response = ''; $gotStatus = false; while (($line = fgets($this->socket)) !== false) { $gotStatus = $gotStatus || strpos($line, 'HTTP') !== false; if ($gotStatus) { $response .= $line; if (rtrim($line) === '') { break; } } } $this->_checkSocketReadTimeout(); $responseObj = Response::fromString($response); $statusCode = $responseObj->getStatusCode(); // Handle 100 and 101 responses internally by restarting the read again if ($statusCode == 100 || $statusCode == 101) { return $this->read(); } // Check headers to see what kind of connection / transfer encoding we have $headers = $responseObj->getHeaders(); /** * Responses to HEAD requests and 204 or 304 responses are not expected * to have a body - stop reading here */ if ($statusCode == 304 || $statusCode == 204 || $this->method == \Zend\Http\Request::METHOD_HEAD) { // Close the connection if requested to do so by the server $connection = $headers->get('connection'); if ($connection && $connection->getFieldValue() == 'close') { $this->close(); } return $response; } // If we got a 'transfer-encoding: chunked' header $transferEncoding = $headers->get('transfer-encoding'); $contentLength = $headers->get('content-length'); if ($transferEncoding !== false) { if (strtolower($transferEncoding->getFieldValue()) == 'chunked') { do { $line = fgets($this->socket); $this->_checkSocketReadTimeout(); $chunk = $line; // Figure out the next chunk size $chunksize = trim($line); if (!ctype_xdigit($chunksize)) { $this->close(); throw new AdapterException\RuntimeException('Invalid chunk size "' . $chunksize . '" unable to read chunked body'); } // Convert the hexadecimal value to plain integer $chunksize = hexdec($chunksize); // Read next chunk $readTo = ftell($this->socket) + $chunksize; do { $currentPos = ftell($this->socket); if ($currentPos >= $readTo) { break; } if ($this->outStream) { if (stream_copy_to_stream($this->socket, $this->outStream, $readTo - $currentPos) == 0) { $this->_checkSocketReadTimeout(); break; } } else { $line = fread($this->socket, $readTo - $currentPos); if ($line === false || strlen($line) === 0) { $this->_checkSocketReadTimeout(); break; } $chunk .= $line; } } while (!feof($this->socket)); ErrorHandler::start(); $chunk .= fgets($this->socket); ErrorHandler::stop(); $this->_checkSocketReadTimeout(); if (!$this->outStream) { $response .= $chunk; } } while ($chunksize > 0); } else { $this->close(); throw new AdapterException\RuntimeException('Cannot handle "' . $transferEncoding->getFieldValue() . '" transfer encoding'); } // We automatically decode chunked-messages when writing to a stream // this means we have to disallow the Zend\Http\Response to do it again if ($this->outStream) { $response = str_ireplace("Transfer-Encoding: chunked\r\n", '', $response); } // Else, if we got the content-length header, read this number of bytes } elseif ($contentLength !== false) { // If we got more than one Content-Length header (see ZF-9404) use // the last value sent if (is_array($contentLength)) { $contentLength = $contentLength[count($contentLength) - 1]; } $contentLength = $contentLength->getFieldValue(); $currentPos = ftell($this->socket); $chunk = ''; for ($readTo = $currentPos + $contentLength; $readTo > $currentPos; $currentPos = ftell($this->socket)) { if ($this->outStream) { if (stream_copy_to_stream($this->socket, $this->outStream, $readTo - $currentPos) == 0) { $this->_checkSocketReadTimeout(); break; } } else { $chunk = fread($this->socket, $readTo - $currentPos); if ($chunk === false || strlen($chunk) === 0) { $this->_checkSocketReadTimeout(); break; } $response .= $chunk; } // Break if the connection ended prematurely if (feof($this->socket)) { break; } } // Fallback: just read the response until EOF } else { do { if ($this->outStream) { if (stream_copy_to_stream($this->socket, $this->outStream) == 0) { $this->_checkSocketReadTimeout(); break; } } else { $buff = fread($this->socket, 8192); if ($buff === false || strlen($buff) === 0) { $this->_checkSocketReadTimeout(); break; } else { $response .= $buff; } } } while (feof($this->socket) === false); $this->close(); } // Close the connection if requested to do so by the server $connection = $headers->get('connection'); if ($connection && $connection->getFieldValue() == 'close') { $this->close(); } return $response; }
public function setUp() { $httpResponse = \Zend\Http\Response::fromString(file_get_contents(TESTS_FILES_DIR . 'http/idp_authn_response.txt')); $this->_response = new IdpAuthnResponse($httpResponse); }