Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
d56ac28b
Commit
d56ac28b
authored
Aug 10, 2016
by
Carter
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Using a LimitReader and fixed test and log format.
parent
3fd8218f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
16 deletions
+25
-16
caddyhttp/httpserver/replacer.go
caddyhttp/httpserver/replacer.go
+13
-8
caddyhttp/httpserver/replacer_test.go
caddyhttp/httpserver/replacer_test.go
+12
-8
No files found.
caddyhttp/httpserver/replacer.go
View file @
d56ac28b
...
...
@@ -2,8 +2,8 @@ package httpserver
import
(
"bytes"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/httputil"
...
...
@@ -127,13 +127,12 @@ func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Repla
return
""
}
body
,
err
:=
readRequestBody
(
r
)
body
,
err
:=
readRequestBody
(
r
,
maxLogBodySize
)
if
err
!=
nil
{
log
.
Printf
(
"[WARNING] Cannot copy request body %v"
,
err
)
return
""
}
return
string
(
body
)
return
requestReplacer
.
Replace
(
string
(
body
)
)
},
},
emptyValue
:
emptyValue
,
...
...
@@ -163,13 +162,18 @@ func canLogRequest(r *http.Request) (canLog bool) {
// readRequestBody reads the request body and sets a
// new io.ReadCloser that has not yet been read.
func
readRequestBody
(
r
*
http
.
Request
)
([]
byte
,
error
)
{
body
,
err
:=
ioutil
.
ReadAll
(
r
.
Body
)
func
readRequestBody
(
r
*
http
.
Request
,
n
int64
)
([]
byte
,
error
)
{
body
,
err
:=
ioutil
.
ReadAll
(
io
.
LimitReader
(
r
.
Body
,
n
)
)
if
err
!=
nil
{
return
nil
,
err
}
// Create a new ReadCloser to keep the body from being drained.
r
.
Body
=
ioutil
.
NopCloser
(
bytes
.
NewBuffer
(
body
))
mr
:=
io
.
MultiReader
(
bytes
.
NewBuffer
(
body
),
r
.
Body
,
)
r
.
Body
=
ioutil
.
NopCloser
(
mr
)
return
body
,
nil
}
...
...
@@ -272,4 +276,5 @@ const (
headerContentType
=
"Content-Type"
contentTypeJSON
=
"application/json"
contentTypeXML
=
"application/xml"
maxLogBodySize
=
100
*
1000
)
caddyhttp/httpserver/replacer_test.go
View file @
d56ac28b
...
...
@@ -2,6 +2,7 @@ package httpserver
import
(
"bytes"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
...
...
@@ -164,23 +165,26 @@ func TestRound(t *testing.T) {
}
func
TestReadRequestBody
(
t
*
testing
.
T
)
{
r
,
err
:=
http
.
NewRequest
(
"POST"
,
"/"
,
strings
.
NewReader
(
`null`
))
payload
:=
[]
byte
(
`{ "foo": "bar" }`
)
var
readSize
int64
=
5
r
,
err
:=
http
.
NewRequest
(
"POST"
,
"/"
,
bytes
.
NewReader
(
payload
))
if
err
!=
nil
{
t
.
Error
(
err
)
}
defer
r
.
Body
.
Close
()
body
,
err
:=
readRequestBody
(
r
)
logBody
,
err
:=
readRequestBody
(
r
,
readSize
)
if
err
!=
nil
{
t
.
Error
(
"readRequestBody failed"
,
err
)
}
else
if
!
bytes
.
EqualFold
(
payload
[
0
:
readSize
],
logBody
)
{
t
.
Error
(
"Expected log comparison failed"
)
}
var
data
=
make
([]
byte
,
len
(
body
))
_
,
err
=
r
.
Body
.
Read
(
data
)
// Ensure the Request body is the same as the original.
reqBody
,
err
:=
ioutil
.
ReadAll
(
r
.
Body
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
else
if
!
bytes
.
Equal
(
body
,
data
)
{
t
.
Error
(
"Exp
ceted equal bytes.
"
)
t
.
Error
(
"Unable to read request body"
,
err
)
}
else
if
!
bytes
.
Equal
Fold
(
payload
,
reqBody
)
{
t
.
Error
(
"Exp
ected request body comparison failed
"
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment