Skip to content
This repository has been archived by the owner on Nov 23, 2020. It is now read-only.

SERVER_NAME differs from wsgiref #162

Closed
msornay opened this issue Oct 1, 2015 · 9 comments
Closed

SERVER_NAME differs from wsgiref #162

msornay opened this issue Oct 1, 2015 · 9 comments

Comments

@msornay
Copy link

msornay commented Oct 1, 2015

(when using an /etc/hosts entry)

Hello,

Consider the following code :

import wsgiref.simple_server as wsgiref
import pulsar.apps.wsgi as wsgi  

def server_name(environ, start_response):                                                                                                      
    start_response('200 OK', [('Content-Type','text/plain; charset=utf-8')])                                                                   
    return [environ['SERVER_NAME'].encode('utf-8')]

if __name__ == '__main__':                                                                                                                     
    # wsgi.WSGIServer(callable=server_name).start()                                                                                            
    wsgiref.make_server('0.0.0.0', 8000, server_name).serve_forever()

If this runs on a remote server, for which I have an entry "server" in my /etc/hosts and I use :

wget -qO- http://server:8000

It returns the remote server hostname ( as returned by gethostname() )

When using Pulsar's WSGIServer (with --bind 0.0.0.0:8000) it returns 'server', probably because socket.getfqdn('server') fails in server.py and return its argument (and it takes a lot of time to do so because it triggers a DNS lookup, which is problematic in my setup).

I have not taken the time to dive further in the server.py code yet to make a pull request, but I will unless the fix is obvious to you.

Thanks !

@lsbardel
Copy link
Member

lsbardel commented Oct 1, 2015

Hi, not obvious to me, any pull request welcome ;-)

@lsbardel lsbardel changed the title SERVER_NAME differs from wsgiref when using an /etc/hosts entry SERVER_NAME differs from wsgiref Oct 1, 2015
@lsbardel
Copy link
Member

lsbardel commented Oct 2, 2015

I've added an endpoint to the HttpBin router, you can use that for tests if you like

@msornay
Copy link
Author

msornay commented Oct 2, 2015

Thanks! I'm looking into it.

@msornay
Copy link
Author

msornay commented Oct 2, 2015

Could you please have a look at https://github.com/msornay/pulsar/commit/633b8622cf48b9d1b676977d8d949afaf01a656e and give me advices ?

It provides what I think is the correct behavior, i.e. the result of socket.getfqdn() on the address returned by socket.getsockname(). This is what happens here.

However :

  • It might not be necessary to do socket.getfqdn() on each requests ;
  • It might change the way "host errors" are handled in HTTPServerResponse._response ;
  • The test I wrote might be incorrect. The bug is only really clear when the remote server running pulsar cannot resolve the HTTP_HOST itself, which is hard to reproduce when the client and the server are on the same machine.

Thanks !

@lsbardel
Copy link
Member

lsbardel commented Oct 4, 2015

Looks good to me, can you pull request? I'll code review there and check test passage. Many thanks

lsbardel added a commit that referenced this issue Oct 6, 2015
SERVER_NAME using getfqdn() on getsockname() #162
@lsbardel
Copy link
Member

lsbardel commented Oct 7, 2015

merged with master, thanks!

@lsbardel lsbardel closed this as completed Oct 7, 2015
@lsbardel lsbardel reopened this Oct 8, 2015
@lsbardel
Copy link
Member

lsbardel commented Oct 8, 2015

@msornay your test fails on mac OS X.

@msornay
Copy link
Author

msornay commented Oct 8, 2015

I have unfortunately no way of testing that myself. Could you give me the output ? Thanks.

@lsbardel
Copy link
Member

On my mac when I do:

>>> server = socket.getfqdn('::1')
>>> info = socket.getaddrinfo(server, None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".pyenv/versions/3.4.3/lib/python3.4/socket.py", line 533, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

On a linux box this works fine, therefore it is not a big deal, but it would be nice to have the test working on mac too.

@lsbardel lsbardel closed this as completed Jan 6, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants