TUN-2881: Parameterize response meta information header name in the generating function

This commit is contained in:
Areg Harutyunyan 2020-04-10 20:26:09 +01:00
parent 322f909edb
commit 06f29306cd
4 changed files with 9 additions and 9 deletions

View File

@ -236,14 +236,14 @@ type responseMetaHeader struct {
Source string `json:"src"`
}
func CreateResponseMetaHeader(source string) Header {
func CreateResponseMetaHeader(headerName, source string) Header {
jsonResponseMetaHeader, err := json.Marshal(responseMetaHeader{Source: source})
if err != nil {
panic(err)
}
return Header{
Name: ResponseMetaHeaderField,
Name: headerName,
Value: string(jsonResponseMetaHeader),
}
}

View File

@ -606,7 +606,7 @@ func (h *TunnelHandler) ServeStream(stream *h2mux.MuxedStream) error {
req, reqErr := h.createRequest(stream)
if reqErr != nil {
h.logError(stream, reqErr)
h.writeErrorResponse(stream, reqErr)
return reqErr
}
@ -622,7 +622,7 @@ func (h *TunnelHandler) ServeStream(stream *h2mux.MuxedStream) error {
resp, respErr = h.serveHTTP(stream, req)
}
if respErr != nil {
h.logError(stream, respErr)
h.writeErrorResponse(stream, respErr)
return respErr
}
h.logResponseOk(resp, cfRay, lbProbe)
@ -689,7 +689,7 @@ func (h *TunnelHandler) serveHTTP(stream *h2mux.MuxedStream, req *http.Request)
defer response.Body.Close()
headers := h2mux.H1ResponseToH2ResponseHeaders(response)
headers = append(headers, h2mux.CreateResponseMetaHeader(h2mux.ResponseSourceOrigin))
headers = append(headers, h2mux.CreateResponseMetaHeader(h2mux.ResponseMetaHeaderField, h2mux.ResponseSourceOrigin))
err = stream.WriteHeaders(headers)
if err != nil {
return nil, errors.Wrap(err, "Error writing response header")
@ -725,11 +725,11 @@ func (h *TunnelHandler) isEventStream(response *http.Response) bool {
return false
}
func (h *TunnelHandler) logError(stream *h2mux.MuxedStream, err error) {
func (h *TunnelHandler) writeErrorResponse(stream *h2mux.MuxedStream, err error) {
h.logger.WithError(err).Error("HTTP request error")
stream.WriteHeaders([]h2mux.Header{
{Name: ":status", Value: "502"},
h2mux.CreateResponseMetaHeader(h2mux.ResponseSourceCloudflared),
h2mux.CreateResponseMetaHeader(h2mux.ResponseMetaHeaderField, h2mux.ResponseSourceCloudflared),
})
stream.Write([]byte("502 Bad Gateway"))
h.metrics.incrementResponses(h.connectionID, "502")

View File

@ -67,7 +67,7 @@ func (hc *HTTPService) Proxy(stream *h2mux.MuxedStream, req *http.Request) (*htt
defer resp.Body.Close()
responseHeaders := h1ResponseToH2Response(resp)
responseHeaders = append(responseHeaders, h2mux.CreateResponseMetaHeader(responseSourceOrigin))
responseHeaders = append(responseHeaders, h2mux.CreateResponseMetaHeader(h2mux.ResponseMetaHeaderField, responseSourceOrigin))
err = stream.WriteHeaders(responseHeaders)
if err != nil {
return nil, errors.Wrap(err, "error writing response header to HTTP origin")

View File

@ -185,7 +185,7 @@ func (s *StreamHandler) writeErrorStatus(stream *h2mux.MuxedStream, status *http
Name: statusPseudoHeader,
Value: status.status,
},
h2mux.CreateResponseMetaHeader(h2mux.ResponseSourceCloudflared),
h2mux.CreateResponseMetaHeader(h2mux.ResponseMetaHeaderField, h2mux.ResponseSourceCloudflared),
})
_, _ = stream.Write(status.text)
}