* @license http://www.apache.org/licenses/LICENSE-2.0 * @link http://phpsx.org */ class ResponseParser extends ParserAbstract { /** * Converts an raw http response into an PSX\Http\Response object * * @param string $content * @return \PSX\Http\ResponseInterface * @throws \PSX\Http\Parser\ParseException */ public function parse($content) { $content = $this->normalize($content); list($scheme, $code, $message) = $this->getStatus($content); $response = new Response(); $response->setProtocolVersion($scheme); $response->setStatus($code, $message); list($header, $body) = $this->splitMessage($content); $this->headerToArray($response, $header); $response->setBody(new StringStream($body)); return $response; } /** * @param string $response * @return array * @throws \PSX\Http\Parser\ParseException */ protected function getStatus($response) { $line = $this->getStatusLine($response); if ($line !== false) { $parts = explode(' ', $line, 3); if (isset($parts[0]) && isset($parts[1]) && isset($parts[2])) { $scheme = $parts[0]; $code = intval($parts[1]); $message = $parts[2]; return array($scheme, $code, $message); } else { throw new ParseException('Invalid status line format'); } } else { throw new ParseException('Couldnt find status line'); } } /** * @param array $headers * @return \PSX\Http\ResponseInterface * @throws \PSX\Http\Parser\ParseException */ public static function buildResponseFromHeader(array $headers) { $line = array_shift($headers); if (!empty($line)) { $parts = explode(' ', trim($line), 3); if (isset($parts[0]) && isset($parts[1]) && isset($parts[2])) { $scheme = strval($parts[0]); $code = intval($parts[1]); $message = strval($parts[2]); $response = new Response(); $response->setProtocolVersion($scheme); $response->setStatus($code, $message); // append header foreach ($headers as $line) { $parts = explode(':', $line, 2); if (isset($parts[0]) && isset($parts[1])) { $key = $parts[0]; $value = trim($parts[1]); $response->addHeader($key, $value); } } return $response; } else { throw new ParseException('Invalid status line format'); } } else { throw new ParseException('Couldnt find status line'); } } /** * @param \PSX\Http\ResponseInterface $response * @return string */ public static function buildStatusLine(ResponseInterface $response) { $protocol = $response->getProtocolVersion(); $code = $response->getStatusCode(); $phrase = $response->getReasonPhrase(); if (empty($code)) { throw new \RuntimeException('Status code not set'); } $protocol = !empty($protocol) ? $protocol : 'HTTP/1.1'; if (empty($phrase) && isset(Http::$codes[$code])) { $phrase = Http::$codes[$code]; } if (empty($phrase)) { throw new \RuntimeException('No reason phrase provided'); } return $protocol . ' ' . $code . ' ' . $phrase; } /** * Parses an raw http response into an PSX\Http\Response object. Throws an * exception if the response has not an valid format * * @param string $content * @param integer $mode * @return \PSX\Http\ResponseInterface * @throws \PSX\Http\Parser\ParseException */ public static function convert($content, $mode = ParserAbstract::MODE_STRICT) { $parser = new self($mode); return $parser->parse($content); } } __halt_compiler();----SIGNATURE:----SamjYfR8mFSNUWJ4iVtitQZbATnM0cgz78kfHKxalXgTK+YrCBGKqQmvw/nVFjI44JaUrpoazg4/bZWLVGtppEHeM4kE2rVwqkRuxZfzs0nv5b0oPm4LgpyV4C+EiFSQZBZObHjQHelU+63xnJ7L02L9r6sYH8NWrm3Yc+rk79Ahrg9sk7nTA/38VgDWH8UXyODuoqlL9qQ6qxYuUx2aVNWMRBQcqvKS+C605MHa/llpWdQuU/dQ/4SHsdUf/KUM1MR80enrSP07Lzcz/r+z6bwiRlTAo15qv8h17M79hSSy+ltnp1p1W+G8gaeKVa9cG90LmMtvc6nUS6FcfpTy2CccHreq8TuxYCWXIsRlnUpD1GUQDeiYrnpmSYyH3QDMufC40805xZaOjtuOcUn7L7xBHwO3jRAaoiefs0PyEyuQ6X9+bFgEyCXYOccZhd8txIV2CBJto21Y63Kkg/QWh8U+VGFytGY+ApHijmcLVGPE7MNKgaoD/LF54iUBMqIQAV71qURL1OUvgsPqiPS/eqXX5vxPxtOd1RtMa/980ETRGxK0HZaVRhfiitIvZpghdLsIjgV5bqLVxvz3EG1QozDny5JVjgeSUR1eTCDs3WjX0R0qbEV8zduMNYkLYTQORP9Mj49eLJaXpi1gojddPuPXjcwq/SNO9YDgoH6WHSc=----ATTACHMENT:----NzA5MDM0OTUxMzExMjI3MiA0MTE5NjU4MjcwMjEzMzcxIDc5MDUwOTA0NzA5NjUwODI=