agent: limit HTTP check output to 4k

This commit is contained in:
Ryan Uber 2016-04-14 14:28:07 -07:00
parent 8c8b146f77
commit 8cc16b8238
2 changed files with 16 additions and 6 deletions

View File

@ -2,7 +2,7 @@ package agent
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"log" "log"
"net" "net"
"net/http" "net/http"
@ -423,13 +423,14 @@ func (c *CheckHTTP) check() {
} }
defer resp.Body.Close() defer resp.Body.Close()
// Format the response body // Read the response into a circular buffer to limit the size
body, err := ioutil.ReadAll(resp.Body) output, _ := circbuf.NewBuffer(CheckBufSize)
if err != nil { if _, err := io.Copy(output, resp.Body); err != nil {
c.Logger.Printf("[WARN] agent: check '%v': Get error while reading body: %s", c.CheckID, err) c.Logger.Printf("[WARN] agent: check '%v': Get error while reading body: %s", c.CheckID, err)
body = []byte{}
} }
result := fmt.Sprintf("HTTP GET %s: %s Output: %s", c.HTTP, resp.Status, body)
// Format the response body
result := fmt.Sprintf("HTTP GET %s: %s Output: %s", c.HTTP, resp.Status, output.String())
if resp.StatusCode >= 200 && resp.StatusCode <= 299 { if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
// PASSING (2xx) // PASSING (2xx)

View File

@ -1,6 +1,7 @@
package agent package agent
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"log" "log"
@ -186,7 +187,10 @@ func TestCheckTTL(t *testing.T) {
func mockHTTPServer(responseCode int) *httptest.Server { func mockHTTPServer(responseCode int) *httptest.Server {
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Body larger than 4k limit
body := bytes.Repeat([]byte{'a'}, 2*CheckBufSize)
w.WriteHeader(responseCode) w.WriteHeader(responseCode)
w.Write(body)
return return
}) })
@ -219,6 +223,11 @@ func expectHTTPStatus(t *testing.T, url string, status string) {
if mock.state["foo"] != status { if mock.state["foo"] != status {
t.Fatalf("should be %v %v", status, mock.state) t.Fatalf("should be %v %v", status, mock.state)
} }
// Allow slightly more data than CheckBufSize, for the header
if n := len(mock.output["foo"]); n > (CheckBufSize + 256) {
t.Fatalf("output too long: %d (%d-byte limit)", n, CheckBufSize)
}
} }
func TestCheckHTTPCritical(t *testing.T) { func TestCheckHTTPCritical(t *testing.T) {