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
Łukasz Nowak
caddy
Commits
3fd8218f
Commit
3fd8218f
authored
Aug 10, 2016
by
Carter
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor and added test
parent
d06c15ca
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
5 deletions
+37
-5
caddyhttp/httpserver/replacer.go
caddyhttp/httpserver/replacer.go
+14
-5
caddyhttp/httpserver/replacer_test.go
caddyhttp/httpserver/replacer_test.go
+23
-0
No files found.
caddyhttp/httpserver/replacer.go
View file @
3fd8218f
...
@@ -127,12 +127,9 @@ func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Repla
...
@@ -127,12 +127,9 @@ func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Repla
return
""
return
""
}
}
body
,
err
:=
ioutil
.
ReadAll
(
r
.
Body
)
body
,
err
:=
readRequestBody
(
r
)
// Create a new ReadCloser to keep the body from being drained.
r
.
Body
=
ioutil
.
NopCloser
(
bytes
.
NewBuffer
(
body
))
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Printf
(
"[WARNING] Cannot
read
request body %v"
,
err
)
log
.
Printf
(
"[WARNING] Cannot
copy
request body %v"
,
err
)
return
""
return
""
}
}
...
@@ -164,6 +161,18 @@ func canLogRequest(r *http.Request) (canLog bool) {
...
@@ -164,6 +161,18 @@ func canLogRequest(r *http.Request) (canLog bool) {
return
return
}
}
// 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
)
if
err
!=
nil
{
return
nil
,
err
}
// Create a new ReadCloser to keep the body from being drained.
r
.
Body
=
ioutil
.
NopCloser
(
bytes
.
NewBuffer
(
body
))
return
body
,
nil
}
// Replace performs a replacement of values on s and returns
// Replace performs a replacement of values on s and returns
// the string with the replaced values.
// the string with the replaced values.
func
(
r
*
replacer
)
Replace
(
s
string
)
string
{
func
(
r
*
replacer
)
Replace
(
s
string
)
string
{
...
...
caddyhttp/httpserver/replacer_test.go
View file @
3fd8218f
package
httpserver
package
httpserver
import
(
import
(
"bytes"
"net/http"
"net/http"
"net/http/httptest"
"net/http/httptest"
"os"
"os"
...
@@ -161,3 +162,25 @@ func TestRound(t *testing.T) {
...
@@ -161,3 +162,25 @@ func TestRound(t *testing.T) {
}
}
}
}
}
}
func
TestReadRequestBody
(
t
*
testing
.
T
)
{
r
,
err
:=
http
.
NewRequest
(
"POST"
,
"/"
,
strings
.
NewReader
(
`null`
))
if
err
!=
nil
{
t
.
Error
(
err
)
}
defer
r
.
Body
.
Close
()
body
,
err
:=
readRequestBody
(
r
)
if
err
!=
nil
{
t
.
Error
(
"readRequestBody failed"
,
err
)
}
var
data
=
make
([]
byte
,
len
(
body
))
_
,
err
=
r
.
Body
.
Read
(
data
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
else
if
!
bytes
.
Equal
(
body
,
data
)
{
t
.
Error
(
"Expceted equal bytes."
)
}
}
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