Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing redirects #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

simonseyock
Copy link

@simonseyock simonseyock commented Aug 8, 2017

The redirects were broken as there were multiple csurl parameters pasted together with '?' in the given location. This was causing "too many redirects" errors. (The $_SERVER['REQUEST_URI'] variable contains the exact uri that was called, including the query_parameters)
In this solution the csurl parameter gets replaced.

The redirects where broken as there where multiple csurl parameters pasted together with '?' in the given location. This was causing "too many redirects" errors. (The $_SERVER['REQUEST_URI'] variable contains the exact uri that was called, including the query_parameters)
In this solution the csurl parameter gets replaced.
@simonseyock
Copy link
Author

simonseyock commented Aug 15, 2017

Assuming the called address is proxy.php?csurl=some.url and the request to some.url returns a redirect to other.url

$_SERVER['REQUEST_URI'] therefore contains proxy.php?csurl=some.url

Current version:
code: 'Location: ' . $_SERVER['REQUEST_URI'] . '?csurl=' . $value;
result: 'Location: proxy.php?csurl=some.url?csurl=other.url
if the browser now calls this url it is just evaluated as another call to some.url and thus causing another redirect. This continues until the "too many redirects" limit is hit.

fixed version:
code: 'Location: ' . preg_replace('/\?csurl=.*/', '?csurl='.urlencode($value), $_SERVER['REQUEST_URI']);
result: 'Location: proxy.php?csurl=other.url'
I don't know exactly if the urlencode is really necessary, but it does not hurt, as the parameters get decoded automatically in the $_GET etc. vars

@likev
Copy link

likev commented Jun 10, 2022

I think you just introduce a new bug.
When $_SERVER['REQUEST_URI'] doesn't contain csurl=some.url preg_replace will not working.

The following code should work:

        $server_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
        $response_header = 'Location: ' . $server_path . '?csurl=' . $value;

Assuming the called address is proxy.php?csurl=some.url and the request to some.url returns a redirect to other.url

$_SERVER['REQUEST_URI'] therefore contains proxy.php?csurl=some.url

Current version: code: 'Location: ' . $_SERVER['REQUEST_URI'] . '?csurl=' . $value; result: 'Location: proxy.php?csurl=some.url?csurl=other.url if the browser now calls this url it is just evaluated as another call to some.url and thus causing another redirect. This continues until the "too many redirects" limit is hit.

fixed version: code: 'Location: ' . preg_replace('/\?csurl=.*/', '?csurl='.urlencode($value), $_SERVER['REQUEST_URI']); result: 'Location: proxy.php?csurl=other.url' I don't know exactly if the urlencode is really necessary, but it does not hurt, as the parameters get decoded automatically in the $_GET etc. vars

@simonseyock
Copy link
Author

simonseyock commented Jun 11, 2022

This is quite a long time ago, but i think the point is that nothing needs to be replaced if there is no csurl in the address.
Can't say what your version does from the top of my head because I don't know what parse_url does in this context.

I can say that at some point we decided to fork this repo here: https://github.com/KlausBenndorf/guide4you-proxy
The fork contains some rework and improvements, but I can't be used 'as is' because it contains templating values in the beginning that need to be filled in.

@simonseyock
Copy link
Author

does your version retain other query parameters?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants