Skip to content

Commit 7bd010a

Browse files
committed
Make sure Transfer-Encoding is not set, regardless of case
1 parent 1e984c9 commit 7bd010a

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

proxy.php

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
<?php
22

3+
/**
4+
* @param whitelist
5+
* @param curl_opts
6+
* @param zlib
7+
*/
8+
39
// Get stuff
410
$headers = getallheaders();
511
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
612
$url = $headers['X-Proxy-Url'] ?? null;
713
$cookie = $headers['X-Proxy-Cookie'] ?? null;
814

915

16+
1017
// Check that we have a URL
1118
if( ! $url)
1219
failure(400, "X-Proxy-Url header missing");
@@ -34,7 +41,6 @@
3441
$value = ucwords($key, '-').": $value";
3542

3643

37-
3844
// Init curl
3945
$curl = curl_init();
4046
$maxredirs = $opts[CURLOPT_MAXREDIRS] ?? 20;
@@ -78,7 +84,6 @@
7884
$out = ob_get_clean();
7985

8086
// Light error handling
81-
// http://php.net/manual/en/curl.constants.php#117723
8287
if(curl_errno($curl))
8388
switch(curl_errno($curl))
8489
{
@@ -95,7 +100,7 @@
95100
failure(503, $curl);
96101
}
97102

98-
// HACK: If for any reason redirection doesn't work, do it manually...
103+
// HACK: Workaround if not following, which happened once...
99104
$url = curl_getinfo($curl, CURLINFO_REDIRECT_URL);
100105
}
101106
while($url and --$maxredirs > 0);
@@ -116,16 +121,18 @@
116121

117122
// Get content and headers
118123
$content = substr($out, $info['header_size']);
119-
$header = substr($out, 0, $info['header_size']);
124+
$headers = substr($out, 0, $info['header_size']);
120125

121126
// 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);
123128

124129
// 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);
126134

127-
// HACK: Prevent chunked encoding and gz issues (Issue #1)
128-
header_remove('Transfer-Encoding');
135+
// HACK: Prevent gzip issue (Issue #1)
129136
header('Content-Length: '.strlen($content), true);
130137

131138
// Output content

test/echo.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010

1111
header_remove();
1212
ini_set('zlib.output_compression', 'On');
13-
header('Content-Type: application/json; charset=utf-8');
13+
1414
header('X-Test-Header: This header should come back through');
15-
setcookie('Test-Cookie', uniqid());
1615
session_name('Test-Session');
1716
session_start();
17+
setcookie('Test-Cookie-A', uniqid());
18+
setcookie('Test-Cookie-B', uniqid(), strtotime( '+1 days' ));
1819

20+
header('Content-Type: application/json; charset=utf-8');
1921
echo json_encode(array_filter($info), JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK | JSON_FORCE_OBJECT);

0 commit comments

Comments
 (0)