Skip to content

Commit

Permalink
Check status on every request to fix #14
Browse files Browse the repository at this point in the history
  • Loading branch information
chuckwagoncomputing committed Jun 18, 2018
1 parent 9ff8e33 commit b45378c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
13 changes: 1 addition & 12 deletions client.go
Expand Up @@ -85,18 +85,7 @@ func (c *Client) Connect() error {
return err
}

if rs.StatusCode == 401 && c.auth.Type() == "NoAuth" {
if strings.Index(rs.Header.Get("Www-Authenticate"), "Digest") > -1 {
c.auth = &DigestAuth{c.auth.User(), c.auth.Pass(), digestParts(rs)}
} else if strings.Index(rs.Header.Get("Www-Authenticate"), "Basic") > -1 {
c.auth = &BasicAuth{c.auth.User(), c.auth.Pass()}
} else {
return newPathError("Authorize", c.root, rs.StatusCode)
}
return c.Connect()
} else if rs.StatusCode == 401 {
return newPathError("Authorize", c.root, rs.StatusCode)
} else if rs.StatusCode != 200 || (rs.Header.Get("Dav") == "" && rs.Header.Get("DAV") == "") {
if rs.StatusCode != 200 || (rs.Header.Get("Dav") == "" && rs.Header.Get("DAV") == "") {
return newPathError("Connect", c.root, rs.StatusCode)
}

Expand Down
26 changes: 23 additions & 3 deletions requests.go
@@ -1,20 +1,25 @@
package gowebdav

import (
"bytes"
"fmt"
"io"
"net/http"
"strings"
)

func (c *Client) req(method, path string, body io.Reader, intercept func(*http.Request)) (req *http.Response, err error) {
r, err := http.NewRequest(method, PathEscape(Join(c.root, path)), body)
// Tee the body, because if authorization fails we will need to read from it again.
var ba bytes.Buffer
bb := io.TeeReader(body, &ba)

r, err := http.NewRequest(method, PathEscape(Join(c.root, path)), &ba)
if err != nil {
return nil, err
}

c.auth.Authorize(c, method, path)

for k, vals := range c.headers {
for _, v := range vals {
r.Header.Add(k, v)
Expand All @@ -25,7 +30,22 @@ func (c *Client) req(method, path string, body io.Reader, intercept func(*http.R
intercept(r)
}

return c.c.Do(r)
rs, err := c.c.Do(r)

if rs.StatusCode == 401 && c.auth.Type() == "NoAuth" {
if strings.Index(rs.Header.Get("Www-Authenticate"), "Digest") > -1 {
c.auth = &DigestAuth{c.auth.User(), c.auth.Pass(), digestParts(rs)}
} else if strings.Index(rs.Header.Get("Www-Authenticate"), "Basic") > -1 {
c.auth = &BasicAuth{c.auth.User(), c.auth.Pass()}
} else {
return rs, newPathError("Authorize", c.root, rs.StatusCode)
}
return c.req(method, path, bb, intercept)
} else if rs.StatusCode == 401 {
return rs, newPathError("Authorize", c.root, rs.StatusCode)
}

return rs, err
}

func (c *Client) mkcol(path string) int {
Expand Down

0 comments on commit b45378c

Please sign in to comment.