/** * Send request to the remote 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 = '') { // Make sure we're properly connected if (! $this->socket) { throw new Adapter\Exception( 'Trying to write but we are not connected'); } $host = $uri->getHost(); $host = (strtolower($uri->getScheme()) == 'https' ? $this->config['ssltransport'] : 'tcp') . '://' . $host; if ($this->connected_to[0] != $host || $this->connected_to[1] != $uri->getPort()) { throw new Adapter\Exception( 'Trying to write but we are connected to the wrong host'); } // Save request method for later $this->method = $method; // Build request headers $path = $uri->getPath(); if ($uri->getQuery()) $path .= '?' . $uri->getQuery(); $request = "{$method} {$path} HTTP/{$http_ver}\r\n"; foreach ($headers as $k => $v) { if (is_string($k)) $v = ucfirst($k) . ": $v"; $request .= "$v\r\n"; } // Send the headers over $request .= "\r\n"; if (! @fwrite($this->socket, $request)) { throw new Adapter\Exception( 'Error writing request to server'); } //read from $body, write to socket $chunk = $body->read(self::CHUNK_SIZE); while ($chunk !== FALSE) { if (! @fwrite($this->socket, $chunk)) { throw new Adapter\Exception( 'Error writing request to server'); } $chunk = $body->read(self::CHUNK_SIZE); } $body->closeFileHandle(); return 'Large upload, request is not cached.'; }
/** * Send request to the remote server * * @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 = '') { $host = $uri->getHost(); $host = strtolower($uri->getScheme()) == 'https' ? 'sslv2://' . $host : $host; // Build request headers $path = $uri->getPath(); if (empty($path)) { $path = '/'; } if ($uri->getQuery()) { $path .= '?' . $uri->getQuery(); } $request = "{$method} {$path} HTTP/{$http_ver}\r\n"; foreach ($headers as $k => $v) { if (is_string($k)) { $v = ucfirst($k) . ": {$v}"; } $request .= "{$v}\r\n"; } // Add the request body $request .= "\r\n" . $body; // Do nothing - just return the request as string return $request; }
/** * Send request to the remote server * * @param string $method * @param \Zend\Uri\Uri $uri * @param string $httpVer * @param array $headers * @param string $body * @throws AdapterException\RuntimeException * @return string Request as string */ public function write($method, $uri, $httpVer = '1.1', $headers = array(), $body = '') { // Make sure we're properly connected if (!$this->socket) { throw new AdapterException\RuntimeException('Trying to write but we are not connected'); } $host = $uri->getHost(); $host = (strtolower($uri->getScheme()) == 'https' ? $this->config['ssltransport'] : 'tcp') . '://' . $host; if ($this->connectedTo[0] != $host || $this->connectedTo[1] != $uri->getPort()) { throw new AdapterException\RuntimeException('Trying to write but we are connected to the wrong host'); } // Save request method for later $this->method = $method; // Build request headers $path = $uri->getPath(); if ($uri->getQuery()) { $path .= '?' . $uri->getQuery(); } $request = "{$method} {$path} HTTP/{$httpVer}\r\n"; foreach ($headers as $k => $v) { if (is_string($k)) { $v = ucfirst($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 ErrorHandler::start(); $test = fwrite($this->socket, $request); $error = ErrorHandler::stop(); if (false === $test) { throw new AdapterException\RuntimeException('Error writing request to server', 0, $error); } 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 initiateHandshake(Uri $uri) { $challenge = self::randHybiKey(); $request = new Request(); $requestUri = $uri->getPath(); if ($uri->getQuery()) { $requestUri .= "?" . $uri->getQuery(); } $request->setUri($requestUri); $request->getHeaders()->addHeaderLine("Connection", "Upgrade"); $request->getHeaders()->addHeaderLine("Host", $uri->getHost()); $request->getHeaders()->addHeaderLine("Sec-WebSocket-Key", $challenge); $request->getHeaders()->addHeaderLine("Sec-WebSocket-Version", 13); $request->getHeaders()->addHeaderLine("Upgrade", "websocket"); $this->setRequest($request); $this->emit("request", array($request)); $this->_socket->write($request->toString()); return $request; }
/** * @group ZF-1480 */ public function testGetQueryAsArrayReturnsCorrectArray() { $url = new Uri('http://example.com/foo/?test=a&var[]=1&var[]=2&some[thing]=3'); $this->assertEquals('test=a&var[]=1&var[]=2&some[thing]=3', $url->getQuery()); $exp = array('test' => 'a', 'var' => array(1, 2), 'some' => array('thing' => 3)); $this->assertEquals($exp, $url->getQueryAsArray()); }
/** * @inheritdoc */ public function parse($uri) { $uri = new Uri($uri); return $this->formatResults(['scheme' => $uri->getScheme(), 'userinfo' => $uri->getUserInfo(), 'host' => $uri->getHost(), 'port' => $uri->getPort(), 'path' => $uri->getPath(), 'query' => $uri->getQuery(), 'fragment' => $uri->getFragment()]); }
public function testParseTwice() { $uri = new Uri(); $uri->parse('http://user@example.com:1/absolute/url?query#fragment'); $uri->parse('/relative/url'); $this->assertNull($uri->getScheme()); $this->assertNull($uri->getHost()); $this->assertNull($uri->getUserInfo()); $this->assertNull($uri->getPort()); $this->assertNull($uri->getQuery()); $this->assertNull($uri->getFragment()); }
/** * Send request to the remote server * * @param string $method * @param \Zend\Uri\Uri $uri * @param string $httpVer * @param array $headers * @param string $body * @return string Request as string */ public function write($method, $uri, $httpVer = '1.1', $headers = [], $body = '') { // Build request headers $path = $uri->getPath(); if (empty($path)) { $path = '/'; } if ($uri->getQuery()) { $path .= '?' . $uri->getQuery(); } $request = "{$method} {$path} HTTP/{$httpVer}\r\n"; foreach ($headers as $k => $v) { if (is_string($k)) { $v = ucfirst($k) . ": {$v}"; } $request .= "{$v}\r\n"; } // Add the request body $request .= "\r\n" . $body; // Do nothing - just return the request as string return $request; }
/** * Get the URI query * * @return string|null */ public function getQuery() { return $this->uri->getQuery(); }