/** * Send request to the proxy server with streaming support * * @param string $method * @param \Zend\Uri\Uri $uri * @param string $http_ver * @param array $headers * @param string $body * @return string Request as string */ public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '') { // If no proxy is set, throw an error if (!$this->config['proxy_host']) { throw new Adapter\Exception('No proxy host set!'); } // Make sure we're properly connected if (!$this->socket) { throw new Adapter\Exception('Trying to write but we are not connected'); } $host = $this->config['proxy_host']; $port = $this->config['proxy_port']; if ($this->connected_to[0] != $host || $this->connected_to[1] != $port) { throw new Adapter\Exception('Trying to write but we are connected to the wrong proxy ' . 'server'); } // Add Proxy-Authorization header if ($this->config['proxy_user'] && !isset($headers['proxy-authorization'])) { $headers['proxy-authorization'] = \Zend\Http\Client::encodeAuthHeader($this->config['proxy_user'], $this->config['proxy_pass'], $this->config['proxy_auth']); } // if we are proxying HTTPS, preform CONNECT handshake with the proxy if ($uri->getScheme() == 'https' && !$this->negotiated) { $this->connectHandshake($uri->getHost(), $uri->getPort(), $http_ver, $headers); $this->negotiated = true; } // Save request method for later $this->method = $method; // Build request headers $request = "{$method} {$uri->toString()} HTTP/{$http_ver}\r\n"; // Add all headers to the request string foreach ($headers as $k => $v) { if (is_string($k)) { $v = "{$k}: {$v}"; } $request .= "{$v}\r\n"; } $request .= "\r\n"; // Send the request headers if (!@fwrite($this->socket, $request)) { throw new Adapter\Exception('Error writing request to proxy server'); } //read from $body, write to socket while ($body->hasData()) { if (!@fwrite($this->socket, $body->read(self::CHUNK_SIZE))) { throw new Adapter\Exception('Error writing request to server'); } } return 'Large upload, request is not cached.'; }
/** * @expectedException Zend\Http\Client\Exception\InvalidArgumentException */ public function testEncodeAuthHeaderThrowsExceptionWhenInvalidAuthTypeIsUsed() { $encoded = Client::encodeAuthHeader('test', 'test', 'test'); }
/** * Send request to the proxy server * * @param string $method * @param \Zend\Uri\Url $uri * @param string $http_ver * @param array $headers * @param string $body * @return string Request as string */ public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '') { // If no proxy is set, fall back to default Socket adapter if (! $this->config['proxy_host']) return parent::write($method, $uri, $http_ver, $headers, $body); // Make sure we're properly connected if (! $this->socket) { throw new AdapterException\RuntimeException("Trying to write but we are not connected"); } $host = $this->config['proxy_host']; $port = $this->config['proxy_port']; if ($this->connected_to[0] != "tcp://$host" || $this->connected_to[1] != $port) { throw new AdapterException\RuntimeException("Trying to write but we are connected to the wrong proxy server"); } // Add Proxy-Authorization header if ($this->config['proxy_user'] && ! isset($headers['proxy-authorization'])) { $headers['proxy-authorization'] = Client::encodeAuthHeader( $this->config['proxy_user'], $this->config['proxy_pass'], $this->config['proxy_auth'] ); } // if we are proxying HTTPS, preform CONNECT handshake with the proxy if ($uri->getScheme() == 'https' && (! $this->negotiated)) { $this->connectHandshake($uri->getHost(), $uri->getPort(), $http_ver, $headers); $this->negotiated = true; } // Save request method for later $this->method = $method; // Build request headers if ($this->negotiated) { $path = $uri->getPath(); if ($uri->getQuery()) { $path .= '?' . $uri->getQuery(); } $request = "$method $path HTTP/$http_ver\r\n"; } else { $request = "$method $uri HTTP/$http_ver\r\n"; } // Add all headers to the request string foreach ($headers as $k => $v) { if (is_string($k)) $v = "$k: $v"; $request .= "$v\r\n"; } if(is_resource($body)) { $request .= "\r\n"; } else { // Add the request body $request .= "\r\n" . $body; } // Send the request if (! @fwrite($this->socket, $request)) { throw new AdapterException\RuntimeException("Error writing request to proxy server"); } if (is_resource($body)) { if(stream_copy_to_stream($body, $this->socket) == 0) { throw new AdapterException\RuntimeException('Error writing request to server'); } } return $request; }
public function testIfClientDoesNotForwardAuthenticationToForeignHost() { // set up user credentials $user = '******'; $password = '******'; $encoded = Client::encodeAuthHeader($user, $password, Client::AUTH_BASIC); $testAdapter = new Test(); $client = new Client(null, array('adapter' => $testAdapter)); // set up two responses that simulate a redirection from example.org to example.com $testAdapter->setResponse("HTTP/1.1 303 See Other\r\n" . "Location: http://example.com/part2\r\n\r\n" . "The URL of this page has changed."); $testAdapter->addResponse("HTTP/1.1 200 OK\r\n\r\n" . "Welcome to this Website."); // set auth and do request $client->setUri('http://example.org/part1')->setAuth($user, $password, Client::AUTH_BASIC); $response = $client->setMethod('GET')->send(); // the last request should NOT contain the Authorization header, // because example.com is different from example.org $this->assertTrue(strpos($client->getLastRawRequest(), $encoded) === false); // set up two responses that simulate a rediration from example.org to sub.example.org $testAdapter->setResponse("HTTP/1.1 303 See Other\r\n" . "Location: http://sub.example.org/part2\r\n\r\n" . "The URL of this page has changed."); $testAdapter->addResponse("HTTP/1.1 200 OK\r\n\r\n" . "Welcome to this Website."); // set auth and do request $client->setUri('http://example.org/part1')->setAuth($user, $password, Client::AUTH_BASIC); $response = $client->setMethod('GET')->send(); // the last request should contain the Authorization header, // because sub.example.org is a subdomain unter example.org $this->assertFalse(strpos($client->getLastRawRequest(), $encoded) === false); // set up two responses that simulate a rediration from sub.example.org to example.org $testAdapter->setResponse("HTTP/1.1 303 See Other\r\n" . "Location: http://example.org/part2\r\n\r\n" . "The URL of this page has changed."); $testAdapter->addResponse("HTTP/1.1 200 OK\r\n\r\n" . "Welcome to this Website."); // set auth and do request $client->setUri('http://sub.example.org/part1')->setAuth($user, $password, Client::AUTH_BASIC); $response = $client->setMethod('GET')->send(); // the last request should NOT contain the Authorization header, // because example.org is not a subdomain unter sub.example.org $this->assertTrue(strpos($client->getLastRawRequest(), $encoded) === false); }
/** * Test encodeAuthHeader (static method) fails when trying to use an * unsupported authentication scheme * */ public function testExceptUnsupportedAuthStatic() { $this->setExpectedException( 'Zend\Http\Client\Exception\InvalidArgumentException', 'Not a supported HTTP authentication type: \'SuperStrongAlgo\''); HTTPClient::encodeAuthHeader('shahar', '1234', 'SuperStrongAlgo'); }
/** * Test encodeAuthHeader (static method) fails when trying to use an * unsupported authentication scheme * * @expectedException Zend\Http\Client\Exception */ public function testExceptUnsupportedAuthStatic() { HTTPClient::encodeAuthHeader('shahar', '1234', 'SuperStrongAlgo'); }