aboutsummaryrefslogtreecommitdiff
path: root/plugins/carddav
diff options
context:
space:
mode:
authorSimon Ser <contact@emersion.fr>2020-02-05 13:52:52 +0100
committerSimon Ser <contact@emersion.fr>2020-02-05 13:52:52 +0100
commitad256906e0f9f23d97bec268b980154726621e8a (patch)
tree1c547347fefaa391d28ae15a0d4fe49b17f0487f /plugins/carddav
parent6a6ece03e49f29de32c8e6d598871de764e07232 (diff)
downloadalps-ad256906e0f9f23d97bec268b980154726621e8a.tar.gz
alps-ad256906e0f9f23d97bec268b980154726621e8a.zip
plugins/carddav: sanity check URL on startup
Diffstat (limited to 'plugins/carddav')
-rw-r--r--plugins/carddav/plugin.go24
1 files changed, 24 insertions, 0 deletions
diff --git a/plugins/carddav/plugin.go b/plugins/carddav/plugin.go
index 45e447e..5f51069 100644
--- a/plugins/carddav/plugin.go
+++ b/plugins/carddav/plugin.go
@@ -11,6 +11,25 @@ import (
"github.com/emersion/go-webdav/carddav"
)
+func sanityCheckURL(u *url.URL) error {
+ req, err := http.NewRequest(http.MethodOptions, u.String(), nil)
+ if err != nil {
+ return err
+ }
+
+ resp, err := http.DefaultClient.Do(req)
+ if err != nil {
+ return err
+ }
+ resp.Body.Close()
+
+ // Servers might require authentication to perform an OPTIONS request
+ if resp.StatusCode/100 != 2 && resp.StatusCode != http.StatusUnauthorized {
+ return fmt.Errorf("HTTP request failed: %v %v", resp.StatusCode, resp.Status)
+ }
+ return nil
+}
+
type authRoundTripper struct {
upstream http.RoundTripper
session *koushin.Session
@@ -43,6 +62,11 @@ func newPlugin(srv *koushin.Server) (koushin.Plugin, error) {
return nil, fmt.Errorf("carddav: Discover returned an invalid URL: %v", err)
}
}
+
+ if err := sanityCheckURL(u); err != nil {
+ return nil, fmt.Errorf("carddav: failed to connect to CardDAV server %q: %v", u, err)
+ }
+
srv.Logger().Printf("Configured upstream CardDAV server: %v", u)
p := koushin.GoPlugin{Name: "carddav"}