public function testParseHttpResponseBody() { $io = new apiCurlIO(); $rawHeaders = "HTTP/1.1 200 OK\r\n" . "Expires: Sun, 22 Jan 2012 09:00:56 GMT\r\n" . "Date: Sun, 22 Jan 2012 09:00:56 GMT\r\n" . "Content-Type: application/json; charset=UTF-8\r\n"; $size = strlen($rawHeaders); $rawBody = "{}"; $rawResponse = "{$rawHeaders}\r\n{$rawBody}"; list($headers, $body) = $io->parseHttpResponse($rawResponse, $size); $this->assertEquals(3, sizeof($headers)); $this->assertEquals(array(), json_decode($body, true)); // Test empty bodies. $rawResponse = $rawHeaders . "\r\n"; list($headers, $body) = $io->parseHttpResponse($rawResponse, $size); $this->assertEquals(3, sizeof($headers)); $this->assertEquals(null, json_decode($body, true)); // Test transforms from proxies. $rawHeaders = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n"; $size = strlen($rawHeaders); $rawBody = "{}"; $rawResponse = apiCurlIO::CONNECTION_ESTABLISHED . "{$rawHeaders}\r\n{$rawBody}"; list($headers, $body) = $io->parseHttpResponse($rawResponse, $size); $this->assertEquals(1, sizeof($headers)); $this->assertEquals(array(), json_decode($body, true)); }
public function parseResponse(apiHttpRequest $response) { $contentType = $response->getResponseHeader('content-type'); $contentType = explode(';', $contentType); $boundary = false; foreach ($contentType as $part) { $part = explode('=', $part, 2); if (isset($part[0]) && 'boundary' == trim($part[0])) { $boundary = $part[1]; } } $body = $response->getResponseBody(); if ($body) { $body = str_replace("--{$boundary}--", "--{$boundary}", $body); $parts = explode("--{$boundary}", $body); $responses = array(); foreach ($parts as $part) { $part = trim($part); if (!empty($part)) { list($metaHeaders, $part) = explode("\r\n\r\n", $part, 2); $metaHeaders = apiCurlIO::parseResponseHeaders($metaHeaders); $status = substr($part, 0, strpos($part, "\n")); $status = explode(" ", $status); $status = $status[1]; list($partHeaders, $partBody) = apiCurlIO::parseHttpResponse($part, false); $response = new apiHttpRequest(""); $response->setResponseHttpCode($status); $response->setResponseHeaders($partHeaders); $response->setResponseBody($partBody); $response = apiREST::decodeHttpResponse($response); // Need content id. $responses[$metaHeaders['content-id']] = $response; } } return $responses; } return null; }