TUN-2881: Parameterize response meta information header name in the generating function
This commit is contained in:
parent
322f909edb
commit
06f29306cd
|
@ -236,14 +236,14 @@ type responseMetaHeader struct {
|
||||||
Source string `json:"src"`
|
Source string `json:"src"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateResponseMetaHeader(source string) Header {
|
func CreateResponseMetaHeader(headerName, source string) Header {
|
||||||
jsonResponseMetaHeader, err := json.Marshal(responseMetaHeader{Source: source})
|
jsonResponseMetaHeader, err := json.Marshal(responseMetaHeader{Source: source})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return Header{
|
return Header{
|
||||||
Name: ResponseMetaHeaderField,
|
Name: headerName,
|
||||||
Value: string(jsonResponseMetaHeader),
|
Value: string(jsonResponseMetaHeader),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -606,7 +606,7 @@ func (h *TunnelHandler) ServeStream(stream *h2mux.MuxedStream) error {
|
||||||
|
|
||||||
req, reqErr := h.createRequest(stream)
|
req, reqErr := h.createRequest(stream)
|
||||||
if reqErr != nil {
|
if reqErr != nil {
|
||||||
h.logError(stream, reqErr)
|
h.writeErrorResponse(stream, reqErr)
|
||||||
return reqErr
|
return reqErr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -622,7 +622,7 @@ func (h *TunnelHandler) ServeStream(stream *h2mux.MuxedStream) error {
|
||||||
resp, respErr = h.serveHTTP(stream, req)
|
resp, respErr = h.serveHTTP(stream, req)
|
||||||
}
|
}
|
||||||
if respErr != nil {
|
if respErr != nil {
|
||||||
h.logError(stream, respErr)
|
h.writeErrorResponse(stream, respErr)
|
||||||
return respErr
|
return respErr
|
||||||
}
|
}
|
||||||
h.logResponseOk(resp, cfRay, lbProbe)
|
h.logResponseOk(resp, cfRay, lbProbe)
|
||||||
|
@ -689,7 +689,7 @@ func (h *TunnelHandler) serveHTTP(stream *h2mux.MuxedStream, req *http.Request)
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
|
|
||||||
headers := h2mux.H1ResponseToH2ResponseHeaders(response)
|
headers := h2mux.H1ResponseToH2ResponseHeaders(response)
|
||||||
headers = append(headers, h2mux.CreateResponseMetaHeader(h2mux.ResponseSourceOrigin))
|
headers = append(headers, h2mux.CreateResponseMetaHeader(h2mux.ResponseMetaHeaderField, h2mux.ResponseSourceOrigin))
|
||||||
err = stream.WriteHeaders(headers)
|
err = stream.WriteHeaders(headers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "Error writing response header")
|
return nil, errors.Wrap(err, "Error writing response header")
|
||||||
|
@ -725,11 +725,11 @@ func (h *TunnelHandler) isEventStream(response *http.Response) bool {
|
||||||
return false
|
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")
|
h.logger.WithError(err).Error("HTTP request error")
|
||||||
stream.WriteHeaders([]h2mux.Header{
|
stream.WriteHeaders([]h2mux.Header{
|
||||||
{Name: ":status", Value: "502"},
|
{Name: ":status", Value: "502"},
|
||||||
h2mux.CreateResponseMetaHeader(h2mux.ResponseSourceCloudflared),
|
h2mux.CreateResponseMetaHeader(h2mux.ResponseMetaHeaderField, h2mux.ResponseSourceCloudflared),
|
||||||
})
|
})
|
||||||
stream.Write([]byte("502 Bad Gateway"))
|
stream.Write([]byte("502 Bad Gateway"))
|
||||||
h.metrics.incrementResponses(h.connectionID, "502")
|
h.metrics.incrementResponses(h.connectionID, "502")
|
||||||
|
|
|
@ -67,7 +67,7 @@ func (hc *HTTPService) Proxy(stream *h2mux.MuxedStream, req *http.Request) (*htt
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
responseHeaders := h1ResponseToH2Response(resp)
|
responseHeaders := h1ResponseToH2Response(resp)
|
||||||
responseHeaders = append(responseHeaders, h2mux.CreateResponseMetaHeader(responseSourceOrigin))
|
responseHeaders = append(responseHeaders, h2mux.CreateResponseMetaHeader(h2mux.ResponseMetaHeaderField, responseSourceOrigin))
|
||||||
err = stream.WriteHeaders(responseHeaders)
|
err = stream.WriteHeaders(responseHeaders)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "error writing response header to HTTP origin")
|
return nil, errors.Wrap(err, "error writing response header to HTTP origin")
|
||||||
|
|
|
@ -185,7 +185,7 @@ func (s *StreamHandler) writeErrorStatus(stream *h2mux.MuxedStream, status *http
|
||||||
Name: statusPseudoHeader,
|
Name: statusPseudoHeader,
|
||||||
Value: status.status,
|
Value: status.status,
|
||||||
},
|
},
|
||||||
h2mux.CreateResponseMetaHeader(h2mux.ResponseSourceCloudflared),
|
h2mux.CreateResponseMetaHeader(h2mux.ResponseMetaHeaderField, h2mux.ResponseSourceCloudflared),
|
||||||
})
|
})
|
||||||
_, _ = stream.Write(status.text)
|
_, _ = stream.Write(status.text)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue