Processing HTTP / 1.1 update requests in CherryPy

I am using CherryPy for a web server, but would like to handle requests HTTP/1.1 Upgrade. Thus, when the client sends:

OPTIONS * HTTP/1.1
Upgrade: NEW_PROTOCOL/1.0
Connection: Upgrade

I want the server to pass the connection to some handler NEW_PROTOCOLafter responding with the necessary HTTP/1.1 101 Switching Protocols..., as specified in RFC 2817 .

I am new to CherryPy and cannot find anything in the documentation on how to handle specific client requests like the one above. If someone can point me to a tutorial or parts of the CherryPy documentation, or even a solution, this will be very helpful.

+3
source share
1 answer

( 3,2 ). , , .

, , wsgiserver.Gateway, , , , . :

class UpgradeGateway(Gateway):
    def respond(self):
        h = self.req.inheaders
        if h.get("Connection", "") == "Upgrade":
            # Turn off auto-output of HTTP response headers
            self.req.sent_headers = True
            # Not sure exactly what you want to pass or how, here a start...
            return protocols[h['Upgrade']].handle(self.req.rfile, self.req.wfile)
        else:
            return old_gateway(self.req).respond()

old_gateway = cherrypy.server.httpserver.gateway
cherrypy.server.httpserver.gateway = UpgradeGateway

, .

+2

Source: https://habr.com/ru/post/1767976/


All Articles