From 51b2c4d01eed1500ab896226b1df5d0541f46452 Mon Sep 17 00:00:00 2001 From: epriestley Date: Tue, 27 Jan 2015 14:50:52 -0800 Subject: [PATCH] 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 --- src/__phutil_library_map__.php | 2 + .../response/AphrontHTTPProxyResponse.php | 65 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/aphront/response/AphrontHTTPProxyResponse.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index b30b54e9b5..2fac7ce13a 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -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', diff --git a/src/aphront/response/AphrontHTTPProxyResponse.php b/src/aphront/response/AphrontHTTPProxyResponse.php new file mode 100644 index 0000000000..c2e5bda603 --- /dev/null +++ b/src/aphront/response/AphrontHTTPProxyResponse.php @@ -0,0 +1,65 @@ +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; + } + +} -- 2.51.2