Skip to content

Commit

Permalink
Restore CORS support to CORS proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonpayton committed Dec 4, 2024
1 parent 51db66a commit 8047847
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/playground/php-cors-proxy/cors-proxy-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,24 @@ function rewrite_relative_redirect(
}
return $proxy_absolute_url . $redirect_location;
}

/**
* Answers whether CORS is allowed for the specified origin.
*/
function should_respond_with_cors_headers($host, $origin) {
if (
$host !== 'playground.wordpress.net' &&
$origin === 'https://playground.wordpress.net'
) {
return true;
}

$origin_host = parse_url($_SERVER['HTTP_ORIGIN'], PHP_URL_HOST);
$is_local_origin = in_array(
$origin_host,
array('localhost', '127.0.0.1'),
true
);

return $is_local_origin;
}
10 changes: 10 additions & 0 deletions packages/playground/php-cors-proxy/cors-proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
require_once $config_file;
}

$server_host = $_SERVER['HTTP_HOST'] ?? '';
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';

if (should_respond_with_cors_headers($server_host, $origin)) {
header('Access-Control-Allow-Origin: ' . $origin);
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Authorization, Content-Type');
}

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
header("Allow: GET, POST, OPTIONS");
exit;
Expand Down

0 comments on commit 8047847

Please sign in to comment.