caldav, carddav: take header when populating object

References: https://github.com/emersion/go-webdav/pull/134
This commit is contained in:
Simon Ser 2023-12-18 18:18:56 +01:00
parent 7d337ac048
commit 0e58dbb003
2 changed files with 14 additions and 14 deletions

View File

@ -268,29 +268,29 @@ func (c *Client) MultiGetCalendar(path string, multiGet *CalendarMultiGet) ([]Ca
return decodeCalendarObjectList(ms) return decodeCalendarObjectList(ms)
} }
func populateCalendarObject(co *CalendarObject, resp *http.Response) error { func populateCalendarObject(co *CalendarObject, h http.Header) error {
if loc := resp.Header.Get("Location"); loc != "" { if loc := h.Get("Location"); loc != "" {
u, err := url.Parse(loc) u, err := url.Parse(loc)
if err != nil { if err != nil {
return err return err
} }
co.Path = u.Path co.Path = u.Path
} }
if etag := resp.Header.Get("ETag"); etag != "" { if etag := h.Get("ETag"); etag != "" {
etag, err := strconv.Unquote(etag) etag, err := strconv.Unquote(etag)
if err != nil { if err != nil {
return err return err
} }
co.ETag = etag co.ETag = etag
} }
if contentLength := resp.Header.Get("Content-Length"); contentLength != "" { if contentLength := h.Get("Content-Length"); contentLength != "" {
n, err := strconv.ParseInt(contentLength, 10, 64) n, err := strconv.ParseInt(contentLength, 10, 64)
if err != nil { if err != nil {
return err return err
} }
co.ContentLength = n co.ContentLength = n
} }
if lastModified := resp.Header.Get("Last-Modified"); lastModified != "" { if lastModified := h.Get("Last-Modified"); lastModified != "" {
t, err := http.ParseTime(lastModified) t, err := http.ParseTime(lastModified)
if err != nil { if err != nil {
return err return err
@ -331,7 +331,7 @@ func (c *Client) GetCalendarObject(path string) (*CalendarObject, error) {
Path: resp.Request.URL.Path, Path: resp.Request.URL.Path,
Data: cal, Data: cal,
} }
if err := populateCalendarObject(co, resp); err != nil { if err := populateCalendarObject(co, resp.Header); err != nil {
return nil, err return nil, err
} }
return co, nil return co, nil
@ -362,7 +362,7 @@ func (c *Client) PutCalendarObject(path string, cal *ical.Calendar) (*CalendarOb
resp.Body.Close() resp.Body.Close()
co := &CalendarObject{Path: path} co := &CalendarObject{Path: path}
if err := populateCalendarObject(co, resp); err != nil { if err := populateCalendarObject(co, resp.Header); err != nil {
return nil, err return nil, err
} }
return co, nil return co, nil

View File

@ -338,29 +338,29 @@ func (c *Client) MultiGetAddressBook(path string, multiGet *AddressBookMultiGet)
return decodeAddressList(ms) return decodeAddressList(ms)
} }
func populateAddressObject(ao *AddressObject, resp *http.Response) error { func populateAddressObject(ao *AddressObject, h http.Header) error {
if loc := resp.Header.Get("Location"); loc != "" { if loc := h.Get("Location"); loc != "" {
u, err := url.Parse(loc) u, err := url.Parse(loc)
if err != nil { if err != nil {
return err return err
} }
ao.Path = u.Path ao.Path = u.Path
} }
if etag := resp.Header.Get("ETag"); etag != "" { if etag := h.Get("ETag"); etag != "" {
etag, err := strconv.Unquote(etag) etag, err := strconv.Unquote(etag)
if err != nil { if err != nil {
return err return err
} }
ao.ETag = etag ao.ETag = etag
} }
if contentLength := resp.Header.Get("Content-Length"); contentLength != "" { if contentLength := h.Get("Content-Length"); contentLength != "" {
n, err := strconv.ParseInt(contentLength, 10, 64) n, err := strconv.ParseInt(contentLength, 10, 64)
if err != nil { if err != nil {
return err return err
} }
ao.ContentLength = n ao.ContentLength = n
} }
if lastModified := resp.Header.Get("Last-Modified"); lastModified != "" { if lastModified := h.Get("Last-Modified"); lastModified != "" {
t, err := http.ParseTime(lastModified) t, err := http.ParseTime(lastModified)
if err != nil { if err != nil {
return err return err
@ -401,7 +401,7 @@ func (c *Client) GetAddressObject(path string) (*AddressObject, error) {
Path: resp.Request.URL.Path, Path: resp.Request.URL.Path,
Card: card, Card: card,
} }
if err := populateAddressObject(ao, resp); err != nil { if err := populateAddressObject(ao, resp.Header); err != nil {
return nil, err return nil, err
} }
return ao, nil return ao, nil
@ -439,7 +439,7 @@ func (c *Client) PutAddressObject(path string, card vcard.Card) (*AddressObject,
resp.Body.Close() resp.Body.Close()
ao := &AddressObject{Path: path} ao := &AddressObject{Path: path}
if err := populateAddressObject(ao, resp); err != nil { if err := populateAddressObject(ao, resp.Header); err != nil {
return nil, err return nil, err
} }
return ao, nil return ao, nil