1 | <?php |
||
29 | class ProxyController |
||
30 | { |
||
31 | /** |
||
32 | * @var Proxy |
||
33 | */ |
||
34 | private $proxy; |
||
35 | |||
36 | /** |
||
37 | * @var EngineInterface |
||
38 | */ |
||
39 | private $templating; |
||
40 | |||
41 | /** |
||
42 | * @var DiactorosFactory |
||
43 | */ |
||
44 | private $diactorosFactory; |
||
45 | |||
46 | /** |
||
47 | * @var ApiDefinitionLoader |
||
48 | */ |
||
49 | private $apiLoader; |
||
50 | |||
51 | /** |
||
52 | * @var HttpFoundationFactory |
||
53 | */ |
||
54 | private $httpFoundationFactory; |
||
55 | |||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | private $proxySourceConfiguration; |
||
60 | |||
61 | /** |
||
62 | * @var TransformationHandler |
||
63 | */ |
||
64 | private $transformationHandler; |
||
65 | |||
66 | /** |
||
67 | * Constructor |
||
68 | * |
||
69 | * @param Proxy $proxy proxy |
||
70 | * @param EngineInterface $templating twig templating engine |
||
71 | * @param ApiDefinitionLoader $loader definition loader |
||
72 | * @param DiactorosFactory $diactorosFactory convert HttpFoundation objects to PSR-7 |
||
73 | * @param HttpFoundationFactory $httpFoundationFactory convert PSR-7 interfaces to HttpFoundation |
||
74 | * @param TransformationHandler $transformationHandler transformation handler |
||
75 | * @param array $proxySourceConfiguration Set of sources to be recognized by the controller. |
||
76 | */ |
||
77 | public function __construct( |
||
78 | Proxy $proxy, |
||
79 | EngineInterface $templating, |
||
80 | ApiDefinitionLoader $loader, |
||
81 | DiactorosFactory $diactorosFactory, |
||
82 | HttpFoundationFactory $httpFoundationFactory, |
||
83 | TransformationHandler $transformationHandler, |
||
84 | array $proxySourceConfiguration |
||
85 | ) { |
||
86 | $this->proxy = $proxy; |
||
87 | $this->templating = $templating; |
||
88 | $this->apiLoader = $loader; |
||
89 | $this->diactorosFactory = $diactorosFactory; |
||
90 | $this->httpFoundationFactory = $httpFoundationFactory; |
||
91 | $this->proxySourceConfiguration = $proxySourceConfiguration; |
||
92 | $this->transformationHandler = $transformationHandler; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * action for routing all requests directly to the third party API |
||
97 | * |
||
98 | * @param Request $request request |
||
99 | * |
||
100 | * @return \Psr\Http\Message\ResponseInterface|Response |
||
101 | */ |
||
102 | public function proxyAction(Request $request) |
||
103 | { |
||
104 | $api = $this->decideApiAndEndpoint($request->getUri()); |
||
105 | $this->registerProxySources($api['apiName']); |
||
106 | |||
107 | $url = $this->apiLoader->getEndpoint($api['endpoint'], true); |
||
108 | if (parse_url($url, PHP_URL_SCHEME) === false) { |
||
109 | $scheme = $request->getScheme(); |
||
110 | $url = $scheme.'://'.$url; |
||
111 | } |
||
112 | $response = null; |
||
113 | try { |
||
114 | $newRequest = Request::create( |
||
115 | $url, |
||
116 | $request->getMethod(), |
||
117 | array (), |
||
118 | array (), |
||
119 | array (), |
||
120 | array (), |
||
121 | $request->getContent(false) |
||
|
|||
122 | ); |
||
123 | $newRequest->headers->add($request->headers->all()); |
||
124 | |||
125 | $newRequest = $this->transformationHandler->transformRequest( |
||
126 | $api['apiName'], |
||
127 | $api['endpoint'], |
||
128 | $request, |
||
129 | $newRequest |
||
130 | ); |
||
131 | $psrRequest = $this->diactorosFactory->createRequest($newRequest); |
||
132 | $psrRequest = $psrRequest->withUri($psrRequest->getUri()->withPort(parse_url($url, PHP_URL_PORT))); |
||
133 | $psrResponse = $this->proxy->forward($psrRequest)->to($this->getHostWithScheme($url)); |
||
134 | $response = $this->httpFoundationFactory->createResponse($psrResponse); |
||
135 | $this->cleanResponseHeaders($response->headers); |
||
136 | $this->transformationHandler->transformResponse( |
||
137 | $api['apiName'], |
||
138 | $api['endpoint'], |
||
139 | $response, |
||
140 | clone $response |
||
141 | ); |
||
142 | } catch (ClientException $e) { |
||
143 | $response = $e->getResponse(); |
||
144 | } catch (ServerException $serverException) { |
||
145 | $response = $serverException->getResponse(); |
||
146 | } |
||
147 | |||
148 | return $response; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Removes some headers from the thirdparty API's response. These headers get always invalid by graviton's |
||
153 | * forwarding and should therefore not be delivered to the client. |
||
154 | * |
||
155 | * @param HeaderBag $headers The headerbag holding the thirdparty API's response headers |
||
156 | * |
||
157 | * @return void |
||
158 | */ |
||
159 | protected function cleanResponseHeaders(HeaderBag $headers) |
||
164 | |||
165 | /** |
||
166 | * get schema info |
||
167 | * |
||
168 | * @param Request $request request |
||
169 | * |
||
170 | * @return Response |
||
171 | */ |
||
172 | public function schemaAction(Request $request) |
||
192 | |||
193 | /** |
||
194 | * get API name and endpoint from the url (third party API) |
||
195 | * |
||
196 | * @param string $url the url |
||
197 | * |
||
198 | * @return array |
||
199 | */ |
||
200 | protected function decideApiAndEndpoint($url) |
||
220 | |||
221 | /** |
||
222 | * Registers configured external services to be proxied. |
||
223 | * |
||
224 | * @param string $apiPrefix The prefix of the API |
||
225 | * |
||
226 | * @return void |
||
227 | */ |
||
228 | private function registerProxySources($apiPrefix = '') |
||
242 | |||
243 | /** |
||
244 | * get host, scheme and port |
||
245 | * |
||
246 | * @param string $url the url |
||
247 | * |
||
248 | * @return string |
||
249 | */ |
||
250 | private function getHostWithScheme($url) |
||
260 | } |
||
261 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.