Handling HTTP/1.1 Upgrade requests in CherryPy

泄露秘密 提交于 2019-12-12 13:10:44

问题


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

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

I'd like the server to hand the connection off to some NEW_PROTOCOL handler after responding with the necessary HTTP/1.1 101 Switching Protocols..., as specified in RFC 2817.

I'm pretty new to CherryPy, and couldn't find anything in the documentation on how to handle specific client requests such as the above. If someone could point me to a tutorial or parts of the CherryPy documentation or even a solution, that would be very helpful.


回答1:


This is fairly easy to do in trunk (which will eventually be 3.2 final). I'm sure it's possible in older versions but much more convoluted.

All you need to do is make a new subclass of wsgiserver.Gateway that looks for the headers in question and then either hands off the conn or proceeds to the usual gateway. For example:

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's 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

There may be a couple other fine points but that's the general technique.



来源:https://stackoverflow.com/questions/3859823/handling-http-1-1-upgrade-requests-in-cherrypy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!