Something went wrong. Try again.
@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
Something went wrong. Try again.
3.2 kB · 150 lines
PHP
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151<?php
final class AphrontHTTPHeaderParser extends Phobject {
private $name; private $content; private $pairs;
public function parseRawHeader($raw_header) { $this->name = null; $this->content = null;
$parts = explode(':', $raw_header, 2); $this->name = trim($parts[0]); if (count($parts) > 1) { $this->content = trim($parts[1]); }
$this->pairs = null;
return $this; }
public function getHeaderName() { $this->requireParse(); return $this->name; }
public function getHeaderContent() { $this->requireParse(); return $this->content; }
public function getHeaderContentAsPairs() { $content = $this->getHeaderContent();
$state = 'prekey'; $length = strlen($content);
$pair_name = null; $pair_value = null;
$pairs = array(); $ii = 0; while ($ii < $length) { $c = $content[$ii];
switch ($state) { case 'prekey': // We're eating space in front of a key. if ($c == ' ') { $ii++; break; } $pair_name = ''; $state = 'key'; break; case 'key': // We're parsing a key name until we find "=" or ";". if ($c == ';') { $state = 'done'; break; }
if ($c == '=') { $ii++; $state = 'value'; break; }
$ii++; $pair_name .= $c; break; case 'value': // We found an "=", so now figure out if the value is quoted // or not. if ($c == '"') { $ii++; $state = 'quoted'; break; } $state = 'unquoted'; break; case 'quoted': // We're in a quoted string, parse until we find the closing quote. if ($c == '"') { $ii++; $state = 'done'; break; }
$ii++; $pair_value .= $c; break; case 'unquoted': // We're in an unquoted string, parse until we find a space or a // semicolon. if ($c == ' ' || $c == ';') { $state = 'done'; break; } $ii++; $pair_value .= $c; break; case 'done': // We parsed something, so eat any trailing whitespace and semicolons // and look for a new value. if ($c == ' ' || $c == ';') { $ii++; break; }
$pairs[] = array( $pair_name, $pair_value, );
$pair_name = null; $pair_value = null;
$state = 'prekey'; break; } }
if ($state == 'quoted') { throw new Exception( pht( 'Header has unterminated double quote for key "%s".', $pair_name)); }
if ($pair_name !== null) { $pairs[] = array( $pair_name, $pair_value, ); }
return $pairs; }
private function requireParse() { if ($this->name === null) { throw new PhutilInvalidStateException('parseRawHeader'); } }
}