Skip to content

Commit

Permalink
Convert to curl header format after filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
maxschmeling committed Dec 4, 2024
1 parent 78501d3 commit d658b45
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
23 changes: 12 additions & 11 deletions packages/playground/php-cors-proxy/cors-proxy-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ private static function ipv6InRange($ip, $start, $end)


function filter_headers_strings($php_headers, $allowed_headers, $remove_headers) {
$allowed_headers = $allowed_headers ?? [];
$remove_headers = $remove_headers ?? [];

$allowed_request_headers_header = strtolower('X-Cors-Proxy-Allowed-Request-Headers');

// Add any additional allowed headers from X-Cors-Proxy-Allowed-Request-Headers
Expand All @@ -326,17 +329,15 @@ function filter_headers_strings($php_headers, $allowed_headers, $remove_headers)
});

$remove_headers = array_map('strtolower', $remove_headers);
$headers = [];
foreach ($php_headers as $header) {
$lower_header = strtolower($header);
foreach($remove_headers as $remove_header) {
if (strpos($lower_header, $remove_header) === 0) {
continue 2;
}
}
$headers[] = $header;
}
return $headers;

// Remove strictly disallowed headers
return array_filter(
$php_headers,
function($key) use ($remove_headers) {
return !in_array(strtolower($key), $remove_headers);
},
ARRAY_FILTER_USE_KEY
);
}

function kv_headers_to_curl_format($headers) {
Expand Down
10 changes: 6 additions & 4 deletions packages/playground/php-cors-proxy/cors-proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,12 @@
'Cookie',
'Host'
];
$curlHeaders = filter_headers_strings(
kv_headers_to_curl_format(getallheaders()),
$default_allowed_headers,
$strictly_disallowed_headers
$curlHeaders = kv_headers_to_curl_format(
filter_headers_strings(
getallheaders(),
$default_allowed_headers,
$strictly_disallowed_headers
)
);
curl_setopt(
$ch,
Expand Down

0 comments on commit d658b45

Please sign in to comment.