自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

兩種方法實(shí)現(xiàn) Http Request Body 多次讀取

開(kāi)發(fā) 前端
在 gin 中, 在讀取了 request body 后, 通過(guò) c.Set(BodyBytesKey, body) 放到了 gin.Context 中的 Keys。這是一個(gè) map, 上面說(shuō)到了。

大家好, 我是 老麥, 一個(gè)運(yùn)維老兵, 現(xiàn)在專注于 Golang,DevOps,云原生基礎(chǔ)設(shè)施建設(shè)。

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

最近在使用 gin 的時(shí)候, 踩了一個(gè)重復(fù)讀取的 Request.Body 的坑。

起因是 gin 的 gin.Context{} 提供了 c.Copy() 方法創(chuàng)建副本。這個(gè)方法一直在用, 但不知道從什么時(shí)候開(kāi)始, 一直認(rèn)為這個(gè)方法是 深拷貝, 但 并不完全是 (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, // 指針, 也算引用類型。 沒(méi)有實(shí)現(xiàn)完全復(fù)制
  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 完全復(fù)制
 for k, v := range c.Keys {
  cp.Keys[k] = v
 }
 paramCopy := make([]Param, len(cp.Params)) // 切片, 完全復(fù)制
 copy(paramCopy, cp.Params) 
 cp.Params = paramCopy
 return &cp
}

1. gin 通過(guò)用一個(gè)全局變量保存

在 gin 中, 在讀取了 request body 后, 通過(guò) c.Set(BodyBytesKey, body) 放到了 gin.Context 中的 Keys。這是一個(gè) map, 上面說(shuō)到了。

因此 在 gin 中通過(guò)中間變量實(shí)現(xiàn)類似效果。雖然感覺(jué)上多次讀取 Body , 但實(shí)際 只讀取了一次,

// 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 中的內(nèi)容放到 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. 再造一個(gè) Request

另外一種方法, 就是在讀取 Body 后, 重建一個(gè) Requset 再把 Body 放進(jìn)去。

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

// 創(chuàng)建新的
r2 := r.Clone(r.Context())

// 將數(shù)據(jù)方進(jìn)去
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 有一個(gè)方法叫 Clone(), 但這也不是一個(gè)完全的深拷貝。Body 沒(méi)有復(fù)制。

// 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
}
責(zé)任編輯:武曉燕 來(lái)源: 熊貓?jiān)圃鶪o
相關(guān)推薦

2009-09-25 14:04:09

Hibernate eHibernate h

2010-07-26 15:42:34

Perl模塊

2010-08-02 16:58:08

Flex配置文件

2010-08-03 13:53:47

Flex+Java配置

2009-08-05 15:54:49

Web Service

2010-08-04 17:41:52

掛載NFS

2009-11-03 16:20:16

VB.NET文本框

2009-04-21 11:23:56

Oraclespool比較

2010-05-28 10:35:46

SVN搭建測(cè)試服務(wù)器

2010-06-02 17:16:16

自動(dòng)運(yùn)行SVN

2009-11-06 09:48:40

WCF服務(wù)

2010-04-13 09:50:44

Oracle跟蹤

2010-11-24 14:36:25

修復(fù)mysql表

2011-03-30 17:04:24

MySQL添加用戶

2010-05-26 18:52:12

SVN庫(kù)

2010-06-17 12:48:05

livecd 修復(fù)Gr

2009-08-03 17:53:11

XML數(shù)據(jù)

2010-02-06 14:35:36

ibmdwRUP迭代

2009-06-18 11:09:42

2009-08-05 13:34:18

C#日期相減
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)