在CherryPy中处理HTTP/1.1升级请求 [英] Handling HTTP/1.1 Upgrade requests in CherryPy
问题描述
我正在将CherryPy用于Web服务器,但希望它处理 HTTP/1.1 Upgrade
请求.因此,当客户发送时:
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
我希望服务器在响应必要的 HTTP/1.1 101交换协议...
后,将连接切换到某些 NEW_PROTOCOL
处理程序. RFC 2817 .
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.
我对CherryPy还是很陌生,在文档中找不到有关如何处理上述特定客户请求的任何信息.如果有人可以指出我的教程或CherryPy文档的一部分甚至是解决方案,那将非常有帮助.
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.
推荐答案
这在主干中非常容易做到(最终将是3.2最终版).我敢肯定,在较旧的版本中是可能的,但是更加令人费解.
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.
所有您需要做的是创建一个 wsgiserver.Gateway
的新子类,该子类查找有问题的标头,然后放开conn或继续使用常规网关.例如:
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.
这篇关于在CherryPy中处理HTTP/1.1升级请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!