登录
首页 >  Golang >  Go问答

避免将未匹配的请求传递给http.handleFunc("/", func(w, r) { })操作

来源:stackoverflow

时间:2024-02-20 10:54:23 272浏览 收藏

从现在开始,努力学习吧!本文《避免将未匹配的请求传递给http.handleFunc("/", func(w, r) { })操作》主要讲解了等等相关知识点,我会在golang学习网中持续更新相关的系列文章,欢迎大家关注并积极留言建议。下面就先一起来看一下本篇正文内容吧,希望能帮到你!

问题内容

考虑下面非常基本的 http 服务器代码,通过使用参数调用 run()main() 函数中启动,其中参数用于指定根目录路径。

package main

import "fmt"
import "net/http"
import "strings"

func main() {
    // run("/")              // option 1
    // run("/newbase")       // option 2
}

func run(basepath string) {
    var path = fmt.sprintf("%s", basepath)
    if !strings.hasprefix(path, "/") {
        path = fmt.sprintf("/%s", path)
    }
    if !strings.hassuffix(path, "/") {
        path = fmt.sprintf("%s/", path)
    }

    http.handlefunc(fmt.sprintf("%sfoo", path), handlefoo)
    http.handlefunc(fmt.sprintf("%sbar", path), handlebar)
    http.handlefunc(fmt.sprintf("%s", path), handleroot)

    http.listenandserve(":60001", nil)
}

func handleroot(w http.responsewriter, r *http.request) {
    fmt.fprint(w, "root")
}
func handlefoo(w http.responsewriter, r *http.request) {
    fmt.fprint(w, "foo")
}
func handlebar(w http.responsewriter, r *http.request) {
    fmt.fprint(w, "bar")
}

如果调用 run("/"),服务器应响应 url //foo/bar

如果调用 run("/newbase"),服务器应响应 url /newbase//newbase/foo/newbase /栏

我希望任何其他 url 返回标准 http/404,但似乎为未处理的子路径调用了 / 处理程序。这会导致行为上出现奇怪的差异:

| for run("/")                    | for run("/newbase")
entered url       |---------------------------------+---------------------------------
                  | desired        | actual         | desired        | actual
------------------+----------------+----------------+----------------+----------------
 /                | http/200 root  | http/200 root  | http/404       | http/404
------------------+----------------+----------------+----------------+----------------
 /foo             | http/200 foo   | http/200 foo   | http/404       | http/404
------------------+----------------+----------------+----------------+----------------
 /foo/qux         | http/404       | http/200 root  | http/404       | http/404
------------------+----------------+----------------+----------------+----------------
 /bar             | http/200 bar   | http/200 bar   | http/404       | http/404
------------------+----------------+----------------+----------------+----------------
 /baz             | http/404       | http/200 root  | http/404       | http/404
------------------+----------------+----------------+----------------+----------------
 /newbase         | http/404       | http/200 root  | http/200 root  | http/200 root
------------------+----------------+----------------+----------------+----------------
 /newbase/foo     | http/404       | http/200 root  | http/200 foo   | http/200 foo
------------------+----------------+----------------+----------------+----------------
 /newbase/foo/qux | http/404       | http/200 root  | http/404       | http/200 root
------------------+----------------+----------------+----------------+----------------
 /newbase/bar     | http/404       | http/200 root  | http/200 bar   | http/200 bar
------------------+----------------+----------------+----------------+----------------
 /newbase/baz     | http/404       | http/200 root  | http/404       | http/200 root

我认为发生这种情况是因为 http.handlefunc 的第一个参数被视为最接近匹配的模式,而不是精确匹配。

是否有任何方法可以强制处理程序要求精确(最好不区分大小写)匹配,以便它不会为未注册 url 的 / 处理程序提供服务,而是使用默认 http 进行响应/404?

# go version
go version go1.14.9 linux/amd64

解决方案


http.ServeMux documentation 说:

模式命名固定的根路径,例如“/favicon.ico”,或根子树,例如“/images/”(注意尾部斜杠)。

请注意,由于以斜杠结尾的模式命名为根子树,因此模式“/”匹配与其他已注册模式不匹配的所有路径,而不仅仅是 path ==“/”的 url。

要处理 /(或以 / 结尾的任何其他模式)上的精确匹配,请向处理程序添加代码,以便在路径不等于该模式时响应 404。使用 http.NotFound 来响应“默认”404 响应。

http.handlefunc("/", func(w http.responsewriter, r *http.request) {
    if r.url.path != "/" {
        http.notfound(w, r)
        return
    }
    // code to handle / here
})

发生这种情况是因为 http.ServeMux 进行了基于前缀的匹配。

以斜杠结尾的模式命名为根子树,模式“/”匹配与其他已注册模式不匹配的所有路径,而不仅仅是 path ==“/”的 url。

因此,如果您想要基于精确路径的匹配,实现目标的一种方法是更改​​这部分

http.handlefunc(fmt.sprintf("%sfoo", path), handlefoo)
    http.handlefunc(fmt.sprintf("%sbar", path), handlebar)
    http.handlefunc(fmt.sprintf("%s", path), handleroot)

对此:

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){
        switch r.URL.Path {
        case fmt.Sprintf("%sfoo", path):
            handleFoo(w, r)
        case fmt.Sprintf("%sbar", path):
            handleBar(w, r)
        case fmt.Sprintf("%s", path):
            handleRoot(w, r)
        default:
            http.NotFoundHandler().ServeHTTP(w, r)
        }
    })

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《避免将未匹配的请求传递给http.handleFunc("/", func(w, r) { })操作》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>