feat(inputs.http_listener_v2): Add custom server http headers (#12645)

This commit is contained in:
Joshua Powers 2023-02-09 02:56:16 -07:00 committed by GitHub
parent 58a01e1daf
commit 5553b33e4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 0 deletions

View File

@ -37,6 +37,11 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
## HTTP methods to accept.
# methods = ["POST", "PUT"]
## Optional HTTP headers
## These headers are applied to the server that is listening for HTTP
## requests and included in responses.
# http_headers = {"HTTP_HEADER" = "TAG_NAME"}
## maximum duration before timing out read of the request
# read_timeout = "10s"
## maximum duration before timing out write of the response

View File

@ -49,6 +49,7 @@ type HTTPListenerV2 struct {
Paths []string `toml:"paths"`
PathTag bool `toml:"path_tag"`
Methods []string `toml:"methods"`
HTTPHeaders map[string]string `toml:"http_headers"`
DataSource string `toml:"data_source"`
ReadTimeout config.Duration `toml:"read_timeout"`
WriteTimeout config.Duration `toml:"write_timeout"`
@ -170,6 +171,10 @@ func (h *HTTPListenerV2) ServeHTTP(res http.ResponseWriter, req *http.Request) {
handler = http.NotFound
}
for key, value := range h.HTTPHeaders {
res.Header().Set(key, value)
}
h.authenticateIfSet(handler, res, req)
}

View File

@ -686,6 +686,27 @@ func TestWriteHTTPFormData(t *testing.T) {
)
}
func TestServerHeaders(t *testing.T) {
listener, err := newTestHTTPListenerV2()
require.NoError(t, err)
listener.HTTPHeaders = map[string]string{
"key": "value",
}
acc := &testutil.Accumulator{}
require.NoError(t, listener.Init())
require.NoError(t, listener.Start(acc))
defer listener.Stop()
// post single message to listener
resp, err := http.Post(createURL(listener, "http", "/write", "db=mydb"), "", bytes.NewBuffer([]byte(testMsg)))
require.NoError(t, err)
require.NoError(t, resp.Body.Close())
require.EqualValues(t, 204, resp.StatusCode)
require.Equal(t, "value", resp.Header.Get("key"))
}
func mustReadHugeMetric() []byte {
filePath := "testdata/huge_metric"
data, err := os.ReadFile(filePath)

View File

@ -12,6 +12,11 @@
## HTTP methods to accept.
# methods = ["POST", "PUT"]
## Optional HTTP headers
## These headers are applied to the server that is listening for HTTP
## requests and included in responses.
# http_headers = {"HTTP_HEADER" = "TAG_NAME"}
## maximum duration before timing out read of the request
# read_timeout = "10s"
## maximum duration before timing out write of the response