首页 > 解决方案 > 如何破解马提尼中间件

问题描述

简而言之,如果我为 an 运行一些中间件http.Request并确定该请求应该得到 HTTP 422,我如何才能“突破”中间件链,并在不调用链中所有中间件函数的情况下提前返回?

例如,如果我有这个:

func Routes(m *martini.ClassicMartini) {

    m.Get("/cp/users", mw.AsJson, mw.RequestTimer, ctr.GetMany)

}

如果我调用return上述任何中间件资金,据我所知,它仍然会调用链中注册的所有中间件资金,因此ctr.GetMany总是会调用它。

有没有办法让请求/响应完成并告诉马提尼停止调用链中的所有函数?

如果第一个返回值是一个整数,我猜 martini 假设它是一个状态码。我目前最好的猜测是根据文档: https ://github.com/go-martini/martini#middleware-handlers

我们可以使用这个:

m.Use(func(c martini.Context, w http.ResponseWriter){

    if reqIsMalformed() {
        http.Error(w, "Bad request because xyz", 422)
        return;
    }

    c.Next()

})

如果不满足条件,我们就永远不能打电话c.Next()

标签: gomartini

解决方案


我用路由器/中间件做了一个实验,结果如下(有用的信息在最后):

func check0() {
    return
}

func check01() int {
    return 200
}

func check02() (int, string) {
    return 200, "boop"
}

func check03() bool {
    return true
}

func check04() string {
    return "04"
}

func check1(res http.ResponseWriter) string {
    return "1"
}

func check2(c martini.Context, res http.ResponseWriter) string {
    if true {
        return "hiii"
    }
    c.Next()
    return "2"
}

func check3(c martini.Context, res http.ResponseWriter) string {
    c.Next()
    return "3"
}

func check4(res http.ResponseWriter) {
    res.Write([]byte("4"))
}

func check5(c martini.Context, res http.ResponseWriter) (int, string, string) {
    res.Write([]byte("5.0"))
    c.Next()
    return 200, "5.1x", "5.1y"
}

func finish(res http.ResponseWriter) {
    fmt.Println("in finish")
    res.Write([]byte("all done"))
}

func Routes(m *martini.ClassicMartini) {
    m.Get("/cp/meta/test/middleware0", check0, finish)
    m.Get("/cp/meta/test/middleware01", check01, finish)
    m.Get("/cp/meta/test/middleware02", check02, finish)
    m.Get("/cp/meta/test/middleware03", check03, finish)
    m.Get("/cp/meta/test/middleware04", check04, finish)
    m.Get("/cp/meta/test/middleware1", check1, finish)
    m.Get("/cp/meta/test/middleware2", check2, finish)
    m.Get("/cp/meta/test/middleware3", check3, finish)
    m.Get("/cp/meta/test/middleware4", check4, finish)
    m.Get("/cp/meta/test/middleware5", check5, finish)
    m.Get("/cp/meta/echo_runtime_config", common.AsJson, common.RequestTimer, mw.BodyToMap, ctr.GetRuntimeConfig)
}

这是我点击 api 时的结果:

GET /cp/meta/test/middleware0 => 'all done'
GET /cp/meta/test/middleware01 => ''
GET /cp/meta/test/middleware03 => '<bool Value>'
GET /cp/meta/test/middleware02 => 'boop'
GET /cp/meta/test/middleware1 => '1'
GET /cp/meta/test/middleware04 => '04'
GET /cp/meta/test/middleware2 => 'hiii'
GET /cp/meta/test/middleware3 => 'all done3'
GET /cp/meta/test/middleware4 => '4'
GET /cp/meta/test/middleware5 => '5.0all done5.1x'

本来是要添加到这个问题上的。所以这里是规则:

  1. 如果中间件函数返回任何内容(又名 func 具有非 void 返回签名),则不会调用后续中间件。
  2. 注入各种参数似乎对是否调用后续中间件(包括 martini.Context 等)没有影响。
  3. 使用 martini.Context.Next() 似乎只对在调用所有其他剩余中间件后运行挂钩有用。
  4. 如果没有返回任何内容,将调用剩余的中间件,您显然不需要调用 c.Next()。
  5. 如果你返回一个 int 作为返回列表中的第一个参数,它将被解释为一个 http 状态码,第二个参数,如果它是任何东西,将被写入正文。如果第一个参数是一个字符串,而不是一个 int,那么它将被写入正文。我不确定是否使用或忽略了第三个参数,但它们似乎被忽略了。

推荐阅读