* @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:----d8SqeN58FhDdUCBJPGvJ93R7PFlCJAEVjU9pJx8aYR+N1UY6M1UvcO2Z8a4McRLB5c1AT47ORB60+NwnAkjDmjnkJZD08XkKqS/3XQN+J/TvaRY1oYGeeKDLwDn3dRyaKYKrBLzRvDKfEzSVNgyZT8NxyI4pu0ZmjdNpqpe19GfiPB8rFxxNr8sMaYEvESKi3c8cBqheTVMgnxz8ogYmApn8fw+SV45a7QXfB/NF1GirP57VrOvdBj1vE08KWfyE8OAjpysESUCYmE5JeslwE4iwfU5rDDvwYGNlAD6QaKx2NnIH3GDUTA1DOaCkXS/Cyi6tSkxllOUtlmn0QmjSGz4dkH0kZdr3xao7KdykcfdDeqgPen7cVje/jnoLrXRwKsxcgOexH78O7V7m7Jq8r06xPLieVQ/X+BGAqzwN2EjixiOM/qT0IIW/P49+axsYhKYNLq+abVHJmU6XzBv8WyV04Bz5Dec3FlmG8L5RJZXStnp4jFjh8M/9m2Ow2z7qof6/bSzXlvszJgrxpBjQgH/CjA2Yml6AH6iZtD2j+DUOQyDc6kErWnB2REuEPgz04icSqkgVhpojbaX5Cw3xUZXzS2O51FeqKoqfn5uWZE9lW3os0oBkSI8Fhu5DripzUoTc8T7UAtrt+eanH9ahsAyZo6Wqy5b05+K2tukDBn8=----ATTACHMENT:----NDg3MTkyMzI3ODgzNTEyIDY4MjUwNTczMjAzMzg4NDkgODc2OTcxNjgwMzE0NDgyNg==