mirror of
https://github.com/hanzoai/oxy.git
synced 2026-09-30 23:42:31 +00:00
chore: linting
This commit is contained in:
+16
-24
@@ -91,42 +91,38 @@ issues:
|
||||
- 'Error return value of .((os\\.)?std(out|err)\\..*|.*Close|.*Flush|os\\.Remove(All)?|.*printf?|os\\.(Un)?Setenv). is not checked'
|
||||
- 'SA1019: http.CloseNotifier has been deprecated'
|
||||
- 'string `https` has 3 occurrences, make it a constant'
|
||||
- 'ST1000: at least one file in a package should have a package comment' # TODO(ldez) must be fixed
|
||||
- 'SA1019: tls.VersionSSL30 has been deprecated' # TODO(ldez) must be fixed
|
||||
- 'ST1005: error strings should not be capitalized'# TODO(ldez) must be fixed
|
||||
- 'exported: func name will be used as roundrobin.RoundRobinLogger by other packages'# TODO(ldez) must be fixed
|
||||
- 'exported: func name will be used as roundrobin.RoundRobinRequestRewriteListener by other packages'# TODO(ldez) must be fixed
|
||||
- 'exported: type name will be used as connlimit.ConnLimitOption by other packages'# TODO(ldez) must be fixed
|
||||
|
||||
- 'ST1003: method ToJson should be ToJSON' # TODO(ldez) must be fixed
|
||||
- 'ST1003: type SerializableHttpRequest should be SerializableHTTPRequest' # TODO(ldez) must be fixed
|
||||
- 'ST1003: func DumpHttpRequest should be DumpHTTPRequest' # TODO(ldez) must be fixed
|
||||
- 'ST1003: method getUrlFromRequest should be getURLFromRequest' # TODO(ldez) must be fixed
|
||||
- 'ST1003: struct field ProxyfiedUrl should be ProxyfiedURL' # TODO(ldez) must be fixed
|
||||
- 'ST1003: struct field TargetUrl should be TargetURL' # TODO(ldez) must be fixed
|
||||
- 'ST1003: const XRealIp should be XRealIP' # TODO(ldez) must be fixed
|
||||
- 'ST1003: type UrlForwardingStateListener should be URLForwardingStateListener' # TODO(ldez) must be fixed
|
||||
- 'ST1003: var backendUrl should be backendURL' # TODO(ldez) must be fixed
|
||||
- 'var-naming: var backendUrl should be backendURL' # TODO(ldez) must be fixed
|
||||
- 'var-naming: type UrlForwardingStateListener should be URLForwardingStateListener' # TODO(ldez) must be fixed
|
||||
- 'var-naming: const XRealIp should be XRealIP' # TODO(ldez) must be fixed
|
||||
- 'var-naming: struct field TargetUrl should be TargetURL' # TODO(ldez) must be fixed
|
||||
- 'var-naming: struct field ProxyfiedUrl should be ProxyfiedURL' # TODO(ldez) must be fixed
|
||||
- 'var-naming: method getUrlFromRequest should be getURLFromRequest' # TODO(ldez) must be fixed
|
||||
- 'var-naming: method ToJson should be ToJSON' # TODO(ldez) must be fixed
|
||||
- 'var-naming: type SerializableHttpRequest should be SerializableHTTPRequest' # TODO(ldez) must be fixed
|
||||
- 'var-naming: func DumpHttpRequest should be DumpHTTPRequest' # TODO(ldez) must be fixed
|
||||
|
||||
- 'exported: func name will be used as roundrobin.RoundRobinLogger by other packages'# TODO(ldez) must be fixed
|
||||
- 'exported: func name will be used as roundrobin.RoundRobinRequestRewriteListener by other packages'# TODO(ldez) must be fixed
|
||||
- 'exported: type name will be used as connlimit.ConnLimitOption by other packages'# TODO(ldez) must be fixed
|
||||
|
||||
- 'ST1000: at least one file in a package should have a package comment' # TODO(ldez) must be fixed
|
||||
- 'SA1019: tls.VersionSSL30 has been deprecated' # TODO(ldez) must be fixed
|
||||
- 'Error return value of `resp.Body.Close` is not checked' # TODO(ldez) must be fixed
|
||||
- '`marshalling` is a misspelling of `marshaling`' # TODO(ldez) must be fixed
|
||||
|
||||
- 'ST1005: error strings should not be capitalized'# TODO(ldez) must be fixed
|
||||
- 'ST1005: error strings should not end with punctuation or a newline' # TODO(ldez) must be fixed
|
||||
- 'error-strings: error strings should not be capitalized or end with punctuation or a newline' # TODO(ldez) must be fixed
|
||||
|
||||
- 'unexported-return: exported func ([^ ]+) returns unexported type stream.optSetter, which can be annoying to use' # TODO(ldez) must be fixed
|
||||
- 'unexported-return: exported func ([^ ]+) returns unexported type buffer.optSetter, which can be annoying to use' # TODO(ldez) must be fixed
|
||||
- 'unexported-return: exported func ([^ ]+) returns unexported type forward.optSetter, which can be annoying to use' # TODO(ldez) must be fixed
|
||||
- 'unexported-return: exported func ([^ ]+) returns unexported type memmetrics.rrOptSetter, which can be annoying to use' # TODO(ldez) must be fixed
|
||||
- 'unexported-return: exported func ([^ ]+) returns unexported type memmetrics.rcOptSetter, which can be annoying to use' # TODO(ldez) must be fixed
|
||||
- 'unexported-return: exported func ([^ ]+) returns unexported type memmetrics.rhOptSetter, which can be annoying to use' # TODO(ldez) must be fixed
|
||||
- 'unexported-return: exported func ([^ ]+) returns unexported type memmetrics.ratioOptSetter, which can be annoying to use' # TODO(ldez) must be fixed
|
||||
- 'unexported-return: exported func ([^ ]+) returns unexported type forward.optSetter, which can be annoying to use' # TODO(ldez) must be fixed
|
||||
- 'error-strings: error strings should not be capitalized or end with punctuation or a newline' # TODO(ldez) must be fixed
|
||||
- 'ST1005: error strings should not end with punctuation or a newline' # TODO(ldez) must be fixed
|
||||
- 'Error return value of `resp.Body.Close` is not checked' # TODO(ldez) must be fixed
|
||||
- 'G104: Errors unhandled.' # TODO(ldez) must be fixed
|
||||
- '`marshalling` is a misspelling of `marshaling`' # TODO(ldez) must be fixed
|
||||
|
||||
exclude-rules:
|
||||
- path: .*_test.go
|
||||
@@ -136,10 +132,6 @@ issues:
|
||||
- path: testutils/.+
|
||||
linters:
|
||||
- gosec
|
||||
- path: .*_test.go
|
||||
text: "context-keys-type: should not use basic type string as key" # TODO(ldez) must be fixed
|
||||
- path: .*_test.go
|
||||
text: "SA1029: should not use built-in type string as key for value" # TODO(ldez) must be fixed
|
||||
- path: cbreaker/cbreaker_test.go
|
||||
text: "`statsNetErrors` - `threshold` always receives `0.6`" # TODO(ldez) must be fixed
|
||||
- path: buffer/buffer.go
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
.PHONY: all
|
||||
.PHONY: default clean checks test test-verbose
|
||||
|
||||
export GO111MODULE=on
|
||||
|
||||
|
||||
+45
-45
@@ -59,47 +59,7 @@ const (
|
||||
|
||||
var errHandler utils.ErrorHandler = &SizeErrHandler{}
|
||||
|
||||
// Buffer is responsible for buffering requests and responses
|
||||
// It buffers large requests and responses to disk,.
|
||||
type Buffer struct {
|
||||
maxRequestBodyBytes int64
|
||||
memRequestBodyBytes int64
|
||||
|
||||
maxResponseBodyBytes int64
|
||||
memResponseBodyBytes int64
|
||||
|
||||
retryPredicate hpredicate
|
||||
|
||||
next http.Handler
|
||||
errHandler utils.ErrorHandler
|
||||
|
||||
log *log.Logger
|
||||
}
|
||||
|
||||
// New returns a new buffer middleware. New() function supports optional functional arguments.
|
||||
func New(next http.Handler, setters ...optSetter) (*Buffer, error) {
|
||||
strm := &Buffer{
|
||||
next: next,
|
||||
|
||||
maxRequestBodyBytes: DefaultMaxBodyBytes,
|
||||
memRequestBodyBytes: DefaultMemBodyBytes,
|
||||
|
||||
maxResponseBodyBytes: DefaultMaxBodyBytes,
|
||||
memResponseBodyBytes: DefaultMemBodyBytes,
|
||||
|
||||
log: log.StandardLogger(),
|
||||
}
|
||||
for _, s := range setters {
|
||||
if err := s(strm); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if strm.errHandler == nil {
|
||||
strm.errHandler = errHandler
|
||||
}
|
||||
|
||||
return strm, nil
|
||||
}
|
||||
type optSetter func(b *Buffer) error
|
||||
|
||||
// Logger defines the logger the buffer will use.
|
||||
//
|
||||
@@ -111,8 +71,6 @@ func Logger(l *log.Logger) optSetter {
|
||||
}
|
||||
}
|
||||
|
||||
type optSetter func(b *Buffer) error
|
||||
|
||||
// CondSetter Conditional setter.
|
||||
// ex: Cond(a > 4, MemRequestBodyBytes(a))
|
||||
func CondSetter(condition bool, setter optSetter) optSetter {
|
||||
@@ -201,6 +159,48 @@ func MemResponseBodyBytes(m int64) optSetter {
|
||||
}
|
||||
}
|
||||
|
||||
// Buffer is responsible for buffering requests and responses
|
||||
// It buffers large requests and responses to disk,.
|
||||
type Buffer struct {
|
||||
maxRequestBodyBytes int64
|
||||
memRequestBodyBytes int64
|
||||
|
||||
maxResponseBodyBytes int64
|
||||
memResponseBodyBytes int64
|
||||
|
||||
retryPredicate hpredicate
|
||||
|
||||
next http.Handler
|
||||
errHandler utils.ErrorHandler
|
||||
|
||||
log *log.Logger
|
||||
}
|
||||
|
||||
// New returns a new buffer middleware. New() function supports optional functional arguments.
|
||||
func New(next http.Handler, setters ...optSetter) (*Buffer, error) {
|
||||
strm := &Buffer{
|
||||
next: next,
|
||||
|
||||
maxRequestBodyBytes: DefaultMaxBodyBytes,
|
||||
memRequestBodyBytes: DefaultMemBodyBytes,
|
||||
|
||||
maxResponseBodyBytes: DefaultMaxBodyBytes,
|
||||
memResponseBodyBytes: DefaultMemBodyBytes,
|
||||
|
||||
log: log.StandardLogger(),
|
||||
}
|
||||
for _, s := range setters {
|
||||
if err := s(strm); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if strm.errHandler == nil {
|
||||
strm.errHandler = errHandler
|
||||
}
|
||||
|
||||
return strm, nil
|
||||
}
|
||||
|
||||
// Wrap sets the next handler to be called by buffer handler.
|
||||
func (b *Buffer) Wrap(next http.Handler) error {
|
||||
b.next = next
|
||||
@@ -209,7 +209,7 @@ func (b *Buffer) Wrap(next http.Handler) error {
|
||||
|
||||
func (b *Buffer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
if b.log.Level >= log.DebugLevel {
|
||||
logEntry := b.log.WithField("Request", utils.DumpHttpRequest(req))
|
||||
logEntry := b.log.WithField("Request", utils.DumpHTTPRequest(req))
|
||||
logEntry.Debug("vulcand/oxy/buffer: begin ServeHttp on request")
|
||||
defer logEntry.Debug("vulcand/oxy/buffer: completed ServeHttp on request")
|
||||
}
|
||||
@@ -397,7 +397,7 @@ func (b *bufferWriter) WriteHeader(code int) {
|
||||
b.code = code
|
||||
}
|
||||
|
||||
// CloseNotifier interface - this allows downstream connections to be terminated when the client terminates.
|
||||
// CloseNotify CloseNotifier interface - this allows downstream connections to be terminated when the client terminates.
|
||||
func (b *bufferWriter) CloseNotify() <-chan bool {
|
||||
if cn, ok := b.responseWriter.(http.CloseNotifier); ok {
|
||||
return cn.CloseNotify()
|
||||
|
||||
@@ -110,7 +110,7 @@ func Logger(l *log.Logger) CircuitBreakerOption {
|
||||
|
||||
func (c *CircuitBreaker) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
if c.log.Level >= log.DebugLevel {
|
||||
logEntry := c.log.WithField("Request", utils.DumpHttpRequest(req))
|
||||
logEntry := c.log.WithField("Request", utils.DumpHTTPRequest(req))
|
||||
logEntry.Debug("vulcand/oxy/circuitbreaker: begin ServeHttp on request")
|
||||
defer logEntry.Debug("vulcand/oxy/circuitbreaker: completed ServeHttp on request")
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ func NewResponseFallback(r Response) (*ResponseFallback, error) {
|
||||
|
||||
func (f *ResponseFallback) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
if f.log.Level >= log.DebugLevel {
|
||||
logEntry := f.log.WithField("Request", utils.DumpHttpRequest(req))
|
||||
logEntry := f.log.WithField("Request", utils.DumpHTTPRequest(req))
|
||||
logEntry.Debug("vulcand/oxy/fallback/response: begin ServeHttp on request")
|
||||
defer logEntry.Debug("vulcand/oxy/fallback/response: completed ServeHttp on request")
|
||||
}
|
||||
@@ -86,7 +86,7 @@ func NewRedirectFallback(r Redirect) (*RedirectFallback, error) {
|
||||
|
||||
func (f *RedirectFallback) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
if f.log.Level >= log.DebugLevel {
|
||||
logEntry := f.log.WithField("Request", utils.DumpHttpRequest(req))
|
||||
logEntry := f.log.WithField("Request", utils.DumpHTTPRequest(req))
|
||||
logEntry.Debug("vulcand/oxy/fallback/redirect: begin ServeHttp on request")
|
||||
defer logEntry.Debug("vulcand/oxy/fallback/redirect: completed ServeHttp on request")
|
||||
}
|
||||
|
||||
+18
-14
@@ -25,7 +25,7 @@ type ConnLimiter struct {
|
||||
}
|
||||
|
||||
// New creates a new ConnLimiter.
|
||||
func New(next http.Handler, extract utils.SourceExtractor, maxConnections int64, options ...ConnLimitOption) (*ConnLimiter, error) {
|
||||
func New(next http.Handler, extract utils.SourceExtractor, maxConnections int64, options ...Option) (*ConnLimiter, error) {
|
||||
if extract == nil {
|
||||
return nil, fmt.Errorf("Extract function can not be nil")
|
||||
}
|
||||
@@ -51,16 +51,6 @@ func New(next http.Handler, extract utils.SourceExtractor, maxConnections int64,
|
||||
return cl, nil
|
||||
}
|
||||
|
||||
// Logger defines the logger the connection limiter will use.
|
||||
//
|
||||
// It defaults to logrus.StandardLogger(), the global logger used by logrus.
|
||||
func Logger(l *log.Logger) ConnLimitOption {
|
||||
return func(cl *ConnLimiter) error {
|
||||
cl.log = l
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Wrap sets the next handler to be called by connection limiter handler.
|
||||
func (cl *ConnLimiter) Wrap(h http.Handler) {
|
||||
cl.next = h
|
||||
@@ -127,7 +117,7 @@ type ConnErrHandler struct {
|
||||
|
||||
func (e *ConnErrHandler) ServeHTTP(w http.ResponseWriter, req *http.Request, err error) {
|
||||
if e.log.Level >= log.DebugLevel {
|
||||
logEntry := e.log.WithField("Request", utils.DumpHttpRequest(req))
|
||||
logEntry := e.log.WithField("Request", utils.DumpHTTPRequest(req))
|
||||
logEntry.Debug("vulcand/oxy/connlimit: begin ServeHttp on request")
|
||||
defer logEntry.Debug("vulcand/oxy/connlimit: completed ServeHttp on request")
|
||||
}
|
||||
@@ -140,11 +130,25 @@ func (e *ConnErrHandler) ServeHTTP(w http.ResponseWriter, req *http.Request, err
|
||||
utils.DefaultHandler.ServeHTTP(w, req, err)
|
||||
}
|
||||
|
||||
// Logger defines the logger the connection limiter will use.
|
||||
//
|
||||
// It defaults to logrus.StandardLogger(), the global logger used by logrus.
|
||||
func Logger(l *log.Logger) Option {
|
||||
return func(cl *ConnLimiter) error {
|
||||
cl.log = l
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// ConnLimitOption connection limit option type.
|
||||
type ConnLimitOption func(l *ConnLimiter) error
|
||||
// Deprecated: use Option instead.
|
||||
type ConnLimitOption = Option
|
||||
|
||||
// Option connection limit option type.
|
||||
type Option func(l *ConnLimiter) error
|
||||
|
||||
// ErrorHandler sets error handler of the server.
|
||||
func ErrorHandler(h utils.ErrorHandler) ConnLimitOption {
|
||||
func ErrorHandler(h utils.ErrorHandler) Option {
|
||||
return func(cl *ConnLimiter) error {
|
||||
cl.errHandler = h
|
||||
return nil
|
||||
|
||||
+18
-15
@@ -121,7 +121,7 @@ func Logger(l log.FieldLogger) optSetter {
|
||||
}
|
||||
|
||||
// StateListener defines a state listener for the HTTP forwarder.
|
||||
func StateListener(stateListener UrlForwardingStateListener) optSetter {
|
||||
func StateListener(stateListener URLForwardingStateListener) optSetter {
|
||||
return func(f *Forwarder) error {
|
||||
f.stateListener = stateListener
|
||||
return nil
|
||||
@@ -157,7 +157,7 @@ func StreamingFlushInterval(flushInterval time.Duration) optSetter {
|
||||
type Forwarder struct {
|
||||
*httpForwarder
|
||||
*handlerContext
|
||||
stateListener UrlForwardingStateListener
|
||||
stateListener URLForwardingStateListener
|
||||
stream bool
|
||||
}
|
||||
|
||||
@@ -166,8 +166,7 @@ type handlerContext struct {
|
||||
errHandler utils.ErrorHandler
|
||||
}
|
||||
|
||||
// httpForwarder is a handler that can reverse proxy
|
||||
// HTTP traffic.
|
||||
// httpForwarder is a handler that can reverse proxy HTTP traffic.
|
||||
type httpForwarder struct {
|
||||
roundTripper http.RoundTripper
|
||||
rewriter ReqRewriter
|
||||
@@ -191,8 +190,12 @@ const (
|
||||
StateDisconnected
|
||||
)
|
||||
|
||||
// UrlForwardingStateListener URL forwarding state listener.
|
||||
type UrlForwardingStateListener func(*url.URL, int)
|
||||
// UrlForwardingStateListener alias on URLForwardingStateListener.
|
||||
// Deprecated: use URLForwardingStateListener instead.
|
||||
type UrlForwardingStateListener = URLForwardingStateListener
|
||||
|
||||
// URLForwardingStateListener URL forwarding state listener.
|
||||
type URLForwardingStateListener func(*url.URL, int)
|
||||
|
||||
// New creates an instance of Forwarder based on the provided list of configuration options.
|
||||
func New(setters ...optSetter) (*Forwarder, error) {
|
||||
@@ -243,7 +246,7 @@ func New(setters ...optSetter) (*Forwarder, error) {
|
||||
// request and delegates to the proper implementation.
|
||||
func (f *Forwarder) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
if f.log.GetLevel() >= log.DebugLevel {
|
||||
logEntry := f.log.WithField("Request", utils.DumpHttpRequest(req))
|
||||
logEntry := f.log.WithField("Request", utils.DumpHTTPRequest(req))
|
||||
logEntry.Debug("vulcand/oxy/forward: begin ServeHttp on request")
|
||||
defer logEntry.Debug("vulcand/oxy/forward: completed ServeHttp on request")
|
||||
}
|
||||
@@ -259,7 +262,7 @@ func (f *Forwarder) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func (f *httpForwarder) getUrlFromRequest(req *http.Request) *url.URL {
|
||||
func (f *httpForwarder) getURLFromRequest(req *http.Request) *url.URL {
|
||||
// If the Request was created by Go via a real HTTP request, RequestURI will
|
||||
// contain the original query string. If the Request was created in code, RequestURI
|
||||
// will be empty, and we will use the URL object instead
|
||||
@@ -281,7 +284,7 @@ func (f *httpForwarder) modifyRequest(outReq *http.Request, target *url.URL) {
|
||||
outReq.URL.Scheme = target.Scheme
|
||||
outReq.URL.Host = target.Host
|
||||
|
||||
u := f.getUrlFromRequest(outReq)
|
||||
u := f.getURLFromRequest(outReq)
|
||||
|
||||
outReq.URL.Path = u.Path
|
||||
outReq.URL.RawPath = u.RawPath
|
||||
@@ -305,7 +308,7 @@ func (f *httpForwarder) modifyRequest(outReq *http.Request, target *url.URL) {
|
||||
// serveWebSocket forwards websocket traffic.
|
||||
func (f *httpForwarder) serveWebSocket(w http.ResponseWriter, req *http.Request, ctx *handlerContext) {
|
||||
if f.log.GetLevel() >= log.DebugLevel {
|
||||
logEntry := f.log.WithField("Request", utils.DumpHttpRequest(req))
|
||||
logEntry := f.log.WithField("Request", utils.DumpHTTPRequest(req))
|
||||
logEntry.Debug("vulcand/oxy/forward/websocket: begin ServeHttp on request")
|
||||
defer logEntry.Debug("vulcand/oxy/forward/websocket: completed ServeHttp on request")
|
||||
}
|
||||
@@ -341,7 +344,7 @@ func (f *httpForwarder) serveWebSocket(w http.ResponseWriter, req *http.Request,
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
conn.Close()
|
||||
_ = conn.Close()
|
||||
if f.websocketConnectionClosedHook != nil {
|
||||
f.websocketConnectionClosedHook(req, conn)
|
||||
}
|
||||
@@ -371,8 +374,8 @@ func (f *httpForwarder) serveWebSocket(w http.ResponseWriter, req *http.Request,
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
underlyingConn.Close()
|
||||
targetConn.Close()
|
||||
_ = underlyingConn.Close()
|
||||
_ = targetConn.Close()
|
||||
if f.websocketConnectionClosedHook != nil {
|
||||
f.websocketConnectionClosedHook(req, underlyingConn.UnderlyingConn())
|
||||
}
|
||||
@@ -461,7 +464,7 @@ func (f *httpForwarder) copyWebSocketRequest(req *http.Request) (outReq *http.Re
|
||||
outReq.URL.Scheme = "ws"
|
||||
}
|
||||
|
||||
u := f.getUrlFromRequest(outReq)
|
||||
u := f.getURLFromRequest(outReq)
|
||||
|
||||
outReq.URL.Path = u.Path
|
||||
outReq.URL.RawPath = u.RawPath
|
||||
@@ -488,7 +491,7 @@ func (f *httpForwarder) copyWebSocketRequest(req *http.Request) (outReq *http.Re
|
||||
// serveHTTP forwards HTTP traffic using the configured transport.
|
||||
func (f *httpForwarder) serveHTTP(w http.ResponseWriter, inReq *http.Request, ctx *handlerContext) {
|
||||
if f.log.GetLevel() >= log.DebugLevel {
|
||||
logEntry := f.log.WithField("Request", utils.DumpHttpRequest(inReq))
|
||||
logEntry := f.log.WithField("Request", utils.DumpHTTPRequest(inReq))
|
||||
logEntry.Debug("vulcand/oxy/forward/http: begin ServeHttp on request")
|
||||
defer logEntry.Debug("vulcand/oxy/forward/http: completed ServeHttp on request")
|
||||
}
|
||||
|
||||
+17
-13
@@ -115,22 +115,22 @@ func TestXForwardedHostHeader(t *testing.T) {
|
||||
tests := []struct {
|
||||
Description string
|
||||
PassHostHeader bool
|
||||
TargetUrl string
|
||||
ProxyfiedUrl string
|
||||
TargetURL string
|
||||
ProxyfiedURL string
|
||||
ExpectedXForwardedHost string
|
||||
}{
|
||||
{
|
||||
Description: "XForwardedHost without PassHostHeader",
|
||||
PassHostHeader: false,
|
||||
TargetUrl: "http://xforwardedhost.com",
|
||||
ProxyfiedUrl: "http://backend.com",
|
||||
TargetURL: "http://xforwardedhost.com",
|
||||
ProxyfiedURL: "http://backend.com",
|
||||
ExpectedXForwardedHost: "xforwardedhost.com",
|
||||
},
|
||||
{
|
||||
Description: "XForwardedHost with PassHostHeader",
|
||||
PassHostHeader: true,
|
||||
TargetUrl: "http://xforwardedhost.com",
|
||||
ProxyfiedUrl: "http://backend.com",
|
||||
TargetURL: "http://xforwardedhost.com",
|
||||
ProxyfiedURL: "http://backend.com",
|
||||
ExpectedXForwardedHost: "xforwardedhost.com",
|
||||
},
|
||||
}
|
||||
@@ -143,11 +143,11 @@ func TestXForwardedHostHeader(t *testing.T) {
|
||||
f, err := New(PassHostHeader(test.PassHostHeader))
|
||||
require.NoError(t, err)
|
||||
|
||||
r, err := http.NewRequest(http.MethodGet, test.TargetUrl, nil)
|
||||
r, err := http.NewRequest(http.MethodGet, test.TargetURL, nil)
|
||||
require.NoError(t, err)
|
||||
backendUrl, err := url.Parse(test.ProxyfiedUrl)
|
||||
backendURL, err := url.Parse(test.ProxyfiedURL)
|
||||
require.NoError(t, err)
|
||||
f.modifyRequest(r, backendUrl)
|
||||
f.modifyRequest(r, backendURL)
|
||||
require.Equal(t, test.ExpectedXForwardedHost, r.Header.Get(XForwardedHost))
|
||||
})
|
||||
}
|
||||
@@ -331,12 +331,14 @@ func TestForwardedProto(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContextWithValueInErrHandler(t *testing.T) {
|
||||
var originalPBool *bool
|
||||
originalBool := false
|
||||
originalPBool = &originalBool
|
||||
originalPBool := &originalBool
|
||||
|
||||
type MyKey string
|
||||
const key MyKey = "test"
|
||||
|
||||
f, err := New(ErrorHandler(utils.ErrorHandlerFunc(func(rw http.ResponseWriter, req *http.Request, err error) {
|
||||
test, isBool := req.Context().Value("test").(*bool)
|
||||
test, isBool := req.Context().Value(key).(*bool)
|
||||
if isBool {
|
||||
*test = true
|
||||
}
|
||||
@@ -349,13 +351,15 @@ func TestContextWithValueInErrHandler(t *testing.T) {
|
||||
proxy := testutils.NewHandler(func(w http.ResponseWriter, req *http.Request) {
|
||||
// We need a network error
|
||||
req.URL = testutils.ParseURI("http://localhost:63450")
|
||||
newReq := req.WithContext(context.WithValue(req.Context(), "test", originalPBool))
|
||||
newReq := req.WithContext(context.WithValue(req.Context(), key, originalPBool))
|
||||
|
||||
f.ServeHTTP(w, newReq)
|
||||
})
|
||||
defer proxy.Close()
|
||||
|
||||
re, _, err := testutils.Get(proxy.URL)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, http.StatusBadGateway, re.StatusCode)
|
||||
assert.True(t, *originalPBool)
|
||||
}
|
||||
|
||||
@@ -151,7 +151,7 @@ func (rb *Rebalancer) Servers() []*url.URL {
|
||||
|
||||
func (rb *Rebalancer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
if rb.log.Level >= log.DebugLevel {
|
||||
logEntry := rb.log.WithField("Request", utils.DumpHttpRequest(req))
|
||||
logEntry := rb.log.WithField("Request", utils.DumpHTTPRequest(req))
|
||||
logEntry.Debug("vulcand/oxy/roundrobin/rebalancer: begin ServeHttp on request")
|
||||
defer logEntry.Debug("vulcand/oxy/roundrobin/rebalancer: completed ServeHttp on request")
|
||||
}
|
||||
@@ -184,7 +184,7 @@ func (rb *Rebalancer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
if log.GetLevel() >= log.DebugLevel {
|
||||
// log which backend URL we're sending this request to
|
||||
log.WithFields(log.Fields{"Request": utils.DumpHttpRequest(req), "ForwardURL": fwdURL}).Debugf("vulcand/oxy/roundrobin/rebalancer: Forwarding this request to URL")
|
||||
log.WithFields(log.Fields{"Request": utils.DumpHTTPRequest(req), "ForwardURL": fwdURL}).Debugf("vulcand/oxy/roundrobin/rebalancer: Forwarding this request to URL")
|
||||
}
|
||||
|
||||
if rb.stickySession != nil {
|
||||
|
||||
+20
-12
@@ -46,6 +46,24 @@ func RoundRobinRequestRewriteListener(rrl RequestRewriteListener) LBOption {
|
||||
}
|
||||
}
|
||||
|
||||
// RoundRobinLogger defines the logger the round robin load balancer will use.
|
||||
//
|
||||
// It defaults to logrus.StandardLogger(), the global logger used by logrus.
|
||||
// Deprecated: use Logger instead.
|
||||
func RoundRobinLogger(l *log.Logger) LBOption {
|
||||
return Logger(l)
|
||||
}
|
||||
|
||||
// Logger defines the logger the round robin load balancer will use.
|
||||
//
|
||||
// It defaults to logrus.StandardLogger(), the global logger used by logrus.
|
||||
func Logger(l *log.Logger) LBOption {
|
||||
return func(r *RoundRobin) error {
|
||||
r.log = l
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// RoundRobin implements dynamic weighted round robin load balancer http handler.
|
||||
type RoundRobin struct {
|
||||
mutex *sync.Mutex
|
||||
@@ -83,16 +101,6 @@ func New(next http.Handler, opts ...LBOption) (*RoundRobin, error) {
|
||||
return rr, nil
|
||||
}
|
||||
|
||||
// RoundRobinLogger defines the logger the round robin load balancer will use.
|
||||
//
|
||||
// It defaults to logrus.StandardLogger(), the global logger used by logrus.
|
||||
func RoundRobinLogger(l *log.Logger) LBOption {
|
||||
return func(r *RoundRobin) error {
|
||||
r.log = l
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Next returns the next handler.
|
||||
func (r *RoundRobin) Next() http.Handler {
|
||||
return r.next
|
||||
@@ -100,7 +108,7 @@ func (r *RoundRobin) Next() http.Handler {
|
||||
|
||||
func (r *RoundRobin) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
if r.log.Level >= log.DebugLevel {
|
||||
logEntry := r.log.WithField("Request", utils.DumpHttpRequest(req))
|
||||
logEntry := r.log.WithField("Request", utils.DumpHTTPRequest(req))
|
||||
logEntry.Debug("vulcand/oxy/roundrobin/rr: begin ServeHttp on request")
|
||||
defer logEntry.Debug("vulcand/oxy/roundrobin/rr: completed ServeHttp on request")
|
||||
}
|
||||
@@ -135,7 +143,7 @@ func (r *RoundRobin) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
if r.log.Level >= log.DebugLevel {
|
||||
// log which backend URL we're sending this request to
|
||||
r.log.WithFields(log.Fields{"Request": utils.DumpHttpRequest(req), "ForwardURL": newReq.URL}).Debugf("vulcand/oxy/roundrobin/rr: Forwarding this request to URL")
|
||||
r.log.WithFields(log.Fields{"Request": utils.DumpHTTPRequest(req), "ForwardURL": newReq.URL}).Debugf("vulcand/oxy/roundrobin/rr: Forwarding this request to URL")
|
||||
}
|
||||
|
||||
// Emit event to a listener if one exists
|
||||
|
||||
+1
-1
@@ -94,7 +94,7 @@ func (s *Stream) Wrap(next http.Handler) error {
|
||||
|
||||
func (s *Stream) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
if s.log.Level >= log.DebugLevel {
|
||||
logEntry := s.log.WithField("Request", utils.DumpHttpRequest(req))
|
||||
logEntry := s.log.WithField("Request", utils.DumpHTTPRequest(req))
|
||||
logEntry.Debug("vulcand/oxy/stream: begin ServeHttp on request")
|
||||
defer logEntry.Debug("vulcand/oxy/stream: completed ServeHttp on request")
|
||||
}
|
||||
|
||||
+23
-7
@@ -9,8 +9,12 @@ import (
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// SerializableHttpRequest serializable HTTP request.
|
||||
type SerializableHttpRequest struct {
|
||||
// SerializableHttpRequest alias on SerializableHTTPRequest.
|
||||
// Deprecated: use SerializableHTTPRequest instead.
|
||||
type SerializableHttpRequest = SerializableHTTPRequest
|
||||
|
||||
// SerializableHTTPRequest serializable HTTP request.
|
||||
type SerializableHTTPRequest struct {
|
||||
Method string
|
||||
URL *url.URL
|
||||
Proto string // "HTTP/1.0"
|
||||
@@ -30,12 +34,12 @@ type SerializableHttpRequest struct {
|
||||
}
|
||||
|
||||
// Clone clone a request.
|
||||
func Clone(r *http.Request) *SerializableHttpRequest {
|
||||
func Clone(r *http.Request) *SerializableHTTPRequest {
|
||||
if r == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
rc := new(SerializableHttpRequest)
|
||||
rc := new(SerializableHTTPRequest)
|
||||
rc.Method = r.Method
|
||||
rc.URL = r.URL
|
||||
rc.Proto = r.Proto
|
||||
@@ -50,15 +54,27 @@ func Clone(r *http.Request) *SerializableHttpRequest {
|
||||
}
|
||||
|
||||
// ToJson serializes to JSON.
|
||||
func (s *SerializableHttpRequest) ToJson() string {
|
||||
// Deprecated: use ToJSON instead.
|
||||
func (s *SerializableHTTPRequest) ToJson() string {
|
||||
return s.ToJSON()
|
||||
}
|
||||
|
||||
// ToJSON serializes to JSON.
|
||||
func (s *SerializableHTTPRequest) ToJSON() string {
|
||||
jsonVal, err := json.Marshal(s)
|
||||
if err != nil || jsonVal == nil {
|
||||
return fmt.Sprintf("Error marshalling SerializableHttpRequest to json: %s", err)
|
||||
return fmt.Sprintf("Error marshalling SerializableHTTPRequest to json: %s", err)
|
||||
}
|
||||
return string(jsonVal)
|
||||
}
|
||||
|
||||
// DumpHttpRequest dump a HTTP request to JSON.
|
||||
// Deprecated: use DumpHTTPRequest instead.
|
||||
func DumpHttpRequest(req *http.Request) string {
|
||||
return Clone(req).ToJson()
|
||||
return DumpHTTPRequest(req)
|
||||
}
|
||||
|
||||
// DumpHTTPRequest dump a HTTP request to JSON.
|
||||
func DumpHTTPRequest(req *http.Request) string {
|
||||
return Clone(req).ToJSON()
|
||||
}
|
||||
|
||||
@@ -28,5 +28,5 @@ func TestHttpReqToString(t *testing.T) {
|
||||
Body: &readCloserTestImpl{},
|
||||
}
|
||||
|
||||
assert.True(t, len(DumpHttpRequest(req)) > 0)
|
||||
assert.True(t, len(DumpHTTPRequest(req)) > 0)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user