/** * action for routing all requests directly to the third party API * * @param Request $request request * * @return \Psr\Http\Message\ResponseInterface|Response */ public function proxyAction(Request $request) { $api = $this->decideApiAndEndpoint($request->getUri()); $this->registerProxySources(); $url = $this->apiLoader->getEndpoint($api['endpoint'], true); if (parse_url($url, PHP_URL_SCHEME) === false) { $scheme = $request->getScheme(); $url = $scheme . '://' . $url; } $response = null; try { $newRequest = Request::create($url, $request->getMethod(), array(), array(), array(), array(), $request->getContent(false)); $newRequest->headers->add($request->headers->all()); $newRequest = $this->transformationHandler->transformRequest($api['apiName'], $api['endpoint'], $request, $newRequest); $psrRequest = $this->diactorosFactory->createRequest($newRequest); $psrRequest = $psrRequest->withUri($psrRequest->getUri()->withPort(parse_url($url, PHP_URL_PORT))); $psrResponse = $this->proxy->forward($psrRequest)->to($this->getHostWithScheme($url)); $response = $this->httpFoundationFactory->createResponse($psrResponse); $this->transformationHandler->transformResponse($api['apiName'], $api['endpoint'], $response, clone $response); } catch (ClientException $e) { $response = $e->getResponse(); } catch (ServerException $serverException) { $response = $serverException->getResponse(); } return $response; }
/** * @test */ public function to_request_filter_returns_new_request() { $replace = new Request(); $this->proxy->addRequestFilter(function (Request $request) use($replace) { return $replace; }); $this->proxy->forward(Request::createFromGlobals())->to('/'); $this->assertEquals($this->proxy->getRequest(), $replace); }
/** * @test */ public function to_sends_request() { $request = Request::createFromGlobals(); $url = 'http://www.example.com'; $adapter = $this->getMockBuilder('\\Proxy\\Adapter\\Dummy\\DummyAdapter')->getMock(); $adapter->expects($this->once())->method('send')->with($request, $url)->willReturn(Response::create()); $proxy = new Proxy($adapter); $proxy->forward($request)->to($url); }
/** * @test */ public function to_sends_request() { $request = new Request('http://localhost/path?query=yes', 'GET'); $url = 'https://www.example.com'; $adapter = $this->getMockBuilder('Proxy\\Adapter\\Dummy\\DummyAdapter')->getMock(); $verifyParam = $this->callback(function (RequestInterface $request) use($url) { return $request->getUri() == 'https://www.example.com/path?query=yes'; }); $adapter->expects($this->once())->method('send')->with($verifyParam)->willReturn(new Response()); $proxy = new Proxy($adapter); $proxy->forward($request)->to($url); }
// use user plugin from /plugins/ require_once './plugins/' . $plugin_class . '.php'; } else { if (class_exists('\\Proxy\\Plugin\\' . $plugin_class)) { // does the native plugin from php-proxy package with such name exist? $plugin_class = '\\Proxy\\Plugin\\' . $plugin_class; } } // otherwise plugin_class better be loaded already through composer.json and match namespace exactly \\Vendor\\Plugin\\SuperPlugin $proxy->getEventDispatcher()->addSubscriber(new $plugin_class()); } try { // request sent to index.php $request = Request::createFromGlobals(); // remove all GET parameters such as ?q= $request->get->clear(); // forward it to some other URL $response = $proxy->forward($request, $url); // if that was a streaming response, then everything was already sent and script will be killed before it even reaches this line $response->send(); } catch (Exception $ex) { // if the site is on server2.proxy.com then you may wish to redirect it back to proxy.com if (Config::get("error_redirect")) { $url = render_string(Config::get("error_redirect"), array('error_msg' => rawurlencode($ex->getMessage()))); // Cannot modify header information - headers already sent header("HTTP/1.1 302 Found"); header("Location: {$url}"); } else { echo render_template("./templates/main.php", array('url' => $url, 'error_msg' => $ex->getMessage(), 'version' => Proxy::VERSION)); } }