mirror of
https://github.com/1f349/go-webdav.git
synced 2024-12-22 08:14:15 +00:00
internal: parse WebDAV toplevel <error> elements
This commit is contained in:
parent
f4e3fe8c0a
commit
4c0dc5d900
@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
@ -77,8 +78,23 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if resp.StatusCode/100 != 2 {
|
if resp.StatusCode/100 != 2 {
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
contentType := resp.Header.Get("Content-Type")
|
||||||
|
if contentType == "" {
|
||||||
|
contentType = "text/plain"
|
||||||
|
}
|
||||||
|
|
||||||
var wrappedErr error
|
var wrappedErr error
|
||||||
if strings.HasPrefix(resp.Header.Get("Content-Type"), "text/") {
|
t, _, _ := mime.ParseMediaType(contentType)
|
||||||
|
if t == "application/xml" || t == "text/xml" {
|
||||||
|
var davErr Error
|
||||||
|
if err := xml.NewDecoder(resp.Body).Decode(&davErr); err != nil {
|
||||||
|
wrappedErr = err
|
||||||
|
} else {
|
||||||
|
wrappedErr = &davErr
|
||||||
|
}
|
||||||
|
} else if strings.HasPrefix(t, "text/") {
|
||||||
lr := io.LimitedReader{R: resp.Body, N: 1024}
|
lr := io.LimitedReader{R: resp.Body, N: 1024}
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
io.Copy(&buf, &lr)
|
io.Copy(&buf, &lr)
|
||||||
|
@ -360,6 +360,11 @@ type Error struct {
|
|||||||
Raw []RawXMLValue `xml:",any"`
|
Raw []RawXMLValue `xml:",any"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (err *Error) Error() string {
|
||||||
|
b, _ := xml.Marshal(err)
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
|
||||||
// https://tools.ietf.org/html/rfc4918#section-15.2
|
// https://tools.ietf.org/html/rfc4918#section-15.2
|
||||||
type DisplayName struct {
|
type DisplayName struct {
|
||||||
XMLName xml.Name `xml:"DAV: displayname"`
|
XMLName xml.Name `xml:"DAV: displayname"`
|
||||||
|
Loading…
Reference in New Issue
Block a user