mirror of
https://github.com/caddyserver/caddy.git
synced 2025-02-03 23:09:57 -05:00
Using a LimitReader and fixed test and log format.
This commit is contained in:
parent
3fd8218f67
commit
d56ac28bec
2 changed files with 25 additions and 16 deletions
|
@ -2,8 +2,8 @@ package httpserver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
|
@ -127,13 +127,12 @@ func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Repla
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := readRequestBody(r)
|
body, err := readRequestBody(r, maxLogBodySize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[WARNING] Cannot copy request body %v", err)
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(body)
|
return requestReplacer.Replace(string(body))
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
emptyValue: emptyValue,
|
emptyValue: emptyValue,
|
||||||
|
@ -163,13 +162,18 @@ func canLogRequest(r *http.Request) (canLog bool) {
|
||||||
|
|
||||||
// readRequestBody reads the request body and sets a
|
// readRequestBody reads the request body and sets a
|
||||||
// new io.ReadCloser that has not yet been read.
|
// new io.ReadCloser that has not yet been read.
|
||||||
func readRequestBody(r *http.Request) ([]byte, error) {
|
func readRequestBody(r *http.Request, n int64) ([]byte, error) {
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := ioutil.ReadAll(io.LimitReader(r.Body, n))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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
|
return body, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -272,4 +276,5 @@ const (
|
||||||
headerContentType = "Content-Type"
|
headerContentType = "Content-Type"
|
||||||
contentTypeJSON = "application/json"
|
contentTypeJSON = "application/json"
|
||||||
contentTypeXML = "application/xml"
|
contentTypeXML = "application/xml"
|
||||||
|
maxLogBodySize = 100 * 1000
|
||||||
)
|
)
|
||||||
|
|
|
@ -2,6 +2,7 @@ package httpserver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
|
@ -164,23 +165,26 @@ func TestRound(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReadRequestBody(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 {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
|
|
||||||
body, err := readRequestBody(r)
|
logBody, err := readRequestBody(r, readSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("readRequestBody failed", err)
|
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))
|
// Ensure the Request body is the same as the original.
|
||||||
_, err = r.Body.Read(data)
|
reqBody, err := ioutil.ReadAll(r.Body)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error("Unable to read request body", err)
|
||||||
} else if !bytes.Equal(body, data) {
|
} else if !bytes.EqualFold(payload, reqBody) {
|
||||||
t.Error("Expceted equal bytes.")
|
t.Error("Expected request body comparison failed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue