1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-20 12:30:56 +01:00

Add an AphrontHTTPProxyResponse

Summary:
Ref T7019. Adds a new response which can proxy an HTTP request and pass the result through.

This is grossly inefficient for the same reasons as HTTP hosting is generally inefficient right now (T4369). This stuff is fixable but not trivial.

Test Plan: Replaced home page with a proxy to `example.org`, used Charles to view headers, saw the page headers and content proxy with an X-Phabricator-Proxied header.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: epriestley

Maniphest Tasks: T7019

Differential Revision: https://secure.phabricator.com/D11492
This commit is contained in:
epriestley 2015-01-27 14:50:52 -08:00
parent ac41db402a
commit 51b2c4d01e
2 changed files with 67 additions and 0 deletions

View file

@ -136,6 +136,7 @@ phutil_register_library_map(array(
'AphrontFormView' => 'view/form/AphrontFormView.php',
'AphrontGlyphBarView' => 'view/widget/bars/AphrontGlyphBarView.php',
'AphrontHTMLResponse' => 'aphront/response/AphrontHTMLResponse.php',
'AphrontHTTPProxyResponse' => 'aphront/response/AphrontHTTPProxyResponse.php',
'AphrontHTTPSink' => 'aphront/sink/AphrontHTTPSink.php',
'AphrontHTTPSinkTestCase' => 'aphront/sink/__tests__/AphrontHTTPSinkTestCase.php',
'AphrontIsolatedDatabaseConnectionTestCase' => 'infrastructure/storage/__tests__/AphrontIsolatedDatabaseConnectionTestCase.php',
@ -3248,6 +3249,7 @@ phutil_register_library_map(array(
'AphrontFormView' => 'AphrontView',
'AphrontGlyphBarView' => 'AphrontBarView',
'AphrontHTMLResponse' => 'AphrontResponse',
'AphrontHTTPProxyResponse' => 'AphrontResponse',
'AphrontHTTPSinkTestCase' => 'PhabricatorTestCase',
'AphrontIsolatedDatabaseConnectionTestCase' => 'PhabricatorTestCase',
'AphrontIsolatedHTTPSink' => 'AphrontHTTPSink',

View file

@ -0,0 +1,65 @@
<?php
/**
* Responds to a request by proxying an HTTP future.
*
* NOTE: This is currently very inefficient for large responses, and buffers
* the entire response into memory before returning it. It should be updated
* to stream the response instead, but we need to complete additional
* infrastructure work first.
*/
final class AphrontHTTPProxyResponse extends AphrontResponse {
private $future;
private $headers;
private $httpCode;
public function setHTTPFuture(HTTPSFuture $future) {
$this->future = $future;
return $this;
}
public function getHTTPFuture() {
return $this->future;
}
public function getCacheHeaders() {
return array();
}
public function getHeaders() {
$this->readRequestHeaders();
return array_merge(
parent::getHeaders(),
$this->headers,
array(
array('X-Phabricator-Proxy', 'true'),
));
}
public function buildResponseString() {
// TODO: AphrontResponse needs to support streaming responses.
return $this->readRequest();
}
public function getHTTPResponseCode() {
$this->readRequestHeaders();
return $this->httpCode;
}
private function readRequestHeaders() {
// TODO: This should read only the headers.
$this->readRequest();
}
private function readRequest() {
// TODO: This is grossly inefficient for large requests.
list($status, $body, $headers) = $this->future->resolve();
$this->httpCode = $status->getStatusCode();
$this->headers = $headers;
return $body;
}
}