Go标准库-ServeMux的使用与模式匹配深入探究

 更新时间:2024年01月15日 10:53:09   作者:凉凉的知识库  
这篇文章主要为大家介绍了Go标准库-ServeMux的使用与模式匹配深入探究,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

引言

本篇为【深入理解Go标准库】系列第二篇

第一篇:http server的启动

第二篇:ServeMux的使用与模式匹配👈

根据 Golang 文档 中的介绍,ServeMux是一个 HTTP 请求多路复用器(HTTP Request multiplexer)。它按照一定规则匹配请求URL和已注册的模式,并执行其中最匹配的模式的Handler

如果你还不知道什么是Handler,强烈建议你先阅读下:第一篇:http server的启动

基本使用

http.ServeMux实现了Handler接口

type Handler interface {
 ServeHTTP(ResponseWriter, *Request)
}

http.ServeMux提供两个函数用于注册不同Path的处理函数

  • ServeMux.Handle 接收的是Handler接口实现

  • ServeMux.HandleFunc 接收的是匿名函数

type PathBar struct {
}

func (m PathBar) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 w.Write([]byte("Receive path bar"))
 return
}

func main() {
 mx := http.NewServeMux()

 mx.Handle("/bar/", PathBar{})
 mx.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
  w.Write([]byte("Receive path foo"))
 })

 http.ListenAndServe(":8009", mx)
}

🌲 通过类型转换实现接口

值得一提的是ServeMux.HandleFunc的实现,底层还是调用了ServeMux.Handle

func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
 if handler == nil {
  panic("http: nil handler")
 }
 mux.Handle(pattern, HandlerFunc(handler))
}

HandlerFunc(handler)这里并不是函数调用,而是类型转换

type HandlerFunc func(ResponseWriter, *Request)

// ServeHTTP calls f(w, r).
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
 f(w, r)
}

通过把handler func(ResponseWriter, *Request)转换成类型HandlerFunc,而类型HandlerFunc实现了Handler接口

🌲 全局默认值

当没有设置http.Server.Handler属性时,http.Server就会使用一个全局的变量DefaultServeMux *ServeMux来作为http.Server.Handler的值

下面的代码和上面的没有区别

func main() {
 http.Handle("/bar/", PathBar{})
 http.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
  w.Write([]byte("Receive path foo"))
 })

 http.ListenAndServe(":8009", nil)
}

Pattern匹配

预处理

预处理的是请求的url,以方便匹配,在注册时是不会做任何处理的

  • 移除host中的端口号

  • 针对 URL 中包含..或者.的请求,ServeMux 会对其 Path 进行整理,并匹配到合适的路由模式上

  • 针对 URL 中包含重复/的请求,ServeMux 会对其进行重定向

func main() {
 mx := http.NewServeMux()

 mx.HandleFunc("/abc/def", func(writer http.ResponseWriter, request *http.Request) {
  fmt.Fprintln(writer, request.Host, request.URL.Path)
 })

 http.ListenAndServe(":8009", mx)
}

🌲 预处理的是请求的url

pattern是不会被处理的,而请求的url都是被处理成标准格式

所以如果注册如下的pattern,无论如何也是无法被命中的

func main() {
 mx := http.NewServeMux()

 mx.HandleFunc("/abc//def", func(writer http.ResponseWriter, request *http.Request) {
  fmt.Fprintln(writer, request.Host, request.URL.Path)
 })
}

无论是/abc/def还是/abc//def都无法被命中

$ curl 127.0.0.1:8009/abc/def
404 page not found
$ curl 127.0.0.1:8009/abc//def
<a href="/abc/def" rel="external nofollow"  rel="external nofollow"     >Moved Permanently</a>.

🌲 带 ..或者.请求与重复/请求的处理不同

包含..或者.整理之后匹配到合适的路由模式上,并不会重定向

$ curl  127.0.0.1:8009/ccc/../abc/./def
127.0.0.1:8009 /abc/def

含重复/,会重定向

$ curl -v  127.0.0.1:8009/abc//def
*   Trying 127.0.0.1:8009...
* Connected to 127.0.0.1 (127.0.0.1) port 8009 (#0)
> GET /abc//def HTTP/1.1
> Host: 127.0.0.1:8009
> User-Agent: curl/7.79.1
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< Content-Type: text/html; charset=utf-8
< Location: /abc/def
< Date: Thu, 10 Nov 2022 16:05:13 GMT
< Content-Length: 43
< 
<a href="/abc/def" rel="external nofollow"  rel="external nofollow"     >Moved Permanently</a>.

* Connection #0 to host 127.0.0.1 left intact

路径匹配

ServeMux 注册路由模式的方式有两种,固定根路径例如"/favicon.ico",与以根路径开始的子树,例如"/images/"

🌲 固定路径(fixed, rooted paths)

固定根路径就是指定一个固定的 URL 和请求进行精确匹配

🌲 以根路径开始的子树(rooted subtrees)

以根路径开始的子树是符合最长路径匹配的原则的,例如我们注册了两个子路径,/image/gif//image/,URL 为/image/gif/的请求会优先匹配第一个路由模式,其他路径会匹配/image/

⚠️ 注意:

1、凡是/结尾的路径都被看作以根路径开始的子树,因此 / 也被看作以根路径开始的子树,它不仅匹配/,而且也会匹配所有未被其他路由模式匹配的请求。

func main() {
 mx := http.NewServeMux()

 mx.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
  fmt.Fprintln(writer, request.URL.EscapedPath())
 })

 http.ListenAndServe(":8009", mx)
}
$ curl 127.0.0.1:8009/abc
/abc

