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

attribute headers of http.server.BaseHTTPRequestHandler sometimes does not exists #62429

Closed
joru mannequin opened this issue Jun 16, 2013 · 7 comments
Closed

attribute headers of http.server.BaseHTTPRequestHandler sometimes does not exists #62429

joru mannequin opened this issue Jun 16, 2013 · 7 comments
Labels
docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error

Comments

@joru
Copy link
Mannequin

joru mannequin commented Jun 16, 2013

BPO 18229
Nosy @birkenfeld
Files
  • documentation18229.patch: documentation concerning headers attribute in HTTPBaseRequestHandler
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2014-04-16.17:58:01.992>
    created_at = <Date 2013-06-16.09:59:21.473>
    labels = ['type-bug', 'docs']
    title = 'attribute headers of http.server.BaseHTTPRequestHandler sometimes does not exists'
    updated_at = <Date 2014-04-16.17:58:01.990>
    user = 'https://bugs.python.org/joru'

    bugs.python.org fields:

    activity = <Date 2014-04-16.17:58:01.990>
    actor = 'orsenthil'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2014-04-16.17:58:01.992>
    closer = 'orsenthil'
    components = ['Documentation']
    creation = <Date 2013-06-16.09:59:21.473>
    creator = 'joru'
    dependencies = []
    files = ['34884']
    hgrepos = []
    issue_num = 18229
    keywords = ['patch']
    message_count = 7.0
    messages = ['191264', '199065', '199077', '199083', '199084', '216349', '216523']
    nosy_count = 5.0
    nosy_names = ['georg.brandl', 'docs@python', 'joru', 'python-dev', 'math_foo']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue18229'
    versions = ['Python 3.4', 'Python 3.5']

    @joru
    Copy link
    Mannequin Author

    joru mannequin commented Jun 16, 2013

    it seems that problem is someone connecting to port 8080 with non-http client, could not find warning in documentation

    ---fragment of less access.log------------
    81.172.30.254 - - [16/Jun/2013 11:36:58] "^SBitTorrent protocol^@^@^@^@^@^X^@^Ej
    81.172.30.254 - - [16/Jun/2013 11:38:11] "^N<U+008E>^@f¸ãÄòQ;³x<U+0092>b^C^HÄA7
    81.172.30.254 - - [16/Jun/2013 11:39:22] "^SBitTorrent protocol^@^@^@^@^@^X^@^Ej
    81.172.30.254 - - [16/Jun/2013 11:40:35] "Ã<U+008D>¬0æz<U+0093>zr^D<U+009B>2]WQ

    Exception happened during processing of request from ('81.172.30.254', 63650)
    Traceback (most recent call last):
      File "c:\Python33\lib\socketserver.py", line 306, in _handle_request_noblock
        self.process_request(request, client_address)
      File "c:\Python33\lib\socketserver.py", line 332, in process_request
        self.finish_request(request, client_address)
      File "c:\Python33\lib\socketserver.py", line 345, in finish_request
        self.RequestHandlerClass(request, client_address, self)
      File "c:\Python33\lib\socketserver.py", line 666, in __init__
        self.handle()
      File "c:\Python33\lib\http\server.py", line 400, in handle
        self.handle_one_request()
      File "c:\Python33\lib\http\server.py", line 380, in handle_one_request
        if not self.parse_request():
      File "c:\Python33\lib\http\server.py", line 283, in parse_request
        self.send_error(400, "Bad request version (%r)" % version)
      File "c:\Python33\lib\http\server.py", line 428, in send_error
        self.send_response(code, message)
      File "c:\Python33\lib\http\server.py", line 443, in send_response
        self.log_request(code)
      File "c:\Users\joru\Dropbox\programowanie\demoniszcze\server\_lowerHTTP.py", line 30, in log_request
        xff=req.headers.get('X-Forwarded-For')
    AttributeError: '_HNDL_3' object has no attribute 'headers'
    # _HNLD_3 derives from http.server.BaseHTTPRequestHandler

    @joru joru mannequin assigned docspython Jun 16, 2013
    @joru joru mannequin added docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error labels Jun 16, 2013
    @birkenfeld
    Copy link
    Member

    This appears to be a bug in the _lowerHTTP module, which does not ship with Python.

    @joru
    Copy link
    Mannequin Author

    joru mannequin commented Oct 6, 2013

    what _lowerHTTP does is try read request header 'X-Forwarded-For', but instance of request handler have attribute headers only if thing that connected to port where server listens happened to send valid enough http request

    my problem is, documentation does not help write code that would not crash when client sends random bytes instead of expected http request

    @joru joru mannequin reopened this Oct 6, 2013
    @joru joru mannequin removed the invalid label Oct 6, 2013
    @joru
    Copy link
    Mannequin Author

    joru mannequin commented Oct 6, 2013

    #minimal server:
    #!/c/Python33/python.exe
    from http.server import HTTPServer as S, BaseHTTPRequestHandler as H
    class HNDL(H):
    	def log_request(req,code):
    		print('header is',req.headers.get('X-Forwarder-For'),', code',code)
    		H.log_request(req)
    s=S(('',54321),HNDL)
    s.serve_forever()
    
    
    #non-http client:
    #!/c/Python33/python.exe
    import socket,os
    s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(('localhost', 54321))
    s.sendall(os.urandom(1024))
    buf=s.recv(2048)
    s.close()
    print(buf)

    #running server:
    $ ./server.py
    127.0.0.1 - - [06/Oct/2013 17:33:41] code 400, message Bad HTTP/0.9 request type
    ('E)\xaeE^2¤\xf2W\x8f\xb3aG')
    ----------------------------------------

    Exception happened during processing of request from ('127.0.0.1', 18234)
    Traceback (most recent call last):
      File "c:\Python33\lib\socketserver.py", line 306, in _handle_request_noblock
        self.process_request(request, client_address)
      File "c:\Python33\lib\socketserver.py", line 332, in process_request
        self.finish_request(request, client_address)
      File "c:\Python33\lib\socketserver.py", line 345, in finish_request
        self.RequestHandlerClass(request, client_address, self)
      File "c:\Python33\lib\socketserver.py", line 666, in __init__
        self.handle()
      File "c:\Python33\lib\http\server.py", line 400, in handle
        self.handle_one_request()
      File "c:\Python33\lib\http\server.py", line 380, in handle_one_request
        if not self.parse_request():
      File "c:\Python33\lib\http\server.py", line 311, in parse_request
        "Bad HTTP/0.9 request type (%r)" % command)
      File "c:\Python33\lib\http\server.py", line 428, in send_error
        self.send_response(code, message)
      File "c:\Python33\lib\http\server.py", line 443, in send_response
        self.log_request(code)
      File "./server.py", line 5, in log_request
        print('header is',req.headers.get('X-Forwarder-For'),', code',code)
    AttributeError: 'HNDL' object has no attribute 'headers'

    #running client:
    $ ./client.py
    b''
    $

    @birkenfeld
    Copy link
    Member

    OK, so I guess it could be documented that the "headers" attribute is not set if the request cannot be parsed as a HTTP request. That's a valid point.

    By the way, you should really call your "self" argument "self".

    @mathfoo
    Copy link
    Mannequin

    mathfoo mannequin commented Apr 15, 2014

    Added comment to documentation concerning when the headers attribute gets set.

    I confirmed that the headers attribute is only ever set in the parse_request method of BaseHTTPRequestHandler and only if the request is successfully parsed as HTTP.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Apr 16, 2014

    New changeset 104fab0143e9 by Senthil Kumaran in branch '3.4':
    Address bpo-18229 - Explain http.server.BaseHTTPRequestHandler's .headers attribute further.
    http://hg.python.org/cpython/rev/104fab0143e9

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants