Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/pkg/gowebdav/requests.go
1560 views
1
package gowebdav
2
3
import (
4
"bytes"
5
"fmt"
6
"io"
7
"net/http"
8
"path"
9
"strings"
10
)
11
12
func (c *Client) req(method, path string, body io.Reader, intercept func(*http.Request)) (req *http.Response, err error) {
13
var r *http.Request
14
var retryBuf io.Reader
15
canRetry := true
16
if body != nil {
17
// If the authorization fails, we will need to restart reading
18
// from the passed body stream.
19
// When body is seekable, use seek to reset the streams
20
// cursor to the start.
21
// Otherwise, copy the stream into a buffer while uploading
22
// and use the buffers content on retry.
23
if sk, ok := body.(io.Seeker); ok {
24
if _, err = sk.Seek(0, io.SeekStart); err != nil {
25
return
26
}
27
retryBuf = body
28
} else if method == http.MethodPut {
29
canRetry = false
30
} else {
31
buff := &bytes.Buffer{}
32
retryBuf = buff
33
body = io.TeeReader(body, buff)
34
}
35
r, err = http.NewRequest(method, PathEscape(Join(c.root, path)), body)
36
} else {
37
r, err = http.NewRequest(method, PathEscape(Join(c.root, path)), nil)
38
}
39
40
if err != nil {
41
return nil, err
42
}
43
44
for k, vals := range c.headers {
45
for _, v := range vals {
46
r.Header.Add(k, v)
47
}
48
}
49
50
// make sure we read 'c.auth' only once since it will be substituted below
51
// and that is unsafe to do when multiple goroutines are running at the same time.
52
c.authMutex.Lock()
53
auth := c.auth
54
c.authMutex.Unlock()
55
56
auth.Authorize(r, method, path)
57
58
if intercept != nil {
59
intercept(r)
60
}
61
62
if c.interceptor != nil {
63
c.interceptor(method, r)
64
}
65
66
rs, err := c.c.Do(r)
67
if err != nil {
68
return nil, err
69
}
70
71
if rs.StatusCode == 401 && auth.Type() == "NoAuth" {
72
wwwAuthenticateHeader := strings.ToLower(rs.Header.Get("Www-Authenticate"))
73
74
if strings.Index(wwwAuthenticateHeader, "digest") > -1 {
75
c.authMutex.Lock()
76
c.auth = &DigestAuth{auth.User(), auth.Pass(), digestParts(rs)}
77
c.authMutex.Unlock()
78
} else if strings.Index(wwwAuthenticateHeader, "basic") > -1 {
79
c.authMutex.Lock()
80
c.auth = &BasicAuth{auth.User(), auth.Pass()}
81
c.authMutex.Unlock()
82
} else {
83
return rs, newPathError("Authorize", c.root, rs.StatusCode)
84
}
85
86
// retryBuf will be nil if body was nil initially so no check
87
// for body == nil is required here.
88
if canRetry {
89
return c.req(method, path, retryBuf, intercept)
90
}
91
} else if rs.StatusCode == 401 {
92
return rs, newPathError("Authorize", c.root, rs.StatusCode)
93
}
94
95
return rs, err
96
}
97
98
func (c *Client) mkcol(path string) (status int, err error) {
99
rs, err := c.req("MKCOL", path, nil, nil)
100
if err != nil {
101
return
102
}
103
defer rs.Body.Close()
104
105
status = rs.StatusCode
106
if status == 405 {
107
status = 201
108
}
109
110
return
111
}
112
113
func (c *Client) options(path string) (*http.Response, error) {
114
return c.req("OPTIONS", path, nil, func(rq *http.Request) {
115
rq.Header.Add("Depth", "0")
116
})
117
}
118
119
func (c *Client) propfind(path string, self bool, body string, resp interface{}, parse func(resp interface{}) error) error {
120
rs, err := c.req("PROPFIND", path, strings.NewReader(body), func(rq *http.Request) {
121
if self {
122
rq.Header.Add("Depth", "0")
123
} else {
124
rq.Header.Add("Depth", "1")
125
}
126
rq.Header.Add("Content-Type", "application/xml;charset=UTF-8")
127
rq.Header.Add("Accept", "application/xml,text/xml")
128
rq.Header.Add("Accept-Charset", "utf-8")
129
// TODO add support for 'gzip,deflate;q=0.8,q=0.7'
130
rq.Header.Add("Accept-Encoding", "")
131
})
132
if err != nil {
133
return err
134
}
135
defer rs.Body.Close()
136
137
if rs.StatusCode != 207 {
138
return newPathError("PROPFIND", path, rs.StatusCode)
139
}
140
141
return parseXML(rs.Body, resp, parse)
142
}
143
144
func (c *Client) doCopyMove(
145
method string,
146
oldpath string,
147
newpath string,
148
overwrite bool,
149
) (
150
status int,
151
r io.ReadCloser,
152
err error,
153
) {
154
rs, err := c.req(method, oldpath, nil, func(rq *http.Request) {
155
rq.Header.Add("Destination", PathEscape(Join(c.root, newpath)))
156
if overwrite {
157
rq.Header.Add("Overwrite", "T")
158
} else {
159
rq.Header.Add("Overwrite", "F")
160
}
161
})
162
if err != nil {
163
return
164
}
165
status = rs.StatusCode
166
r = rs.Body
167
return
168
}
169
170
func (c *Client) copymove(method string, oldpath string, newpath string, overwrite bool) (err error) {
171
s, data, err := c.doCopyMove(method, oldpath, newpath, overwrite)
172
if err != nil {
173
return
174
}
175
if data != nil {
176
defer data.Close()
177
}
178
179
switch s {
180
case 201, 204:
181
return nil
182
183
case 207:
184
// TODO handle multistat errors, worst case ...
185
log(fmt.Sprintf(" TODO handle %s - %s multistatus result %s", method, oldpath, String(data)))
186
187
case 409:
188
err := c.createParentCollection(newpath)
189
if err != nil {
190
return err
191
}
192
193
return c.copymove(method, oldpath, newpath, overwrite)
194
}
195
196
return newPathError(method, oldpath, s)
197
}
198
199
func (c *Client) put(path string, stream io.Reader, callback func(r *http.Request)) (status int, err error) {
200
rs, err := c.req(http.MethodPut, path, stream, callback)
201
if err != nil {
202
return
203
}
204
defer rs.Body.Close()
205
//all, _ := io.ReadAll(rs.Body)
206
//logrus.Debugln("put res: ", string(all))
207
status = rs.StatusCode
208
return
209
}
210
211
func (c *Client) createParentCollection(itemPath string) (err error) {
212
parentPath := path.Dir(itemPath)
213
if parentPath == "." || parentPath == "/" {
214
return nil
215
}
216
217
return c.MkdirAll(parentPath, 0755)
218
}
219
220