2、如果只注册了一个子树路径(/结尾)并且请求URL没有/结尾,ServeMux会返回重定向。如果再增加一个没有/结尾的模式的话,就会精确匹配,也就不会有这种行为了

例如我们只注册了子路径/abc/,服务器会自动将/abc请求重定向为/abc/

func main() {
 mx := http.NewServeMux()
 mx.HandleFunc("/abc/", func(writer http.ResponseWriter, request *http.Request) {
  fmt.Fprintln(writer, request.URL.EscapedPath())
 })
 http.ListenAndServe(":8009", mx)
}
$ curl -v 127.0.0.1:8009/abc
*   Trying 127.0.0.1:8009...
* Connected to 127.0.0.1 (127.0.0.1) port 8009 (#0)
> GET /abc HTTP/1.1
> Host: 127.0.0.1:8009
> User-Agent: curl/7.79.1
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< Content-Type: text/html; charset=utf-8
< Location: /abc/
< Date: Thu, 10 Nov 2022 15:30:13 GMT
< Content-Length: 40
< 
<a href="/abc/" rel="external nofollow"   >Moved Permanently</a>.

* Connection #0 to host 127.0.0.1 left intact

如果我们不想让服务器自动重定向的话,只需要再添加一个/abc模式就好了

func main() {
 mx := http.NewServeMux()

 mx.HandleFunc("/abc/", func(writer http.ResponseWriter, request *http.Request) {
  fmt.Fprintln(writer, request.URL.EscapedPath())
 })

 mx.HandleFunc("/abc", func(writer http.ResponseWriter, request *http.Request) {
  fmt.Fprintln(writer, request.URL.EscapedPath())
 })

 http.ListenAndServe(":8009", mx)
}
$ curl  127.0.0.1:8009/abc 
/abc

域名匹配(Host-specific patterns)

ServeMux 还支持根据主机名精确匹配,匹配时会严格匹配host,path的匹配则还遵循上面的原则

⚠️ 注意:

有域名的优先级会更高,所以可以注册一个带域名的路径和不带域名的路径

func main() {
 mx := http.NewServeMux()

 mx.HandleFunc("example01.com/abc/",
  func(writer http.ResponseWriter, request *http.Request) {
   fmt.Fprintln(writer, request.Host, request.URL.EscapedPath())
  })

 mx.HandleFunc("/abc/", func(writer http.ResponseWriter, request *http.Request) {
  fmt.Fprintln(writer, request.Host, request.URL.EscapedPath())
 })

 http.ListenAndServe(":8009", mx)
}

example01.com会匹配第一个handler,其他域名则匹配第二个

$ curl -H 'HOST:example01.com'  127.0.0.1:8009/abc/
example01.com /abc/

$ curl -H 'HOST:example02.com'  127.0.0.1:8009/abc 
example02.com /abc

Method和路径参数匹配(method, path specificity patterns)

最新的特性还在讨论中,大致的patterns会像下面这样

https://github.com/golang/go/discussions/60227 

/item/
POST /item/{user}
/item/{user}
/item/{user}/{id}
/item/{$}
POST alt.com/item/{user}

以上就是Go标准库-ServeMux的使用与模式匹配深入探究的详细内容,更多关于Go ServeMux模式匹配的资料请关注脚本之家其它相关文章!

相关文章

  • golang进行简单权限认证的实现

    golang进行简单权限认证的实现

    本文主要介绍了golang简单权限认证的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • go sync Waitgroup数据结构实现基本操作详解

    go sync Waitgroup数据结构实现基本操作详解

    这篇文章主要为大家介绍了go sync Waitgroup数据结构实现基本操作详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • Ubuntu18.04 LTS搭建GO语言开发环境过程解析

    Ubuntu18.04 LTS搭建GO语言开发环境过程解析

    这篇文章主要介绍了Ubuntu18.04 LTS搭建GO语言开发环境过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11
  • 从零封装Gin框架及项目初始化教程

    从零封装Gin框架及项目初始化教程

    这篇文章主要为大家介绍了从零封装Gin框架及项目的初始化教程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2024-01-01
  • Golang结构化日志包log/slog的使用详解

    Golang结构化日志包log/slog的使用详解

    官方提供的用于打印日志的包是标准库中的 log 包,该包虽然被广泛使用,但是缺点也很多,所以Go 1.21新增的 log/slog 完美解决了以上问题,下面我们就来看看log/slog包的使用吧
    2023-09-09
  • 详解Go sync 同步原语

    详解Go sync 同步原语

    Go 中不仅有 channel 这种 CSP 同步机制,还有 sync.Mutex、sync.WaitGroup 等比较原始的同步原语,使用它们,可以更灵活的控制数据同步和多协程并发,这篇文章主要介绍了Go sync 同步原语,需要的朋友可以参考下
    2023-12-12
  • go语言入门环境搭建及GoLand安装教程详解

    go语言入门环境搭建及GoLand安装教程详解

    这篇文章主要介绍了go语言入门环境搭建及GoLand安装教程详解,需要的朋友可以参考下
    2020-12-12
  • Go语言原子操作atomic的使用

    Go语言原子操作atomic的使用

    本文介绍了Go语言原子操作的使用方法,原子操作是一种无锁的技术,可通过CPU指令实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-10-10
  • go语言反射的基础教程示例

    go语言反射的基础教程示例

    这篇文章主要为大家介绍了go语言反射的基础教程,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • omitempty在go中的使用方式

    omitempty在go中的使用方式

    在Go语言编程中,`omitempty`标记用于JSON编解码过程中控制字段是否被包含,当结构体字段标记为`omitempty`且字段值为空时,该字段不会出现在生成的JSON中,有助于优化JSON结构和减小数据体积,通过具体示例解释了`omitempty`的工作机制和实际效果
    2024-09-09

最新评论