2016-02-20 21:05:48 +00:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
2016-08-21 15:46:23 +00:00
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/options"
|
2016-02-20 21:05:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config contains all configuration necessary to connect to a REST server.
|
|
|
|
type Config struct {
|
2017-06-05 22:25:22 +00:00
|
|
|
URL *url.URL
|
2017-06-11 11:46:54 +00:00
|
|
|
Connections uint `option:"connections" help:"set a limit for the number of concurrent connections (default: 5)"`
|
2017-06-05 22:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
options.Register("rest", Config{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewConfig returns a new Config with the default values filled in.
|
|
|
|
func NewConfig() Config {
|
|
|
|
return Config{
|
2017-06-11 11:46:54 +00:00
|
|
|
Connections: 5,
|
2017-06-05 22:25:22 +00:00
|
|
|
}
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseConfig parses the string s and extracts the REST server URL.
|
2023-04-21 19:35:34 +00:00
|
|
|
func ParseConfig(s string) (*Config, error) {
|
2016-02-20 21:05:48 +00:00
|
|
|
if !strings.HasPrefix(s, "rest:") {
|
2023-04-21 19:35:34 +00:00
|
|
|
return nil, errors.New("invalid REST backend specification")
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 22:52:27 +00:00
|
|
|
s = prepareURL(s)
|
2018-03-15 20:37:18 +00:00
|
|
|
|
2016-02-20 21:05:48 +00:00
|
|
|
u, err := url.Parse(s)
|
|
|
|
if err != nil {
|
2023-04-21 19:35:34 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 22:25:22 +00:00
|
|
|
cfg := NewConfig()
|
|
|
|
cfg.URL = u
|
2023-04-21 19:35:34 +00:00
|
|
|
return &cfg, nil
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
2020-03-20 22:52:27 +00:00
|
|
|
|
|
|
|
// StripPassword removes the password from the URL
|
|
|
|
// If the repository location cannot be parsed as a valid URL, it will be returned as is
|
|
|
|
// (it's because this function is used for logging errors)
|
|
|
|
func StripPassword(s string) string {
|
|
|
|
scheme := s[:5]
|
|
|
|
s = prepareURL(s)
|
|
|
|
|
|
|
|
u, err := url.Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
return scheme + s
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, set := u.User.Password(); !set {
|
|
|
|
return scheme + s
|
|
|
|
}
|
|
|
|
|
|
|
|
// a password was set: we replace it with ***
|
|
|
|
return scheme + strings.Replace(u.String(), u.User.String()+"@", u.User.Username()+":***@", 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareURL(s string) string {
|
|
|
|
s = s[5:]
|
|
|
|
if !strings.HasSuffix(s, "/") {
|
|
|
|
s += "/"
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|