Skip to content

Commit

Permalink
refactor: renove unuse check for body parser
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Nov 27, 2020
1 parent c1861ee commit 17d2b15
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func GetClientIP(req *http.Request) string {
return v
}
}
// 如果所有IP都是非内网IP,则直接取第一个
// 如果所有IP都是内网IP,则直接取第一个
if len(arr) != 0 {
return strings.TrimSpace(arr[0])
}
Expand Down
37 changes: 16 additions & 21 deletions middleware/body_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,29 +290,24 @@ func NewBodyParser(config BodyParserConfig) elton.Handler {
if !valid {
return c.Next()
}
// 如果request body为空,则表示未读取数据
if c.RequestBody == nil {
r := c.Request.Body
if limit > 0 {
r = MaxBytesReader(r, int64(limit))
}
defer r.Close()
body, e := ioutil.ReadAll(r)
if e != nil {
// IO 读取失败的认为是 exception
err = &hes.Error{
Exception: true,
StatusCode: http.StatusInternalServerError,
Message: e.Error(),
Category: ErrBodyParserCategory,
Err: e,
}
return
r := c.Request.Body
if limit > 0 {
r = MaxBytesReader(r, int64(limit))
}
defer r.Close()
body, e := ioutil.ReadAll(r)
if e != nil {
// IO 读取失败的认为是 exception
err = &hes.Error{
Exception: true,
StatusCode: http.StatusInternalServerError,
Message: e.Error(),
Category: ErrBodyParserCategory,
Err: e,
}
c.Request.Body.Close()
c.RequestBody = body
return
}
body := c.RequestBody
c.RequestBody = body

matchDecoders := make([]BodyDecoder, 0)
for _, decoder := range config.Decoders {
Expand Down
3 changes: 1 addition & 2 deletions middleware/concurrent_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ func NewConcurrentLimiter(config ConcurrentLimiterConfig) elton.Handler {
} else if key.Header {
v = c.GetRequestHeader(name)
} else if key.Query {
query := c.Query()
v = query[name]
v = c.QueryParam(name)
} else if key.Params {
v = c.Param(name)
} else {
Expand Down

0 comments on commit 17d2b15

Please sign in to comment.