在CherryPy中停止请求处理,并从工具返回200响应 [英] Stop request processing in CherryPy and return 200 response from a tool

查看:353
本文介绍了在CherryPy中停止请求处理,并从工具返回200响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题

我正在寻找一种方法来停止工具中的请求处理,而不会引发异常。换句话说:我想停止请求,因为它到达指定的控制器并返回2xx状态码?

I am looking for a way to stop request processing in a Tool without raising an exception. In other words: I want to stop the request befor it gets to the specified controller and return a 2xx status code?

背景 >

我们希望我们的应用程序支持CORS,因此支持预检请求。这个想法是写一个钩子 before_handler 的工具。如果发出了OPTIONS请求,返回相关的CORS头并退出。

Background

问题是,我没有找到一种方法来停止执行流程。这意味着处理原始网址,如果正常请求的话。重点是:这可能导致安全问题,因为总是进行预检请求。这是我到目前为止:

We want our application to support CORS and therefore the preflight request. The idea was to write a tool which hooks before_handler. If an OPTIONS request is made, return the relevant CORS-headers and exit.

The problem is, that I havn't found a way to stop the execution flow. This means that the original URL is processed, as it would be if requested normally. The point is: this could lead to security issues, as the preflight request is always made. This is what I have so far:

替代品可能无法正常工作

我知道有一个 cherrypy-cors 插件,但是从源代码我不知道这如何阻止执行。

I know that there is a cherrypy-cors plugin, but from the source I can't see how this stops the execution.

我也知道CherryPy有一个MethodDispatcher,但这将意味着我们的代码的完全重写。

I also know that CherryPy has a MethodDispatcher, but that would mean a complete rewrite of our code.

搜索Stackoverflow我发现了这< a> answer,但是我不想杀死执行,我只想要一种方法来防止处理程序被调用。

Searching Stackoverflow I found this answer, however I don't want to "kill" the execution, I just want a way to prevent the handler from being called.

推荐答案

只需让工具集request.handler = None。请参见请求对话代码实现此和 CachingTool 的示例:

Just have your Tool set request.handler = None. See Request.respond for the code which implements this and the CachingTool for an example:


    request = cherrypy.serving.request
    if _caching.get(**kwargs):
        request.handler = None


这篇关于在CherryPy中停止请求处理,并从工具返回200响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