避免将未匹配的请求传递给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学习网公众号了解相关技术文章。
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习