* @license http://www.apache.org/licenses/LICENSE-2.0 * @link http://phpsx.org */ abstract class ParserAbstract { const MODE_STRICT = 0x1; const MODE_LOOSE = 0x2; protected $mode; /** * The mode indicates how the header is detected in strict mode we search * exactly for CRLF CRLF in loose mode we look for the first empty line. In * loose mode we can parse an header wich was defined in the code means is * not strictly seperated by CRLF * * @param integer $mode */ public function __construct($mode = self::MODE_STRICT) { if ($mode == self::MODE_STRICT || $mode == self::MODE_LOOSE) { $this->mode = $mode; } else { throw new InvalidArgumentException('Invalid parse mode'); } } /** * Converts an raw http message into an PSX\Http\Message object * * @param string $content * @return \PSX\Http\MessageInterface */ abstract public function parse($content); /** * Splits an given http message into the header and body part * * @param string $message * @return array */ protected function splitMessage($message) { if ($this->mode == self::MODE_STRICT) { $pos = strpos($message, Http::NEW_LINE . Http::NEW_LINE); $header = substr($message, 0, $pos); $body = trim(substr($message, $pos + 1)); } elseif ($this->mode == self::MODE_LOOSE) { $lines = explode("\n", $message); $header = ''; $body = ''; $found = false; $count = count($lines); foreach ($lines as $i => $line) { $line = trim($line); if (!$found && empty($line)) { $found = true; continue; } if (!$found) { $header.= $line . Http::NEW_LINE; } else { $body.= $line . ($i < $count - 1 ? "\n" : ''); } } } return array($header, $body); } /** * @param string $content * @return string */ protected function normalize($content) { if (empty($content)) { throw new InvalidArgumentException('Empty message'); } if ($this->mode == self::MODE_LOOSE) { $content = str_replace(array("\r\n", "\n", "\r"), "\n", $content); } return $content; } /** * Parses an raw http header string into an Message object * * @param \PSX\Http\MessageInterface $message * @param string $header */ protected function headerToArray(MessageInterface $message, $header) { $lines = explode(Http::NEW_LINE, $header); foreach ($lines as $line) { $parts = explode(':', $line, 2); if (isset($parts[0]) && isset($parts[1])) { $key = $parts[0]; $value = substr($parts[1], 1); $message->addHeader($key, $value); } } } /** * @param string $message * @return boolean|string */ protected function getStatusLine($message) { if ($this->mode == self::MODE_STRICT) { $pos = strpos($message, Http::NEW_LINE); } elseif ($this->mode == self::MODE_LOOSE) { $pos = strpos($message, "\n"); } return $pos !== false ? substr($message, 0, $pos) : false; } /** * @param \PSX\Http\MessageInterface $message * @return array */ public static function buildHeaderFromMessage(MessageInterface $message) { $headers = $message->getHeaders(); $result = array(); foreach ($headers as $key => $value) { if ($key == 'set-cookie') { foreach ($value as $cookie) { $result[] = $key . ': ' . $cookie; } } else { $result[] = $key . ': ' . implode(', ', $value); } } return $result; } } __halt_compiler();----SIGNATURE:----Amv4QpMb2N5uHNiIzj5PJiNoXv+AT3FLffxS/7xEjFdiQUbPnznGEbokrf3FvkTOCA2iQBPPgKtGphToaF8x2ULMvVZ03ObjKv1Y6gtbWsDqDVxEVQClcW7Y1EpSSb1tyl4r4nyEjmBVVo6gLzUDG2dVcnO4NAGBt5E78VR8B1grYhT8XqbGpRjE602hbDTwK9YG3Bg9qZOuo5RXzcYwd6KyJwPL71PsKRoVwdyKzxEU0PyoFsulqY9AUB0T6DXumwivnFHfTV74Q2NrZouDFje88XrZP0oiKhpxbu4L6pfgEcn6q7g0QuXHrL2v55wU/hbF1eJx6zUoeb8v7hR1gCEloPwFQ/L9xmS/wEmB3+djvZ9G03x/kiD4nM1VcLAb3BzC105VM15g8R2CL6cttWZsuQhGXIE2Y2CI035wNXMLMGD4ZFofC6YopzPZP3IlLOivbmRMqu9Exa42i5+VkfGAjynqISABvcv/IN2hPIBFM1vPde2772mWGFsXZrhBmqoUmTo3S3np637xSRdo7xmkRiFVWHw1tsioJQv1V7MttR3POcyk6TLAUUYA6P9oWq8LNdGi/gbiOOcrTNXhDO7QRgcuAH+Yo7Jo/CbJkwAwmgyf1ATuLSwj36mYuzx9xhsZ3k5Gnw4+EMHgu0uVR1TgZk/25NrF7tHUof0f87o=----ATTACHMENT:----MjAzOTI0MDI1OTg3MjczMiAyMDcwOTQ4MjQ4MDY4NzE1IDE5NzY1MTkwNDU0MDgzNDA=