* @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:----KGB8CawPE9sf2gylCEgtRRj0IWxJwYvY9OuDZOv6dCMA1vIHevPkyjvwiPmijG8ByvoXZAQNKMqN+2BeEoGSCkFeSbO3lBw52nvyek6GzM4aMtQflntSNqN06gMX9A7stKD3fNpveCL2cOGyeyQ1hAjY6prtTPZAyVGb8zUr0OLMoc9S30Vog0liDqqJxYmuBKol0SSaBI7B0pEhl344qLDs5HGZrAbcp+cAMiUbQTos5NEwHOc9lz91JPjb0gpXL6/Qt9OKWqKF7JZnHacCXh2kBNQgZ+G8i6Hag9BZTlF8qYzVZLQ2Mmog19k5BqmTp3SN2N6sqqR/sfAjFP+ldzf3XhhiFp1WzZaknLMhTx8TJBPq4C4paSHNWBi9pYD5Ioa2+P6Qo3DF5DbZUD/5BfGDheXPtmiQKtbk/CWHej+v5ng4jdDiMn+EzWyPt1FnmhaImEZEPepqjt4D+aq+mA5oRsKRC6E3f/SZ9whgNHBj2InruX8nzpYfpQyymxPns6o6NtgfqmKPrtwssw+vlkMhgyxo9Pgxiisfi0sPws2x03kF3u6mrcilY8xKYzRlOAV2fpGTP1B19sKQGG4UqfFlx9iva+O5PnPvO+NM1sxOwPbwCnYq8Y5nVOWA0lTQ7XJAg1RKrqIynrojhF2vxVhqtI53nD7+mgCs2vHf8MQ=----ATTACHMENT:----ODUyNjM3OTMwNTAwMzI4OCAyODczMzY0MTE5MjE3NDM2IDgwOTEwMjAyNDI2OTQ5NjI=