|
1 | 1 | <?php
|
2 | 2 |
|
| 3 | +/** |
| 4 | + * @param whitelist |
| 5 | + * @param curl_opts |
| 6 | + * @param zlib |
| 7 | + */ |
| 8 | + |
3 | 9 | // Get stuff
|
4 | 10 | $headers = getallheaders();
|
5 | 11 | $method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
|
6 | 12 | $url = $headers['X-Proxy-Url'] ?? null;
|
7 | 13 | $cookie = $headers['X-Proxy-Cookie'] ?? null;
|
8 | 14 |
|
9 | 15 |
|
| 16 | + |
10 | 17 | // Check that we have a URL
|
11 | 18 | if( ! $url)
|
12 | 19 | failure(400, "X-Proxy-Url header missing");
|
|
34 | 41 | $value = ucwords($key, '-').": $value";
|
35 | 42 |
|
36 | 43 |
|
37 |
| - |
38 | 44 | // Init curl
|
39 | 45 | $curl = curl_init();
|
40 | 46 | $maxredirs = $opts[CURLOPT_MAXREDIRS] ?? 20;
|
|
78 | 84 | $out = ob_get_clean();
|
79 | 85 |
|
80 | 86 | // Light error handling
|
81 |
| - // http://php.net/manual/en/curl.constants.php#117723 |
82 | 87 | if(curl_errno($curl))
|
83 | 88 | switch(curl_errno($curl))
|
84 | 89 | {
|
|
95 | 100 | failure(503, $curl);
|
96 | 101 | }
|
97 | 102 |
|
98 |
| - // HACK: If for any reason redirection doesn't work, do it manually... |
| 103 | + // HACK: Workaround if not following, which happened once... |
99 | 104 | $url = curl_getinfo($curl, CURLINFO_REDIRECT_URL);
|
100 | 105 | }
|
101 | 106 | while($url and --$maxredirs > 0);
|
|
116 | 121 |
|
117 | 122 | // Get content and headers
|
118 | 123 | $content = substr($out, $info['header_size']);
|
119 |
| -$header = substr($out, 0, $info['header_size']); |
| 124 | +$headers = substr($out, 0, $info['header_size']); |
120 | 125 |
|
121 | 126 | // Rename Set-Cookie header
|
122 |
| -$header = preg_replace('/^Set-Cookie:/im', 'X-Proxy-Set-Cookie:', $header); |
| 127 | +$headers = preg_replace('/^Set-Cookie:/im', 'X-Proxy-Set-Cookie:', $headers); |
123 | 128 |
|
124 | 129 | // Output headers
|
125 |
| -array_map('header', explode("\r\n", $header)); |
| 130 | +foreach(explode("\r\n", $headers) as $h) |
| 131 | + // HACK: Prevent chunked encoding issues (Issue #1) |
| 132 | + if( ! preg_match('/^Transfer-Encoding:/i', $h)) |
| 133 | + header($h, false); |
126 | 134 |
|
127 |
| -// HACK: Prevent chunked encoding and gz issues (Issue #1) |
128 |
| -header_remove('Transfer-Encoding'); |
| 135 | +// HACK: Prevent gzip issue (Issue #1) |
129 | 136 | header('Content-Length: '.strlen($content), true);
|
130 | 137 |
|
131 | 138 | // Output content
|
|
0 commit comments