Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Go Httpbin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Jira
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Container registry
Monitor
Service Desk
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Nix
Go Httpbin
Commits
9e4e1b6a
Commit
9e4e1b6a
authored
7 years ago
by
Will McCutchen
Browse files
Options
Downloads
Patches
Plain Diff
Use middleware to limit request body size
parent
f6cde8d9
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
httpbin/handlers.go
+1
-1
1 addition, 1 deletion
httpbin/handlers.go
httpbin/helpers.go
+9
-6
9 additions, 6 deletions
httpbin/helpers.go
httpbin/httpbin.go
+7
-1
7 additions, 1 deletion
httpbin/httpbin.go
httpbin/middleware.go
+9
-0
9 additions, 0 deletions
httpbin/middleware.go
with
26 additions
and
8 deletions
httpbin/handlers.go
+
1
−
1
View file @
9e4e1b6a
...
@@ -63,7 +63,7 @@ func (h *HTTPBin) RequestWithBody(w http.ResponseWriter, r *http.Request) {
...
@@ -63,7 +63,7 @@ func (h *HTTPBin) RequestWithBody(w http.ResponseWriter, r *http.Request) {
URL
:
getURL
(
r
)
.
String
(),
URL
:
getURL
(
r
)
.
String
(),
}
}
err
:=
parseBody
(
w
,
r
,
resp
,
h
.
options
.
MaxMemory
)
err
:=
parseBody
(
w
,
r
,
resp
)
if
err
!=
nil
{
if
err
!=
nil
{
http
.
Error
(
w
,
fmt
.
Sprintf
(
"error parsing request body: %s"
,
err
),
http
.
StatusBadRequest
)
http
.
Error
(
w
,
fmt
.
Sprintf
(
"error parsing request body: %s"
,
err
),
http
.
StatusBadRequest
)
return
return
...
...
This diff is collapsed.
Click to expand it.
httpbin/helpers.go
+
9
−
6
View file @
9e4e1b6a
...
@@ -70,14 +70,14 @@ func writeHTML(w http.ResponseWriter, body []byte, status int) {
...
@@ -70,14 +70,14 @@ func writeHTML(w http.ResponseWriter, body []byte, status int) {
// parseBody handles parsing a request body into our standard API response,
// parseBody handles parsing a request body into our standard API response,
// taking care to only consume the request body once based on the Content-Type
// taking care to only consume the request body once based on the Content-Type
// of the request. The given Resp will be updated.
// of the request. The given bodyResponse will be modified.
func
parseBody
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
resp
*
bodyResponse
,
maxMemory
int64
)
error
{
//
// Note: this function expects callers to limit the the maximum size of the
// request body. See, e.g., the limitRequestSize middleware.
func
parseBody
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
resp
*
bodyResponse
)
error
{
if
r
.
Body
==
nil
{
if
r
.
Body
==
nil
{
return
nil
return
nil
}
}
// Restrict size of request body
r
.
Body
=
http
.
MaxBytesReader
(
w
,
r
.
Body
,
maxMemory
)
defer
r
.
Body
.
Close
()
defer
r
.
Body
.
Close
()
ct
:=
r
.
Header
.
Get
(
"Content-Type"
)
ct
:=
r
.
Header
.
Get
(
"Content-Type"
)
...
@@ -89,7 +89,10 @@ func parseBody(w http.ResponseWriter, r *http.Request, resp *bodyResponse, maxMe
...
@@ -89,7 +89,10 @@ func parseBody(w http.ResponseWriter, r *http.Request, resp *bodyResponse, maxMe
}
}
resp
.
Form
=
r
.
PostForm
resp
.
Form
=
r
.
PostForm
case
strings
.
HasPrefix
(
ct
,
"multipart/form-data"
)
:
case
strings
.
HasPrefix
(
ct
,
"multipart/form-data"
)
:
err
:=
r
.
ParseMultipartForm
(
maxMemory
)
// The memory limit here only restricts how many parts will be kept in
// memory before overflowing to disk:
// http://localhost:8080/pkg/net/http/#Request.ParseMultipartForm
err
:=
r
.
ParseMultipartForm
(
1024
*
1024
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
...
...
This diff is collapsed.
Click to expand it.
httpbin/httpbin.go
+
7
−
1
View file @
9e4e1b6a
...
@@ -157,7 +157,13 @@ func (h *HTTPBin) Handler() http.Handler {
...
@@ -157,7 +157,13 @@ func (h *HTTPBin) Handler() http.Handler {
mux
.
HandleFunc
(
"/stream-bytes"
,
http
.
NotFound
)
mux
.
HandleFunc
(
"/stream-bytes"
,
http
.
NotFound
)
mux
.
HandleFunc
(
"/links"
,
http
.
NotFound
)
mux
.
HandleFunc
(
"/links"
,
http
.
NotFound
)
return
logger
(
cors
(
mux
))
// Apply global middleware
var
handler
http
.
Handler
handler
=
mux
handler
=
limitRequestSize
(
h
.
options
.
MaxMemory
,
handler
)
handler
=
logger
(
handler
)
handler
=
cors
(
handler
)
return
handler
}
}
// NewHTTPBin creates a new HTTPBin
// NewHTTPBin creates a new HTTPBin
...
...
This diff is collapsed.
Click to expand it.
httpbin/middleware.go
+
9
−
0
View file @
9e4e1b6a
...
@@ -47,3 +47,12 @@ func methods(h http.HandlerFunc, methods ...string) http.HandlerFunc {
...
@@ -47,3 +47,12 @@ func methods(h http.HandlerFunc, methods ...string) http.HandlerFunc {
h
.
ServeHTTP
(
w
,
r
)
h
.
ServeHTTP
(
w
,
r
)
}
}
}
}
func
limitRequestSize
(
maxSize
int64
,
h
http
.
Handler
)
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
if
r
.
Body
!=
nil
{
r
.
Body
=
http
.
MaxBytesReader
(
w
,
r
.
Body
,
maxSize
)
}
h
.
ServeHTTP
(
w
,
r
)
})
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment