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

"on_end_request" hook is getting called before the response header is sent #1404

Closed
bb-migration opened this Issue Jan 29, 2016 · 2 comments

Comments

2 participants
@bb-migration

bb-migration commented Jan 29, 2016

Originally reported by: ronin8600 (Bitbucket: ronin8600, GitHub: ronin8600)


While debugging an issue with ws4py calling the connected callback before response headers were sent to the client I noticed a possible problem in when CherryPy calls the "on_end_request " hook. It appears that CherryPy is calling the "on_end_request" before it sends the response headers. I made a quick changed to delay calling close on the response in the gateway till after the headers are sent and it looks like it works for my ws4py test case.

cherrypy\wsgiserver\wsgiserver3.py

#!python

def respond(self):
        """Call the gateway and write its iterable output."""
        mrbs = self.server.max_request_body_size
        if self.chunked_read:
            self.rfile = ChunkedRFile(self.conn.rfile, mrbs)
        else:
            cl = int(self.inheaders.get(b"Content-Length", 0))
            if mrbs and mrbs < cl:
                if not self.sent_headers:
                    self.simple_response(
                        "413 Request Entity Too Large",
                        "The entity sent with the request exceeds the "
                        "maximum allowed bytes.")
                return
            self.rfile = KnownLengthRFile(self.conn.rfile, cl)

        # This will cause the "on_end_request" hook to be called before the headers are sent below
        self.server.gateway(self).respond()

        if (self.ready and not self.sent_headers):
            self.sent_headers = True
            self.send_headers()
        if self.chunked_write:
            self.conn.wfile.write(b"0\r\n\r\n")

@bb-migration

This comment has been minimized.

bb-migration commented Jan 30, 2016

Original comment by ronin8600 (Bitbucket: ronin8600, GitHub: ronin8600):


Proposed changes in pull request (https://bitbucket.org/cherrypy/cherrypy/pull-requests/121/fixing-issue-1404-on_end_request-hook-is/diff)

@webknjaz

This comment has been minimized.

Member

webknjaz commented Feb 9, 2018

/cc: @moigagoo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment