studio-b12 / gowebdav

A golang WebDAV client library and command line tool.
BSD 3-Clause "New" or "Revised" License
309 stars 89 forks source link

quick bugfix for issue #20 #21

Closed MrVine closed 6 years ago

MrVine commented 6 years ago

Problem described in #20 is in using of TeeReader. Our current code for TeeReader is:

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

and as you see when we creating new http.NewRequest() we send &ba as body:

r, err := http.NewRequest(method, PathEscape(Join(c.root, path)), &ba)

But in documentation to TeeReader you can see, that data should be read from bb first (because TeeReader writes data to Writer only when this data is read from returned Reader, so in line 16 our Writer is always empty).

I go further and trying to just replace &ba to bb in line 16. But in this case I face with new problem: application begin panic with message runtime error: invalid memory address or nil pointer dereference, because in some cases our TeeReader wraps nil, because body from arguments to c.req() is nil (for example when c.req() was called by c.mkcol(), c.options() and other methods which set body to nil).

This is quickfix, so I sure that code can be more elegant, but this pull-request allow to restore functionality of gowebdav project while this bug will be fixed in more elegant way.