2019-03-14 08:30:04 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
2020-08-17 16:04:03 +00:00
|
|
|
ptypes "github.com/traefik/paerser/types"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/log"
|
2019-03-14 08:30:04 +00:00
|
|
|
)
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// StatusClientClosedRequest non-standard HTTP status code for client disconnection.
|
2019-03-14 08:30:04 +00:00
|
|
|
const StatusClientClosedRequest = 499
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// StatusClientClosedRequestText non-standard HTTP status for client disconnection.
|
2019-03-14 08:30:04 +00:00
|
|
|
const StatusClientClosedRequestText = "Client Closed Request"
|
|
|
|
|
2020-09-11 13:40:03 +00:00
|
|
|
func buildProxy(passHostHeader *bool, responseForwarding *dynamic.ResponseForwarding, roundTripper http.RoundTripper, bufferPool httputil.BufferPool) (http.Handler, error) {
|
2020-08-17 16:04:03 +00:00
|
|
|
var flushInterval ptypes.Duration
|
2019-03-14 08:30:04 +00:00
|
|
|
if responseForwarding != nil {
|
|
|
|
err := flushInterval.Set(responseForwarding.FlushInterval)
|
|
|
|
if err != nil {
|
2020-05-11 10:06:07 +00:00
|
|
|
return nil, fmt.Errorf("error creating flush interval: %w", err)
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if flushInterval == 0 {
|
2020-08-17 16:04:03 +00:00
|
|
|
flushInterval = ptypes.Duration(100 * time.Millisecond)
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
proxy := &httputil.ReverseProxy{
|
|
|
|
Director: func(outReq *http.Request) {
|
|
|
|
u := outReq.URL
|
|
|
|
if outReq.RequestURI != "" {
|
|
|
|
parsedURL, err := url.ParseRequestURI(outReq.RequestURI)
|
|
|
|
if err == nil {
|
|
|
|
u = parsedURL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
outReq.URL.Path = u.Path
|
|
|
|
outReq.URL.RawPath = u.RawPath
|
|
|
|
outReq.URL.RawQuery = u.RawQuery
|
|
|
|
outReq.RequestURI = "" // Outgoing request should not have RequestURI
|
|
|
|
|
|
|
|
outReq.Proto = "HTTP/1.1"
|
|
|
|
outReq.ProtoMajor = 1
|
|
|
|
outReq.ProtoMinor = 1
|
|
|
|
|
2019-12-18 10:22:06 +00:00
|
|
|
if _, ok := outReq.Header["User-Agent"]; !ok {
|
|
|
|
outReq.Header.Set("User-Agent", "")
|
|
|
|
}
|
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
// Do not pass client Host header unless optsetter PassHostHeader is set.
|
2019-09-30 16:12:04 +00:00
|
|
|
if passHostHeader != nil && !*passHostHeader {
|
2019-03-14 08:30:04 +00:00
|
|
|
outReq.Host = outReq.URL.Host
|
|
|
|
}
|
|
|
|
|
2019-09-17 14:12:04 +00:00
|
|
|
// Even if the websocket RFC says that headers should be case-insensitive,
|
2020-04-27 16:12:04 +00:00
|
|
|
// some servers need Sec-WebSocket-Key, Sec-WebSocket-Extensions, Sec-WebSocket-Accept,
|
|
|
|
// Sec-WebSocket-Protocol and Sec-WebSocket-Version to be case-sensitive.
|
2019-09-17 14:12:04 +00:00
|
|
|
// https://tools.ietf.org/html/rfc6455#page-20
|
|
|
|
outReq.Header["Sec-WebSocket-Key"] = outReq.Header["Sec-Websocket-Key"]
|
2020-04-27 16:12:04 +00:00
|
|
|
outReq.Header["Sec-WebSocket-Extensions"] = outReq.Header["Sec-Websocket-Extensions"]
|
|
|
|
outReq.Header["Sec-WebSocket-Accept"] = outReq.Header["Sec-Websocket-Accept"]
|
2020-04-14 16:24:04 +00:00
|
|
|
outReq.Header["Sec-WebSocket-Protocol"] = outReq.Header["Sec-Websocket-Protocol"]
|
2020-04-21 15:16:05 +00:00
|
|
|
outReq.Header["Sec-WebSocket-Version"] = outReq.Header["Sec-Websocket-Version"]
|
2019-09-17 14:12:04 +00:00
|
|
|
delete(outReq.Header, "Sec-Websocket-Key")
|
2020-04-27 16:12:04 +00:00
|
|
|
delete(outReq.Header, "Sec-Websocket-Extensions")
|
|
|
|
delete(outReq.Header, "Sec-Websocket-Accept")
|
2020-04-14 16:24:04 +00:00
|
|
|
delete(outReq.Header, "Sec-Websocket-Protocol")
|
2020-04-21 15:16:05 +00:00
|
|
|
delete(outReq.Header, "Sec-Websocket-Version")
|
2019-03-14 08:30:04 +00:00
|
|
|
},
|
2020-09-11 13:40:03 +00:00
|
|
|
Transport: roundTripper,
|
2020-09-01 16:16:04 +00:00
|
|
|
FlushInterval: time.Duration(flushInterval),
|
|
|
|
BufferPool: bufferPool,
|
2019-03-14 08:30:04 +00:00
|
|
|
ErrorHandler: func(w http.ResponseWriter, request *http.Request, err error) {
|
|
|
|
statusCode := http.StatusInternalServerError
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case err == io.EOF:
|
|
|
|
statusCode = http.StatusBadGateway
|
|
|
|
case err == context.Canceled:
|
|
|
|
statusCode = StatusClientClosedRequest
|
|
|
|
default:
|
|
|
|
if e, ok := err.(net.Error); ok {
|
|
|
|
if e.Timeout() {
|
|
|
|
statusCode = http.StatusGatewayTimeout
|
|
|
|
} else {
|
|
|
|
statusCode = http.StatusBadGateway
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("'%d %s' caused by: %v", statusCode, statusText(statusCode), err)
|
|
|
|
w.WriteHeader(statusCode)
|
|
|
|
_, werr := w.Write([]byte(statusText(statusCode)))
|
|
|
|
if werr != nil {
|
|
|
|
log.Debugf("Error while writing status code", werr)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return proxy, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func statusText(statusCode int) string {
|
|
|
|
if statusCode == StatusClientClosedRequest {
|
|
|
|
return StatusClientClosedRequestText
|
|
|
|
}
|
|
|
|
return http.StatusText(statusCode)
|
|
|
|
}
|