perf(webdav): support request with cookies (#5391)

pull/5420/head
gmugu 2023-10-19 19:17:53 +08:00 committed by GitHub
parent 8ef8023c20
commit aaffaee2b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package webdav
import (
"net/http"
"net/http/cookiejar"
"github.com/alist-org/alist/v3/drivers/webdav/odrvcookie"
"github.com/alist-org/alist/v3/internal/model"
@ -26,6 +27,13 @@ func (d *WebDav) setClient() error {
} else {
return err
}
} else {
cookieJar, err := cookiejar.New(nil)
if err == nil {
c.SetJar(cookieJar)
} else {
return err
}
}
d.client = c
return nil

View File

@ -83,6 +83,11 @@ func (c *Client) SetTransport(transport http.RoundTripper) {
c.c.Transport = transport
}
// SetJar exposes the ability to set a cookie jar to the client.
func (c *Client) SetJar(jar http.CookieJar) {
c.c.Jar = jar
}
// Connect connects to our dav server
func (c *Client) Connect() error {
rs, err := c.options("/")
@ -351,6 +356,11 @@ func (c *Client) Link(path string) (string, http.Header, error) {
return "", nil, newPathErrorErr("Link", path, err)
}
if c.c.Jar != nil {
for _, cookie := range c.c.Jar.Cookies(r.URL) {
r.AddCookie(cookie)
}
}
for k, vals := range c.headers {
for _, v := range vals {
r.Header.Add(k, v)