TUN-3559: Share response meta header with other packages
This commit is contained in:
parent
fdb1f961b3
commit
a1a554a29d
|
@ -207,14 +207,14 @@ type h2muxRespWriter struct {
|
||||||
|
|
||||||
func (rp *h2muxRespWriter) WriteRespHeaders(resp *http.Response) error {
|
func (rp *h2muxRespWriter) WriteRespHeaders(resp *http.Response) error {
|
||||||
headers := h2mux.H1ResponseToH2ResponseHeaders(resp)
|
headers := h2mux.H1ResponseToH2ResponseHeaders(resp)
|
||||||
headers = append(headers, h2mux.Header{Name: responseMetaHeaderField, Value: responseMetaHeaderOrigin})
|
headers = append(headers, h2mux.Header{Name: ResponseMetaHeaderField, Value: responseMetaHeaderOrigin})
|
||||||
return rp.WriteHeaders(headers)
|
return rp.WriteHeaders(headers)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rp *h2muxRespWriter) WriteErrorResponse() {
|
func (rp *h2muxRespWriter) WriteErrorResponse() {
|
||||||
rp.WriteHeaders([]h2mux.Header{
|
rp.WriteHeaders([]h2mux.Header{
|
||||||
{Name: ":status", Value: "502"},
|
{Name: ":status", Value: "502"},
|
||||||
{Name: responseMetaHeaderField, Value: responseMetaHeaderCfd},
|
{Name: ResponseMetaHeaderField, Value: responseMetaHeaderCfd},
|
||||||
})
|
})
|
||||||
rp.Write([]byte("502 Bad Gateway"))
|
rp.Write([]byte("502 Bad Gateway"))
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,9 +105,9 @@ func TestServeStreamHTTP(t *testing.T) {
|
||||||
require.True(t, hasHeader(stream, ":status", strconv.Itoa(test.expectedStatus)))
|
require.True(t, hasHeader(stream, ":status", strconv.Itoa(test.expectedStatus)))
|
||||||
|
|
||||||
if test.isProxyError {
|
if test.isProxyError {
|
||||||
assert.True(t, hasHeader(stream, responseMetaHeaderField, responseMetaHeaderCfd))
|
assert.True(t, hasHeader(stream, ResponseMetaHeaderField, responseMetaHeaderCfd))
|
||||||
} else {
|
} else {
|
||||||
assert.True(t, hasHeader(stream, responseMetaHeaderField, responseMetaHeaderOrigin))
|
assert.True(t, hasHeader(stream, ResponseMetaHeaderField, responseMetaHeaderOrigin))
|
||||||
body := make([]byte, len(test.expectedBody))
|
body := make([]byte, len(test.expectedBody))
|
||||||
_, err = stream.Read(body)
|
_, err = stream.Read(body)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -154,7 +154,7 @@ func TestServeStreamWS(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
require.True(t, hasHeader(stream, ":status", strconv.Itoa(http.StatusSwitchingProtocols)))
|
require.True(t, hasHeader(stream, ":status", strconv.Itoa(http.StatusSwitchingProtocols)))
|
||||||
assert.True(t, hasHeader(stream, responseMetaHeaderField, responseMetaHeaderOrigin))
|
assert.True(t, hasHeader(stream, ResponseMetaHeaderField, responseMetaHeaderOrigin))
|
||||||
|
|
||||||
data := []byte("test websocket")
|
data := []byte("test websocket")
|
||||||
err = wsutil.WriteClientText(writePipe, data)
|
err = wsutil.WriteClientText(writePipe, data)
|
||||||
|
@ -209,7 +209,7 @@ func benchmarkServeStreamHTTPSimple(b *testing.B, test testRequest) {
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
|
|
||||||
require.NoError(b, openstreamErr)
|
require.NoError(b, openstreamErr)
|
||||||
assert.True(b, hasHeader(stream, responseMetaHeaderField, responseMetaHeaderOrigin))
|
assert.True(b, hasHeader(stream, ResponseMetaHeaderField, responseMetaHeaderOrigin))
|
||||||
require.True(b, hasHeader(stream, ":status", strconv.Itoa(http.StatusOK)))
|
require.True(b, hasHeader(stream, ":status", strconv.Itoa(http.StatusOK)))
|
||||||
require.NoError(b, readBodyErr)
|
require.NoError(b, readBodyErr)
|
||||||
require.Equal(b, test.expectedBody, body)
|
require.Equal(b, test.expectedBody, body)
|
||||||
|
|
|
@ -8,12 +8,12 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
responseMetaHeaderField = "cf-cloudflared-response-meta"
|
ResponseMetaHeaderField = "cf-cloudflared-response-meta"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
canonicalResponseUserHeadersField = http.CanonicalHeaderKey(h2mux.ResponseUserHeadersField)
|
canonicalResponseUserHeadersField = http.CanonicalHeaderKey(h2mux.ResponseUserHeadersField)
|
||||||
canonicalResponseMetaHeaderField = http.CanonicalHeaderKey(responseMetaHeaderField)
|
canonicalResponseMetaHeaderField = http.CanonicalHeaderKey(ResponseMetaHeaderField)
|
||||||
responseMetaHeaderCfd = mustInitRespMetaHeader("cloudflared")
|
responseMetaHeaderCfd = mustInitRespMetaHeader("cloudflared")
|
||||||
responseMetaHeaderOrigin = mustInitRespMetaHeader("origin")
|
responseMetaHeaderOrigin = mustInitRespMetaHeader("origin")
|
||||||
)
|
)
|
||||||
|
|
|
@ -100,9 +100,9 @@ func TestServeHTTP(t *testing.T) {
|
||||||
require.Equal(t, test.expectedBody, respBody)
|
require.Equal(t, test.expectedBody, respBody)
|
||||||
}
|
}
|
||||||
if test.isProxyError {
|
if test.isProxyError {
|
||||||
require.Equal(t, responseMetaHeaderCfd, resp.Header.Get(responseMetaHeaderField))
|
require.Equal(t, responseMetaHeaderCfd, resp.Header.Get(ResponseMetaHeaderField))
|
||||||
} else {
|
} else {
|
||||||
require.Equal(t, responseMetaHeaderOrigin, resp.Header.Get(responseMetaHeaderField))
|
require.Equal(t, responseMetaHeaderOrigin, resp.Header.Get(ResponseMetaHeaderField))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cancel()
|
cancel()
|
||||||
|
@ -202,7 +202,7 @@ func TestServeWS(t *testing.T) {
|
||||||
resp := respWriter.Result()
|
resp := respWriter.Result()
|
||||||
// http2RespWriter should rewrite status 101 to 200
|
// http2RespWriter should rewrite status 101 to 200
|
||||||
require.Equal(t, http.StatusOK, resp.StatusCode)
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||||
require.Equal(t, responseMetaHeaderOrigin, resp.Header.Get(responseMetaHeaderField))
|
require.Equal(t, responseMetaHeaderOrigin, resp.Header.Get(ResponseMetaHeaderField))
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue