Path: blob/master/src/aphront/response/AphrontHTTPProxyResponse.php
12241 views
<?php12/**3* Responds to a request by proxying an HTTP future.4*5* NOTE: This is currently very inefficient for large responses, and buffers6* the entire response into memory before returning it. It should be updated7* to stream the response instead, but we need to complete additional8* infrastructure work first.9*/10final class AphrontHTTPProxyResponse extends AphrontResponse {1112private $future;13private $headers;14private $httpCode;1516public function setHTTPFuture(HTTPSFuture $future) {17$this->future = $future;18return $this;19}2021public function getHTTPFuture() {22return $this->future;23}2425public function getCacheHeaders() {26return array();27}2829public function getHeaders() {30$this->readRequestHeaders();31return array_merge(32parent::getHeaders(),33$this->headers,34array(35array('X-Phabricator-Proxy', 'true'),36));37}3839public function buildResponseString() {40// TODO: AphrontResponse needs to support streaming responses.41return $this->readRequest();42}4344public function getHTTPResponseCode() {45$this->readRequestHeaders();46return $this->httpCode;47}4849private function readRequestHeaders() {50// TODO: This should read only the headers.51$this->readRequest();52}5354private function readRequest() {55// TODO: This is grossly inefficient for large requests.5657list($status, $body, $headers) = $this->future->resolve();58$this->httpCode = $status->getStatusCode();5960// Strip "Transfer-Encoding" headers. Particularly, the server we proxied61// may have chunked the response, but cURL will already have un-chunked it.62// If we emit the header and unchunked data, the response becomes invalid.6364// See T13517. Strip "Content-Encoding" and "Content-Length" headers, since65// they may reflect compressed content.6667foreach ($headers as $key => $header) {68list($header_head, $header_body) = $header;69$header_head = phutil_utf8_strtolower($header_head);70switch ($header_head) {71case 'transfer-encoding':72case 'content-encoding':73case 'content-length':74unset($headers[$key]);75break;76}77}7879$this->headers = $headers;8081return $body;82}8384}858687