脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Golang - 两种方法实现 Http Request Body 多次读取

两种方法实现 Http Request Body 多次读取

2024-01-02 13:10熊猫云原生Go Golang

在 gin 中, 在读取了 request body 后, 通过 c.Set(BodyBytesKey, body) 放到了 gin.Context 中的 Keys。这是一个 map, 上面说到了。

大家好, 我是 老麦, 一个运维老兵, 现在专注于 Golang,DevOps,云原生基础设施建设。

原文链接: https://typonotes.com/posts/2024/01/02/http-request-multiple-times-read/

最近在使用 gin 的时候, 踩了一个重复读取的 Request.Body 的坑。

起因是 gin 的 gin.Context{} 提供了 c.Copy() 方法创建副本。这个方法一直在用, 但不知道从什么时候开始, 一直认为这个方法是 深拷贝, 但 并不完全是 (T_T)

// Copy returns a copy of the current context that can be safely used outside the request's scope.
// This has to be used when the context has to be passed to a goroutine.
func (c *Context) Copy() *Context {
 cp := Context{
  writermem: c.writermem,
  Request:   c.Request, // 指针, 也算引用类型。 没有实现完全复制
  Params:    c.Params,
  engine:    c.engine,
 }
 cp.writermem.ResponseWriter = nil
 cp.Writer = &cp.writermem
 cp.index = abortIndex
 cp.handlers = nil
 cp.Keys = map[string]interface{}{} // Keys 完全复制
 for k, v := range c.Keys {
  cp.Keys[k] = v
 }
 paramCopy := make([]Param, len(cp.Params)) // 切片, 完全复制
 copy(paramCopy, cp.Params) 
 cp.Params = paramCopy
 return &cp
}

1. gin 通过用一个全局变量保存

在 gin 中, 在读取了 request body 后, 通过 c.Set(BodyBytesKey, body) 放到了 gin.Context 中的 Keys。这是一个 map, 上面说到了。

因此 在 gin 中通过中间变量实现类似效果。虽然感觉上多次读取 Body , 但实际 只读取了一次,

// ShouldBindBodyWith is similar with ShouldBindWith, but it stores the request
// body into the context, and reuse when it is called again.
//
// NOTE: This method reads the body before binding. So you should use
// ShouldBindWith for better performance if you need to call only once.
func (c *Context) ShouldBindBodyWith(obj any, bb binding.BindingBody) (err error) {
 var body []byte
 if cb, ok := c.Get(BodyBytesKey); ok {
  if cbb, ok := cb.([]byte); ok {
   body = cbb
  }
 }
 if body == nil {
  body, err = io.ReadAll(c.Request.Body)
  if err != nil {
   return err
  }
  // 将 Body 中的内容放到 gin.Context 中的 Keys 中
  c.Set(BodyBytesKey, body)
 }
 return bb.BindBody(body, obj)
}

参考文档: https://github.com/gin-gonic/gin/blob/v1.9.1/context.go#L744-L764

2. 再造一个 Request

另外一种方法, 就是在读取 Body 后, 重建一个 Requset 再把 Body 放进去。

// 读取老的
body, err := ioutil.ReadAll(r.Body)
if err != nil {
    // ...
}
url, _ := url.Parse(config.GetGameHost())

// 创建新的
r2 := r.Clone(r.Context())

// 将数据方进去
r.Body = ioutil.NopCloser(bytes.NewReader(body))
r2.Body = ioutil.NopCloser(bytes.NewReader(body))

r.ParseForm()

proxy := httputil.NewSingleHostReverseProxy(url)
proxy.ServeHTTP(w, r2)

参考文档: https://stackoverflow.com/q/62017146

注意 http.Request 有一个方法叫 Clone(), 但这也不是一个完全的深拷贝。Body 没有复制。

// Clone returns a deep copy of r with its context changed to ctx.
// The provided ctx must be non-nil.
//
// For an outgoing client request, the context controls the entire
// lifetime of a request and its response: obtaining a connection,
// sending the request, and reading the response headers and body.
func (r *Request) Clone(ctx context.Context) *Request {
 if ctx == nil {
  panic("nil context")
 }
 r2 := new(Request)
 *r2 = *r
 r2.ctx = ctx
 r2.URL = cloneURL(r.URL)
 if r.Header != nil {
  r2.Header = r.Header.Clone()
 }
 if r.Trailer != nil {
  r2.Trailer = r.Trailer.Clone()
 }
 if s := r.TransferEncoding; s != nil {
  s2 := make([]string, len(s))
  copy(s2, s)
  r2.TransferEncoding = s2
 }
 r2.Form = cloneURLValues(r.Form)
 r2.PostForm = cloneURLValues(r.PostForm)
 r2.MultipartForm = cloneMultipartForm(r.MultipartForm)
 return r2
}

原文地址:https://mp.weixin.qq.com/s/5d9H8yMBOGZBYCEYaO7Q0A

延伸 · 阅读

精彩推荐
  • Golang使用Go语言实现远程传输文件

    使用Go语言实现远程传输文件

    本文主要介绍如何利用Go语言实现远程传输文件的功能,有需要的小伙伴们可以参考学习。下面跟着小编一起来学习学习。 ...

    daisy4762020-05-01
  • GolangGolang使用lua脚本实现redis原子操作

    Golang使用lua脚本实现redis原子操作

    这篇文章主要介绍了Golang使用lua脚本实现redis原子操作,本文通过实例代码给大家介绍的非常详细,对大家的工作或学习具有一定的参考借鉴价值,需要的朋...

    GrassInWind201910272020-06-06
  • Golang解决go语言ssh客户端密码过期问题

    解决go语言ssh客户端密码过期问题

    这篇文章主要介绍了go语言ssh客户端解决密码过期问题,本文给大家分享了解决的方法和原理,非常不错,对大家的学习或工作具有一定的参考借鉴价值,需...

    cuidi5072020-06-08
  • GolangGo简单实现协程方法

    Go简单实现协程方法

    本文主要介绍了Go简单实现协程的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着...

    Onemorelight9511062022-12-15
  • GolangGo处理json数据方法详解(Marshal,UnMarshal)

    Go处理json数据方法详解(Marshal,UnMarshal)

    这篇文章主要介绍了Go处理json数据的方法详解,Marshal(),UnMarshal(),需要的朋友可以参考下...

    骏马金龙5092022-09-23
  • Golanggolang中为什么不存在三元运算符详解

    golang中为什么不存在三元运算符详解

    这篇文章主要给大家介绍了关于golang中为什么不存在三元运算符的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学...

    apocelipes8602020-09-05
  • GolangGo语言中struct的匿名属性特征实例分析

    Go语言中struct的匿名属性特征实例分析

    这篇文章主要介绍了Go语言中struct的匿名属性特征,实例分析了struct的匿名属性特征,对于深入学习Go语言程序设计具有一定参考借鉴价值,需要的朋友可以参考...

    脚本之家5872020-04-13
  • GolangGo 并发控制context实现原理剖析(小结)

    Go 并发控制context实现原理剖析(小结)

    Golang context是Golang应用开发常用的并发控制技术,这篇文章主要介绍了Go 并发控制context实现原理剖析(小结),具有一定的参考价值,感兴趣的小伙伴们可以...

    恋恋美食1802020-05-21