diff --git a/cmd/serve/http/http.go b/cmd/serve/http/http.go index 594ac551c..d88714ddd 100644 --- a/cmd/serve/http/http.go +++ b/cmd/serve/http/http.go @@ -29,14 +29,14 @@ import ( // Options required for http server type Options struct { Auth libhttp.AuthConfig - HTTP libhttp.HTTPConfig + HTTP libhttp.Config Template libhttp.TemplateConfig } // DefaultOpt is the default values used for Options var DefaultOpt = Options{ Auth: libhttp.DefaultAuthCfg(), - HTTP: libhttp.DefaultHTTPCfg(), + HTTP: libhttp.DefaultCfg(), Template: libhttp.DefaultTemplateCfg(), } diff --git a/cmd/serve/http/http_test.go b/cmd/serve/http/http_test.go index 9f264adce..f285c1261 100644 --- a/cmd/serve/http/http_test.go +++ b/cmd/serve/http/http_test.go @@ -34,7 +34,7 @@ func start(t *testing.T, f fs.Fs) { ctx := context.Background() opts := Options{ - HTTP: libhttp.DefaultHTTPCfg(), + HTTP: libhttp.DefaultCfg(), Template: libhttp.TemplateConfig{ Path: testTemplate, }, diff --git a/lib/http/auth.go b/lib/http/auth.go index b7784c4b4..50b024f70 100644 --- a/lib/http/auth.go +++ b/lib/http/auth.go @@ -5,8 +5,7 @@ import ( "github.com/spf13/pflag" ) -// Help contains text describing the http authentication to add to the command -// help. +// AuthHelp contains text describing the http authentication to add to the command help. var AuthHelp = ` #### Authentication diff --git a/lib/http/middleware.go b/lib/http/middleware.go index d19d75259..cef2485d6 100644 --- a/lib/http/middleware.go +++ b/lib/http/middleware.go @@ -33,6 +33,7 @@ func parseAuthorization(r *http.Request) (user, pass string, ok bool) { return } +// LoggedBasicAuth simply wraps the goauth.BasicAuth struct type LoggedBasicAuth struct { goauth.BasicAuth } diff --git a/lib/http/middleware_test.go b/lib/http/middleware_test.go index 4839b87e3..8932ca4fd 100644 --- a/lib/http/middleware_test.go +++ b/lib/http/middleware_test.go @@ -13,14 +13,14 @@ import ( func TestMiddlewareAuth(t *testing.T) { servers := []struct { name string - http HTTPConfig + http Config auth AuthConfig user string pass string }{ { name: "Basic", - http: HTTPConfig{ + http: Config{ ListenAddr: []string{"127.0.0.1:0"}, }, auth: AuthConfig{ @@ -33,7 +33,7 @@ func TestMiddlewareAuth(t *testing.T) { }, { name: "Htpasswd/MD5", - http: HTTPConfig{ + http: Config{ ListenAddr: []string{"127.0.0.1:0"}, }, auth: AuthConfig{ @@ -45,7 +45,7 @@ func TestMiddlewareAuth(t *testing.T) { }, { name: "Htpasswd/SHA", - http: HTTPConfig{ + http: Config{ ListenAddr: []string{"127.0.0.1:0"}, }, auth: AuthConfig{ @@ -57,7 +57,7 @@ func TestMiddlewareAuth(t *testing.T) { }, { name: "Htpasswd/Bcrypt", - http: HTTPConfig{ + http: Config{ ListenAddr: []string{"127.0.0.1:0"}, }, auth: AuthConfig{ @@ -69,7 +69,7 @@ func TestMiddlewareAuth(t *testing.T) { }, { name: "Custom", - http: HTTPConfig{ + http: Config{ ListenAddr: []string{"127.0.0.1:0"}, }, auth: AuthConfig{ @@ -168,19 +168,19 @@ var _testCORSHeaderKeys = []string{ func TestMiddlewareCORS(t *testing.T) { servers := []struct { name string - http HTTPConfig + http Config origin string }{ { name: "EmptyOrigin", - http: HTTPConfig{ + http: Config{ ListenAddr: []string{"127.0.0.1:0"}, }, origin: "", }, { name: "CustomOrigin", - http: HTTPConfig{ + http: Config{ ListenAddr: []string{"127.0.0.1:0"}, }, origin: "http://test.rclone.org", diff --git a/lib/http/server.go b/lib/http/server.go index d4e5e827b..4dbc31c8e 100644 --- a/lib/http/server.go +++ b/lib/http/server.go @@ -76,8 +76,8 @@ certificate authority certificate. // Middleware function signature required by chi.Router.Use() type Middleware func(http.Handler) http.Handler -// HTTPConfig contains options for the http Server -type HTTPConfig struct { +// Config contains options for the http Server +type Config struct { ListenAddr []string // Port to listen on BaseURL string // prefix to strip from URLs ServerReadTimeout time.Duration // Timeout for server reading data @@ -93,7 +93,7 @@ type HTTPConfig struct { } // AddFlagsPrefix adds flags for the httplib -func (cfg *HTTPConfig) AddFlagsPrefix(flagSet *pflag.FlagSet, prefix string) { +func (cfg *Config) AddFlagsPrefix(flagSet *pflag.FlagSet, prefix string) { flags.StringArrayVarP(flagSet, &cfg.ListenAddr, prefix+"addr", "", cfg.ListenAddr, "IPaddress:Port or :Port to bind server to") flags.DurationVarP(flagSet, &cfg.ServerReadTimeout, prefix+"server-read-timeout", "", cfg.ServerReadTimeout, "Timeout for server reading data") flags.DurationVarP(flagSet, &cfg.ServerWriteTimeout, prefix+"server-write-timeout", "", cfg.ServerWriteTimeout, "Timeout for server writing data") @@ -106,13 +106,13 @@ func (cfg *HTTPConfig) AddFlagsPrefix(flagSet *pflag.FlagSet, prefix string) { } // AddHTTPFlagsPrefix adds flags for the httplib -func AddHTTPFlagsPrefix(flagSet *pflag.FlagSet, prefix string, cfg *HTTPConfig) { +func AddHTTPFlagsPrefix(flagSet *pflag.FlagSet, prefix string, cfg *Config) { cfg.AddFlagsPrefix(flagSet, prefix) } -// DefaultHTTPCfg is the default values used for Config -func DefaultHTTPCfg() HTTPConfig { - return HTTPConfig{ +// DefaultCfg is the default values used for Config +func DefaultCfg() Config { + return Config{ ListenAddr: []string{"127.0.0.1:8080"}, ServerReadTimeout: 1 * time.Hour, ServerWriteTimeout: 1 * time.Hour, @@ -151,7 +151,7 @@ type server struct { tlsConfig *tls.Config instances []instance auth AuthConfig - cfg HTTPConfig + cfg Config template *TemplateConfig htmlTemplate *template.Template } @@ -166,8 +166,8 @@ func WithAuth(cfg AuthConfig) Option { } } -// WithConfig option applies the HTTPConfig to the server, overriding defaults -func WithConfig(cfg HTTPConfig) Option { +// WithConfig option applies the Config to the server, overriding defaults +func WithConfig(cfg Config) Option { return func(s *server) { s.cfg = cfg } @@ -187,7 +187,7 @@ func WithTemplate(cfg TemplateConfig) Option { func NewServer(ctx context.Context, options ...Option) (*server, error) { s := &server{ mux: chi.NewRouter(), - cfg: DefaultHTTPCfg(), + cfg: DefaultCfg(), } for _, opt := range options { @@ -307,11 +307,14 @@ func (s *server) initTemplate() error { } var ( - // hard coded errors, allowing for easier testing + // ErrInvalidMinTLSVersion - hard coded errors, allowing for easier testing ErrInvalidMinTLSVersion = errors.New("invalid value for --min-tls-version") - ErrTLSBodyMismatch = errors.New("need both TLSCertBody and TLSKeyBody to use TLS") - ErrTLSFileMismatch = errors.New("need both --cert and --key to use TLS") - ErrTLSParseCA = errors.New("unable to parse client certificate authority") + // ErrTLSBodyMismatch - hard coded errors, allowing for easier testing + ErrTLSBodyMismatch = errors.New("need both TLSCertBody and TLSKeyBody to use TLS") + // ErrTLSFileMismatch - hard coded errors, allowing for easier testing + ErrTLSFileMismatch = errors.New("need both --cert and --key to use TLS") + // ErrTLSParseCA - hard coded errors, allowing for easier testing + ErrTLSParseCA = errors.New("unable to parse client certificate authority") ) func (s *server) initTLS() error { diff --git a/lib/http/server_test.go b/lib/http/server_test.go index 884214f43..42c630ea4 100644 --- a/lib/http/server_test.go +++ b/lib/http/server_test.go @@ -54,7 +54,7 @@ func TestNewServerUnix(t *testing.T) { tempDir := t.TempDir() path := filepath.Join(tempDir, "rclone.sock") - cfg := DefaultHTTPCfg() + cfg := DefaultCfg() cfg.ListenAddr = []string{path} auth := AuthConfig{ @@ -97,7 +97,7 @@ func TestNewServerUnix(t *testing.T) { func TestNewServerHTTP(t *testing.T) { ctx := context.Background() - cfg := DefaultHTTPCfg() + cfg := DefaultCfg() cfg.ListenAddr = []string{"127.0.0.1:0"} auth := AuthConfig{ @@ -153,12 +153,12 @@ func TestNewServerHTTP(t *testing.T) { func TestNewServerBaseURL(t *testing.T) { servers := []struct { name string - http HTTPConfig + cfg Config suffix string }{ { name: "Empty", - http: HTTPConfig{ + cfg: Config{ ListenAddr: []string{"127.0.0.1:0"}, BaseURL: "", }, @@ -166,7 +166,7 @@ func TestNewServerBaseURL(t *testing.T) { }, { name: "Single/NoTrailingSlash", - http: HTTPConfig{ + cfg: Config{ ListenAddr: []string{"127.0.0.1:0"}, BaseURL: "/rclone", }, @@ -174,7 +174,7 @@ func TestNewServerBaseURL(t *testing.T) { }, { name: "Single/TrailingSlash", - http: HTTPConfig{ + cfg: Config{ ListenAddr: []string{"127.0.0.1:0"}, BaseURL: "/rclone/", }, @@ -182,7 +182,7 @@ func TestNewServerBaseURL(t *testing.T) { }, { name: "Multi/NoTrailingSlash", - http: HTTPConfig{ + cfg: Config{ ListenAddr: []string{"127.0.0.1:0"}, BaseURL: "/rclone/test/base/url", }, @@ -190,7 +190,7 @@ func TestNewServerBaseURL(t *testing.T) { }, { name: "Multi/TrailingSlash", - http: HTTPConfig{ + cfg: Config{ ListenAddr: []string{"127.0.0.1:0"}, BaseURL: "/rclone/test/base/url/", }, @@ -200,7 +200,7 @@ func TestNewServerBaseURL(t *testing.T) { for _, ss := range servers { t.Run(ss.name, func(t *testing.T) { - s, err := NewServer(context.Background(), WithConfig(ss.http)) + s, err := NewServer(context.Background(), WithConfig(ss.cfg)) require.NoError(t, err) defer func() { require.NoError(t, s.Shutdown()) @@ -246,11 +246,11 @@ func TestNewServerTLS(t *testing.T) { name string wantErr bool err error - http HTTPConfig + http Config }{ { name: "FromFile/Valid", - http: HTTPConfig{ + http: Config{ ListenAddr: []string{"127.0.0.1:0"}, TLSCert: "./testdata/local.crt", TLSKey: "./testdata/local.key", @@ -261,7 +261,7 @@ func TestNewServerTLS(t *testing.T) { name: "FromFile/NoCert", wantErr: true, err: ErrTLSFileMismatch, - http: HTTPConfig{ + http: Config{ ListenAddr: []string{"127.0.0.1:0"}, TLSCert: "", TLSKey: "./testdata/local.key", @@ -271,7 +271,7 @@ func TestNewServerTLS(t *testing.T) { { name: "FromFile/InvalidCert", wantErr: true, - http: HTTPConfig{ + http: Config{ ListenAddr: []string{"127.0.0.1:0"}, TLSCert: "./testdata/local.crt.invalid", TLSKey: "./testdata/local.key", @@ -282,7 +282,7 @@ func TestNewServerTLS(t *testing.T) { name: "FromFile/NoKey", wantErr: true, err: ErrTLSFileMismatch, - http: HTTPConfig{ + http: Config{ ListenAddr: []string{"127.0.0.1:0"}, TLSCert: "./testdata/local.crt", TLSKey: "", @@ -292,7 +292,7 @@ func TestNewServerTLS(t *testing.T) { { name: "FromFile/InvalidKey", wantErr: true, - http: HTTPConfig{ + http: Config{ ListenAddr: []string{"127.0.0.1:0"}, TLSCert: "./testdata/local.crt", TLSKey: "./testdata/local.key.invalid", @@ -301,7 +301,7 @@ func TestNewServerTLS(t *testing.T) { }, { name: "FromBody/Valid", - http: HTTPConfig{ + http: Config{ ListenAddr: []string{"127.0.0.1:0"}, TLSCertBody: certBytes, TLSKeyBody: keyBytes, @@ -312,7 +312,7 @@ func TestNewServerTLS(t *testing.T) { name: "FromBody/NoCert", wantErr: true, err: ErrTLSBodyMismatch, - http: HTTPConfig{ + http: Config{ ListenAddr: []string{"127.0.0.1:0"}, TLSCertBody: nil, TLSKeyBody: keyBytes, @@ -322,7 +322,7 @@ func TestNewServerTLS(t *testing.T) { { name: "FromBody/InvalidCert", wantErr: true, - http: HTTPConfig{ + http: Config{ ListenAddr: []string{"127.0.0.1:0"}, TLSCertBody: []byte("JUNK DATA"), TLSKeyBody: keyBytes, @@ -333,7 +333,7 @@ func TestNewServerTLS(t *testing.T) { name: "FromBody/NoKey", wantErr: true, err: ErrTLSBodyMismatch, - http: HTTPConfig{ + http: Config{ ListenAddr: []string{"127.0.0.1:0"}, TLSCertBody: certBytes, TLSKeyBody: nil, @@ -343,7 +343,7 @@ func TestNewServerTLS(t *testing.T) { { name: "FromBody/InvalidKey", wantErr: true, - http: HTTPConfig{ + http: Config{ ListenAddr: []string{"127.0.0.1:0"}, TLSCertBody: certBytes, TLSKeyBody: []byte("JUNK DATA"), @@ -352,7 +352,7 @@ func TestNewServerTLS(t *testing.T) { }, { name: "MinTLSVersion/Valid/1.1", - http: HTTPConfig{ + http: Config{ ListenAddr: []string{"127.0.0.1:0"}, TLSCertBody: certBytes, TLSKeyBody: keyBytes, @@ -361,7 +361,7 @@ func TestNewServerTLS(t *testing.T) { }, { name: "MinTLSVersion/Valid/1.2", - http: HTTPConfig{ + http: Config{ ListenAddr: []string{"127.0.0.1:0"}, TLSCertBody: certBytes, TLSKeyBody: keyBytes, @@ -370,7 +370,7 @@ func TestNewServerTLS(t *testing.T) { }, { name: "MinTLSVersion/Valid/1.3", - http: HTTPConfig{ + http: Config{ ListenAddr: []string{"127.0.0.1:0"}, TLSCertBody: certBytes, TLSKeyBody: keyBytes, @@ -381,7 +381,7 @@ func TestNewServerTLS(t *testing.T) { name: "MinTLSVersion/Invalid", wantErr: true, err: ErrInvalidMinTLSVersion, - http: HTTPConfig{ + http: Config{ ListenAddr: []string{"127.0.0.1:0"}, TLSCertBody: certBytes, TLSKeyBody: keyBytes,