Skip to content

Commit

Permalink
feat(service): add a new filter to support setting maxMemory manually…
Browse files Browse the repository at this point in the history
… when parsing request form (#317)

Co-authored-by: iawia002 <xuxinzhao@caicloud.io>
  • Loading branch information
caicloud-bot and iawia002 committed Apr 30, 2020
1 parent 8a547eb commit a44d6d7
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions service/filter.go
Expand Up @@ -64,18 +64,18 @@ func FillLeadingSlash() Filter {
}
}

// ParseRequestForm returns a filter to parse request form when content
// ParseRequestFormWithMaxMemory returns a filter to parse request form when content
// type is "application/x-www-form-urlencoded" or "multipart/form-data".
// The filter won't filter anything unless some error occurs in parsing.
func ParseRequestForm() Filter {
func ParseRequestFormWithMaxMemory(maxMemory int64) Filter {
return func(resp http.ResponseWriter, req *http.Request) bool {
ct, err := ContentType(req)
if err == nil {
switch ct {
case definition.MIMEURLEncoded:
err = req.ParseForm()
case definition.MIMEFormData:
err = req.ParseMultipartForm(32 << 20)
err = req.ParseMultipartForm(maxMemory)
default:
req.Form = req.URL.Query()
}
Expand All @@ -88,6 +88,12 @@ func ParseRequestForm() Filter {
}
}

// ParseRequestForm returns a filter to parse request form.
// Same as ParseRequestFormWithMaxMemory, except that maxMemory is set to 32MB by default.
func ParseRequestForm() Filter {
return ParseRequestFormWithMaxMemory(32 << 20)
}

// isGTZero returns a boolean result indicating if the content length is greater than 0.
func isGTZero(length string) bool {
if length == "" {
Expand Down

0 comments on commit a44d6d7

Please sign in to comment.