-
Notifications
You must be signed in to change notification settings - Fork 105
Improve service discovery, implement SD for CalDav #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,13 +6,99 @@ import ( | |
| "fmt" | ||
| "io" | ||
| "mime" | ||
| "net" | ||
| "net/http" | ||
| "net/url" | ||
| "path" | ||
| "strings" | ||
| "unicode" | ||
| ) | ||
|
|
||
| // Discover performs a DNS-based CalDAV/CardDAV service discovery as described | ||
| // in RFC 6764 section 6. It returns the URL to the CalDAV/CardDAV server. | ||
| func Discover(service string, host string) (string, error) { | ||
| if service != "caldav" && service != "carddav" { | ||
| return "", fmt.Errorf("webdav: service discovery of type %v not supported", service) | ||
| } | ||
|
|
||
| path := "" | ||
|
|
||
| // Check for SRV records for the service we want, only lookup secure versions | ||
| // (caldavs, carddavs), plaintext connections are insecure | ||
| _, addrs, err := net.LookupSRV(fmt.Sprintf("%vs", service), "tcp", host) | ||
| if dnsErr, ok := err.(*net.DNSError); ok { | ||
| if dnsErr.IsTemporary { | ||
| return "", err | ||
| } | ||
| } else if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| if len(addrs) > 0 { | ||
| srvTarget := strings.TrimSuffix(addrs[0].Target, ".") | ||
|
|
||
| // If we found one, check for a TXT record specifying the path | ||
| if srvTarget != "" { | ||
| txtRecs, err := net.LookupTXT(fmt.Sprintf("_%vs._tcp.%v", service, host)) | ||
| if dnsErr, ok := err.(*net.DNSError); ok { | ||
| if dnsErr.IsTemporary { | ||
| return "", err | ||
| } | ||
| } else if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| for _, txtRec := range txtRecs { | ||
| // This is not correct according to RFC 6763 sections 6.3 to 6.5, | ||
| // but LookupTXT merges all constituent strings together | ||
| for _, txtRecKeyVal := range strings.Split(txtRec, " ") { | ||
| if strings.HasPrefix(strings.ToLower(txtRecKeyVal), "path=") { | ||
| path = txtRecKeyVal[5:] | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if path != "" { | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if addrs[0].Port == 443 { | ||
| host = srvTarget | ||
| } else { | ||
| host = fmt.Sprintf("%v:%v", srvTarget, addrs[0].Port) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // If we didn't get a path from TXT records, use the default well-known location | ||
| if path == "" { | ||
| path = fmt.Sprintf("/.well-known/%v", service) | ||
| } | ||
|
|
||
| u := url.URL{Scheme: "https", Host: host, Path: path} | ||
| serviceUrl := u.String() | ||
|
|
||
| // Check if the resulting URL hosts a service | ||
| req, err := http.NewRequest(http.MethodOptions, serviceUrl, nil) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. RFC 6764 section 6 mandates that a PROPFIND request should be made with the username/password set. Alps has a different requirement: it needs to sanity check the URLs on startup (without any username/password). To follow the RFC, we could:
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To accomodate for Alps, we could have an intermediate |
||
| 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 to %v failed: %v %v", serviceUrl, resp.StatusCode, resp.Status) | ||
| } | ||
|
dpeukert marked this conversation as resolved.
|
||
|
|
||
| return serviceUrl, nil | ||
| } | ||
|
|
||
| // HTTPClient performs HTTP requests. It's implemented by *http.Client. | ||
| type HTTPClient interface { | ||
| Do(req *http.Request) (*http.Response, error) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.