/** * Send request to the remote server * * @param string $method * @param Microsoft_Uri_Http $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) { require_once 'Microsoft/Http/Client/Adapter/Exception.php'; throw new Microsoft_Http_Client_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()) { require_once 'Microsoft/Http/Client/Adapter/Exception.php'; throw new Microsoft_Http_Client_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"; } if (is_resource($body)) { $request .= "\r\n"; } else { // Add the request body $request .= "\r\n" . $body; } // Send the request if (!@fwrite($this->socket, $request)) { require_once 'Microsoft/Http/Client/Adapter/Exception.php'; throw new Microsoft_Http_Client_Adapter_Exception('Error writing request to server'); } if (is_resource($body)) { if (stream_copy_to_stream($body, $this->socket) == 0) { require_once 'Microsoft/Http/Client/Adapter/Exception.php'; throw new Microsoft_Http_Client_Adapter_Exception('Error writing request to server'); } } return $request; }
/** * Send the HTTP request and return an HTTP response object * * @param string $method * @return Microsoft_Http_Response * @throws Microsoft_Http_Client_Exception */ public function request($method = null) { if (!$this->uri instanceof Microsoft_Uri_Http) { /** @see Microsoft_Http_Client_Exception */ require_once 'Microsoft/Http/Client/Exception.php'; throw new Microsoft_Http_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 Microsoft_Http_Client_Adapter_Stream) { /** @see Microsoft_Http_Client_Exception */ require_once 'Microsoft/Http/Client/Exception.php'; throw new Microsoft_Http_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 Microsoft_Http_Client_Adapter_Stream) { $stream = $this->_openTempStream(); $this->adapter->setOutputStream($stream); } else { /** @see Microsoft_Http_Client_Exception */ require_once 'Microsoft/Http/Client/Exception.php'; throw new Microsoft_Http_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) { /** @see Microsoft_Http_Client_Exception */ require_once 'Microsoft/Http/Client/Exception.php'; throw new Microsoft_Http_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 = Microsoft_Http_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 = Microsoft_Http_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 if (Microsoft_Uri_Http::check($location)) { $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; }