Skip to content

Commit

Permalink
Rewrite Set-Cookie domain name to no.php host
Browse files Browse the repository at this point in the history
  • Loading branch information
GiovanniMounir authored and michaelfranzl committed Oct 15, 2020
1 parent 75982ba commit cd4df25
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions no.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@


$backend_url = "https://myapp.backend.com:3000/";
$backend_info = parse_url($backend_url);
$host = $_SERVER['HTTP_HOST'];
$request_uri = $_SERVER['REQUEST_URI'];
$uri_rel = "subdir/no.php"; # URI to this file relative to public_html

Expand Down Expand Up @@ -66,6 +68,18 @@ function getRequestHeaders($multipart_delimiter=NULL) {
return $headers;
}

function build_domain_regex($hostname)
{
$names = explode('.', $hostname); //assumes main domain is the TLD
$regex = "";
for ($i= 0; $i < count ($names)-2; $i++)
{
$regex .= '['.$names[$i].'.]?';
}
$main_domain = $names[count($names)-2] .".". $names[count($names)-1];
$regex .= $main_domain;
return $regex;
}

function build_multipart_data_files($delimiter, $fields, $files) {
# Inspiration from: https://gist.github.com/maxivak/18fcac476a2f4ea02e5f80b303811d5f :)
Expand Down Expand Up @@ -125,6 +139,11 @@ function build_multipart_data_files($delimiter, $fields, $files) {
# rewrite absolute local redirects to relative ones
$header = str_replace($backend_url, "/", $header);
}
else if ( preg_match( '/^set-cookie:/i', $header ) ) {
# replace original domain name in Set-Cookie headers with our server's domain
$domain_regex = build_domain_regex($backend_info['host']);
$header = preg_replace('/Domain='.$domain_regex.'/', 'Domain='.$host, $header);
}
header( $header );
}
}
Expand Down

0 comments on commit cd4df25

Please sign in to comment.