Changeset View
Changeset View
Standalone View
Standalone View
src/parser/http/PhutilHTTPResponseParser.php
<?php | <?php | ||||
final class PhutilHTTPResponseParser extends Phobject { | final class PhutilHTTPResponseParser extends Phobject { | ||||
private $followLocationHeaders; | private $followLocationHeaders; | ||||
private $responses = array(); | private $responses = array(); | ||||
private $response; | private $response; | ||||
private $buffer; | private $buffer; | ||||
private $state = 'headers'; | private $state = 'headers'; | ||||
private $writeHandle; | |||||
public function setFollowLocationHeaders($follow_location_headers) { | public function setFollowLocationHeaders($follow_location_headers) { | ||||
$this->followLocationHeaders = $follow_location_headers; | $this->followLocationHeaders = $follow_location_headers; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function getFollowLocationHeaders() { | public function getFollowLocationHeaders() { | ||||
return $this->followLocationHeaders; | return $this->followLocationHeaders; | ||||
} | } | ||||
public function setWriteHandle($write_handle) { | |||||
$this->writeHandle = $write_handle; | |||||
return $this; | |||||
} | |||||
public function getWriteHandle() { | |||||
return $this->writeHandle; | |||||
} | |||||
public function readBytes($bytes) { | public function readBytes($bytes) { | ||||
if ($this->state == 'discard') { | if ($this->state == 'discard') { | ||||
return $this; | return $this; | ||||
} | } | ||||
$this->buffer .= $bytes; | $this->buffer .= $bytes; | ||||
while (true) { | while (true) { | ||||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | while (true) { | ||||
// line, or any of the header lines were not formatted correctly, add | // line, or any of the header lines were not formatted correctly, add | ||||
// a malformed response to the response list and discard anything else | // a malformed response to the response list and discard anything else | ||||
// we're given. | // we're given. | ||||
if (!$response_valid) { | if (!$response_valid) { | ||||
$malformed = new HTTPFutureParseResponseStatus( | $malformed = new HTTPFutureParseResponseStatus( | ||||
HTTPFutureParseResponseStatus::ERROR_MALFORMED_RESPONSE, | HTTPFutureParseResponseStatus::ERROR_MALFORMED_RESPONSE, | ||||
$raw_headers); | $raw_headers); | ||||
$this->newHTTPRepsonse() | $this->newHTTPResponse() | ||||
->setStatus($malformed); | ->setStatus($malformed); | ||||
$this->buffer = ''; | $this->buffer = ''; | ||||
$this->state = 'discard'; | $this->state = 'discard'; | ||||
break; | break; | ||||
} | } | ||||
// Otherwise, we have a valid set of response headers. | // Otherwise, we have a valid set of response headers. | ||||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | if ($this->state !== 'body') { | ||||
$this->buffer); | $this->buffer); | ||||
} | } | ||||
return $this->responses; | return $this->responses; | ||||
} | } | ||||
private function newHTTPResponse() { | private function newHTTPResponse() { | ||||
$response = new PhutilHTTPResponse(); | $response = new PhutilHTTPResponse(); | ||||
$write_handle = $this->getWriteHandle(); | |||||
if ($write_handle) { | |||||
$response->setWriteHandle($write_handle); | |||||
} | |||||
$this->responses[] = $response; | $this->responses[] = $response; | ||||
$this->response = $response; | $this->response = $response; | ||||
return $response; | return $response; | ||||
} | } | ||||
} | } |