diff --git a/http_proxy.php b/http_proxy.php new file mode 100644 index 0000000..f69c350 --- /dev/null +++ b/http_proxy.php @@ -0,0 +1,85 @@ + $_SERVER['REQUEST_METHOD'], + 'headers' => [ + ...getallheaders(), + 'Host' => $host, + ], + 'body_stream' => $_SERVER['REQUEST_METHOD'] === 'POST' ? fopen('php://input', 'r') : null, + ] + ), +]; + +$client = new Client(); +$client->enqueue( $requests ); + +$headers_sent = false; +while ( $client->await_next_event() ) { + $request = $client->get_request(); + switch ( $client->get_event() ) { + case Client::EVENT_GOT_HEADERS: + http_response_code($request->response->status_code); + foreach ( $request->response->get_headers() as $name => $value ) { + if( + $name === 'transfer-encoding' || + $name === 'set-cookie' || + $name === 'content-encoding' + ) { + continue; + } + header("$name: $value"); + } + $headers_sent = true; + break; + case Client::EVENT_BODY_CHUNK_AVAILABLE: + echo $client->get_response_body_chunk(); + break; + case Client::EVENT_FAILED: + if(!$headers_sent) { + http_response_code(500); + echo "Failed request to " . $request->url . " – " . $request->error; + } + break; + case Client::EVENT_REDIRECT: + case Client::EVENT_FINISHED: + } + echo "\n"; +} +