Skip to content

Commit

Permalink
Make sure Transfer-Encoding is not set, regardless of case
Browse files Browse the repository at this point in the history
  • Loading branch information
Svish committed Feb 6, 2017
1 parent 1e984c9 commit 7bd010a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
23 changes: 15 additions & 8 deletions proxy.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
<?php

/**
* @param whitelist
* @param curl_opts
* @param zlib
*/

// Get stuff
$headers = getallheaders();
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
$url = $headers['X-Proxy-Url'] ?? null;
$cookie = $headers['X-Proxy-Cookie'] ?? null;



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



// Init curl
$curl = curl_init();
$maxredirs = $opts[CURLOPT_MAXREDIRS] ?? 20;
Expand Down Expand Up @@ -78,7 +84,6 @@
$out = ob_get_clean();

// Light error handling
// http://php.net/manual/en/curl.constants.php#117723
if(curl_errno($curl))
switch(curl_errno($curl))
{
Expand All @@ -95,7 +100,7 @@
failure(503, $curl);
}

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

// Get content and headers
$content = substr($out, $info['header_size']);
$header = substr($out, 0, $info['header_size']);
$headers = substr($out, 0, $info['header_size']);

// Rename Set-Cookie header
$header = preg_replace('/^Set-Cookie:/im', 'X-Proxy-Set-Cookie:', $header);
$headers = preg_replace('/^Set-Cookie:/im', 'X-Proxy-Set-Cookie:', $headers);

// Output headers
array_map('header', explode("\r\n", $header));
foreach(explode("\r\n", $headers) as $h)
// HACK: Prevent chunked encoding issues (Issue #1)
if( ! preg_match('/^Transfer-Encoding:/i', $h))
header($h, false);

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

// Output content
Expand Down
6 changes: 4 additions & 2 deletions test/echo.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

header_remove();
ini_set('zlib.output_compression', 'On');
header('Content-Type: application/json; charset=utf-8');

header('X-Test-Header: This header should come back through');
setcookie('Test-Cookie', uniqid());
session_name('Test-Session');
session_start();
setcookie('Test-Cookie-A', uniqid());
setcookie('Test-Cookie-B', uniqid(), strtotime( '+1 days' ));

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

0 comments on commit 7bd010a

Please sign in to comment.