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

Clarification with SERVER_PORT #45

Closed
EvanCarroll opened this issue Feb 14, 2021 · 6 comments
Closed

Clarification with SERVER_PORT #45

EvanCarroll opened this issue Feb 14, 2021 · 6 comments

Comments

@EvanCarroll
Copy link

EvanCarroll commented Feb 14, 2021

The PSGI spec doesn't mention using HTTP_HOST in preference to SERVER_PORT. Just SERVER_NAME

SERVER_NAME, SERVER_PORT: When combined with SCRIPT_NAME and PATH_INFO, these keys can be used to complete the URL. Note, however, that HTTP_HOST, if present, should be used in preference to SERVER_NAME for reconstructing the request URL. SERVER_NAME and SERVER_PORT MUST NOT be empty strings, and are always required.

The code for Plack uses SERVER_PORT only to source the port..

What should happen if the HTTP_HOST is "127.0.0.1:12345" and the SERVER_PORT is "54321" (ie, if they conflict). The WSGI spec has pseudo code in it which takes HTTP_HOST (ie conflicts with Plack as I read it). I've linked a request for clarification on PEP 333 below.

from urllib import quote
url = environ['wsgi.url_scheme']+'://'

if environ.get('HTTP_HOST'):
    url += environ['HTTP_HOST']
else:
    url += environ['SERVER_NAME']

    if environ['wsgi.url_scheme'] == 'https':
        if environ['SERVER_PORT'] != '443':
           url += ':' + environ['SERVER_PORT']
    else:
        if environ['SERVER_PORT'] != '80':
           url += ':' + environ['SERVER_PORT']

url += quote(environ.get('SCRIPT_NAME', ''))
url += quote(environ.get('PATH_INFO', ''))
if environ.get('QUERY_STRING'):
    url += '?' + environ['QUERY_STRING']
@miyagawa
Copy link
Member

miyagawa commented Feb 14, 2021

The PSGI spec has pseudo code in it which takes HTTP_HOST

You meant the WSGI spec.

(ie conflicts with Plack as I read it).

I don't think there's a conflict, and you're reading the Plack example code wrong - the code in question uses HTTP_HOST in its entirety, and not use SERVER_PORT. I know the amount of parentheses might be confusing you.

    my $uri = ($env->{'psgi.url_scheme'} || "http") .
        "://" .
        ($env->{HTTP_HOST} || (($env->{SERVER_NAME} || "") . ":" . ($env->{SERVER_PORT} || 80))) .
        ($env->{SCRIPT_NAME} || '/');

which is the same as:

    my $uri = ($env->{'psgi.url_scheme'} || "http") .
        "://" .
        (
          $env->{HTTP_HOST} ||
          (($env->{SERVER_NAME} || "") . ":" . ($env->{SERVER_PORT} || 80))
        ) .
        ($env->{SCRIPT_NAME} || '/');

@EvanCarroll
Copy link
Author

Wow, you got me with those parens. One point for that one +1. Ok so the Plack and PSGI spec are in line. I guess if you want to write a server that works on different systems, CGI and PSGI for example you'll have to have different branches because they're incompatible specs for port resolution (psgi using HTTP_HOST, cgi using SERVER_PORT).

@miyagawa
Copy link
Member

because they're incompatible specs for port resolution (psgi using HTTP_HOST, cgi using SERVER_PORT).

I don't believe they're necessarily incompatible but if that's the case it's a job for the CGI-PSGI adapter to adjust these environment variables as needed, since it's clearly specified in the PSGI spec.

@EvanCarroll
Copy link
Author

EvanCarroll commented Feb 16, 2021

PEP3333 has been updated to clarify (on WSGI side): python/peps@5f16dd9

@miyagawa
Copy link
Member

You linked to your fork, which is not exactly what was merged as I see it.
python/peps#1817

As stated in the PR above by the author, "violation of the CGI spec" doesn't really reflect what we need to do in the real world, since the CGI spec was written before virtual hosting with Host header became universal, and before it becomes more common to put reverse proxy/frontend web servers in front of the backend.

I'll only accept PRs similar to above for clarifications, but not actually changing the requirement for URL reconstruction.

@EvanCarroll
Copy link
Author

Yep, wrong link my bad. You found the right one.

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

No branches or pull requests

2 participants