From b84cb058484e7900d60324da1289d319c77f5447 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Mon, 20 May 2019 22:00:54 -0600 Subject: Fix deferred header ops --- modules/caddyhttp/headers/headers.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'modules/caddyhttp/headers') diff --git a/modules/caddyhttp/headers/headers.go b/modules/caddyhttp/headers/headers.go index 1826c7a..37efc57 100644 --- a/modules/caddyhttp/headers/headers.go +++ b/modules/caddyhttp/headers/headers.go @@ -17,16 +17,16 @@ func init() { // Headers is a middleware which can mutate HTTP headers. type Headers struct { - Request HeaderOps - Response RespHeaderOps + Request HeaderOps `json:"request"` + Response RespHeaderOps `json:"response"` } // HeaderOps defines some operations to // perform on HTTP headers. type HeaderOps struct { - Add http.Header - Set http.Header - Delete []string + Add http.Header `json:"add"` + Set http.Header `json:"set"` + Delete []string `json:"delete"` } // RespHeaderOps is like HeaderOps, but @@ -67,10 +67,22 @@ func apply(ops HeaderOps, hdr http.Header) { // operations until WriteHeader is called. type responseWriterWrapper struct { *caddyhttp.ResponseWriterWrapper - headerOps HeaderOps + headerOps HeaderOps + wroteHeader bool +} + +func (rww *responseWriterWrapper) Write(d []byte) (int, error) { + if !rww.wroteHeader { + rww.WriteHeader(http.StatusOK) + } + return rww.ResponseWriterWrapper.Write(d) } func (rww *responseWriterWrapper) WriteHeader(status int) { + if rww.wroteHeader { + return + } + rww.wroteHeader = true apply(rww.headerOps, rww.ResponseWriterWrapper.Header()) rww.ResponseWriterWrapper.WriteHeader(status) } -- cgit v1.2.3