22
33namespace CachingProxy ;
44
5+ use RuntimeException ;
6+
57class ProxyServer
68{
79 public function __construct (
810 private FileCache $ cache = new FileCache ()
911 ) {
1012 }
1113
12- public function handleRequest ()
14+ public function handleRequest (): void
1315 {
1416 header ('Access-Control-Allow-Origin: * ' );
1517 header ('Access-Control-Allow-Methods: GET, POST, OPTIONS ' );
@@ -63,7 +65,13 @@ private function getTargetUrl(): ?string
6365 return urldecode ($ _GET ['url ' ]);
6466 }
6567
66- $ input = json_decode (file_get_contents ('php://input ' ), true );
68+ $ requestBody = file_get_contents ('php://input ' );
69+
70+ if ($ requestBody === false ) {
71+ throw new RuntimeException ("Failed to read `POST` request body " );
72+ }
73+
74+ $ input = json_decode ($ requestBody , true );
6775
6876 if ($ input ['url ' ] !== null ) {
6977 return $ input ['url ' ];
@@ -88,7 +96,10 @@ private function checkFilterUrl(string $url): bool
8896 return true ;
8997 }
9098
91- private function makeRequest (string $ url )
99+ /**
100+ * @return array<mixed>
101+ */
102+ private function makeRequest (string $ url ): array
92103 {
93104 $ ch = curl_init ();
94105
@@ -107,7 +118,14 @@ private function makeRequest(string $url)
107118 // Handle POST requests
108119 if ($ _SERVER ['REQUEST_METHOD ' ] === 'POST ' ) {
109120 curl_setopt ($ ch , CURLOPT_POST , true );
110- curl_setopt ($ ch , CURLOPT_POSTFIELDS , file_get_contents ('php://input ' ));
121+
122+ $ requestBody = file_get_contents ('php://input ' );
123+
124+ if ($ requestBody === false ) {
125+ throw new RuntimeException ("Failed to read `POST` request body " );
126+ }
127+
128+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ requestBody );
111129 }
112130
113131 // Forward headers (excluding some sensitive headers)
@@ -159,7 +177,10 @@ private function makeRequest(string $url)
159177 ];
160178 }
161179
162- private function cacheResponse ($ url , $ response )
180+ /**
181+ * @param array<mixed> $response
182+ */
183+ private function cacheResponse (string $ url , array $ response ): void
163184 {
164185 if ($ response ['status_code ' ] === 200 ) {
165186 header ('X-Proxy-Cache: MISS ' );
@@ -168,13 +189,19 @@ private function cacheResponse($url, $response)
168189 }
169190 }
170191
171- private function sendCachedResponse ($ cachedResponse )
192+ /**
193+ * @param array<mixed> $cachedResponse
194+ */
195+ private function sendCachedResponse (array $ cachedResponse ): void
172196 {
173197 header ('X-Proxy-Cache: HIT ' );
174198 $ this ->sendResponse ($ cachedResponse );
175199 }
176200
177- private function sendResponse (array $ response )
201+ /**
202+ * @param array<mixed> $response
203+ */
204+ private function sendResponse (array $ response ): void
178205 {
179206 http_response_code ($ response ['status_code ' ]);
180207
@@ -190,7 +217,7 @@ private function sendResponse(array $response)
190217 echo $ response ['body ' ];
191218 }
192219
193- private function sendError ($ message , $ statusCode = 500 )
220+ private function sendError (string $ message , int $ statusCode = 500 ): void
194221 {
195222 http_response_code ($ statusCode );
196223 header ('Content-Type: application/json ' );
0 commit comments