/** * Processes the reuqest through HTTP pipeline with passed $filters, * sends HTTP request to the wire and process the response in the HTTP pipeline. * * @param array $filters HTTP filters which will be applied to the request before * send and then applied to the response. * @param IUrl $url Request url. * * @throws WindowsAzure\Common\ServiceException * * @return string The response body */ public function send($filters, $url = null) { if (isset($url)) { $this->setUrl($url); $this->_request->setUrl($this->_requestUrl->getUrl()); } $contentLength = Resources::EMPTY_STRING; if (strtoupper($this->getMethod()) != Resources::HTTP_GET && strtoupper($this->getMethod()) != Resources::HTTP_DELETE && strtoupper($this->getMethod()) != Resources::HTTP_HEAD) { $contentLength = 0; if (!is_null($this->getBody())) { $contentLength = strlen($this->getBody()); } $this->_request->setHeader(Resources::CONTENT_LENGTH, $contentLength); } foreach ($filters as $filter) { $this->_request = $filter->handleRequest($this)->_request; } $this->_response = $this->_request->send(); $start = count($filters) - 1; for ($index = $start; $index >= 0; --$index) { $this->_response = $filters[$index]->handleResponse($this, $this->_response); } self::throwIfError($this->_response->getStatus(), $this->_response->getReasonPhrase(), $this->_response->getBody(), $this->_expectedStatusCodes); return $this->_response->getBody(); }
/** * * @expectedException HTTP_Request2_MessageException */ public function testParseStatusLine() { $response = new HTTP_Request2_Response('HTTP/1.1 200 OK'); $this->assertEquals('1.1', $response->getVersion()); $this->assertEquals(200, $response->getStatus()); $this->assertEquals('OK', $response->getReasonPhrase()); $response2 = new HTTP_Request2_Response('HTTP/1.2 222 Nishtyak!'); $this->assertEquals('1.2', $response2->getVersion()); $this->assertEquals(222, $response2->getStatus()); $this->assertEquals('Nishtyak!', $response2->getReasonPhrase()); $response3 = new HTTP_Request2_Response('Invalid status line'); }
public function testParseStatusLine() { $response = new HTTP_Request2_Response('HTTP/1.1 200 OK'); $this->assertEquals('1.1', $response->getVersion()); $this->assertEquals(200, $response->getStatus()); $this->assertEquals('OK', $response->getReasonPhrase()); $response2 = new HTTP_Request2_Response('HTTP/1.2 222 Nishtyak!'); $this->assertEquals('1.2', $response2->getVersion()); $this->assertEquals(222, $response2->getStatus()); $this->assertEquals('Nishtyak!', $response2->getReasonPhrase()); try { $response3 = new HTTP_Request2_Response('Invalid status line'); } catch (HTTP_Request2_Exception $e) { return; } $this->fail('Expected HTTP_Request2_Exception was not thrown'); }
/** * Saves the response body to a specified directory * * @param HTTP_Request2_Response $response * @return void * @throws BuildException */ protected function processResponse(HTTP_Request2_Response $response) { if ($response->getStatus() != 200) { throw new BuildException("Request unsuccessful. Response from server: " . $response->getStatus() . " " . $response->getReasonPhrase()); } $content = $response->getBody(); $disposition = $response->getHeader('content-disposition'); if ($this->filename) { $filename = $this->filename; } elseif ($disposition && 0 == strpos($disposition, 'attachment') && preg_match('/filename="([^"]+)"/', $disposition, $m)) { $filename = basename($m[1]); } else { $filename = basename(parse_url($this->url, PHP_URL_PATH)); } if (!is_writable($this->dir)) { throw new BuildException("Cannot write to directory: " . $this->dir); } $filename = $this->dir . "/" . $filename; file_put_contents($filename, $content); $this->log("Contents from " . $this->url . " saved to {$filename}"); }
/** * Constructor * * @param string $content Http response * as string * * @param WindowsAzure\Common\Internal\Http\BatchRequest $request Source batch * request object */ public function __construct($content, $request = null) { $params['include_bodies'] = true; $params['input'] = $content; $mimeDecoder = new \Mail_mimeDecode($content); $structure = $mimeDecoder->decode($params); $parts = $structure->parts; $this->_contexts = array(); $requestContexts = null; if ($request != null) { Validate::isA($request, 'WindowsAzure\\Common\\Internal\\Http\\BatchRequest', 'request'); $requestContexts = $request->getContexts(); } $i = 0; foreach ($parts as $part) { if (!empty($part->body)) { $headerEndPos = strpos($part->body, "\r\n\r\n"); $header = substr($part->body, 0, $headerEndPos); $body = substr($part->body, $headerEndPos + 4); $headerStrings = explode("\r\n", $header); $response = new \HTTP_Request2_Response(array_shift($headerStrings)); foreach ($headerStrings as $headerString) { $response->parseHeaderLine($headerString); } $response->appendBody($body); $this->_contexts[] = $response; if (is_array($requestContexts)) { $expectedCodes = $requestContexts[$i]->getStatusCodes(); $statusCode = $response->getStatus(); if (!in_array($statusCode, $expectedCodes)) { $reason = $response->getReasonPhrase(); throw new ServiceException($statusCode, $reason, $body); } } $i++; } } }
/** * Parses the response, returning stdClass of the reponse body * * @param HTTP_Request2_Response $response The HTTP_Request2 response * * @ignore * @throws Services_Digg2_Exception on a non-2XX response * @return stdClass (decoded json) */ protected function parseResponse(HTTP_Request2_Response $response) { $this->lastResponse = $response; $body = json_decode($response->getBody()); if (!is_object($body)) { throw new Services_Digg2_Exception('Unabled to decode result: ' . $response->getBody()); } $status = $response->getStatus(); $message = $status . ' - ' . $response->getReasonPhrase(); if (strncmp($status, '2', 1) !== 0) { throw new Services_Digg2_Exception($message, $body->code, $status); } return $body; }