Skip to content

Commit

Permalink
rest: remove workaround for content-length handling bug
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelEischer committed Nov 26, 2022
1 parent f185640 commit 4300f79
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 120 deletions.
48 changes: 0 additions & 48 deletions internal/backend/rest/rest.go
Expand Up @@ -8,10 +8,8 @@ import (
"io"
"io/ioutil"
"net/http"
"net/textproto"
"net/url"
"path"
"strconv"
"strings"

"github.com/restic/restic/internal/backend/layout"
Expand Down Expand Up @@ -214,44 +212,6 @@ func (b *Backend) Load(ctx context.Context, h restic.Handle, length int, offset
return err
}

// checkContentLength returns an error if the server returned a value in the
// Content-Length header in an HTTP2 connection, but closed the connection
// before any data was sent.
//
// This is a workaround for https://github.com/golang/go/issues/46071
//
// See also https://forum.restic.net/t/http2-stream-closed-connection-reset-context-canceled/3743/10
func checkContentLength(resp *http.Response) error {
// the following code is based on
// https://github.com/golang/go/blob/b7a85e0003cedb1b48a1fd3ae5b746ec6330102e/src/net/http/h2_bundle.go#L8646

if resp.ContentLength != 0 {
return nil
}

if resp.ProtoMajor != 2 && resp.ProtoMinor != 0 {
return nil
}

if len(resp.Header[textproto.CanonicalMIMEHeaderKey("Content-Length")]) != 1 {
return nil
}

// make sure that if the server returned a content length and we can
// parse it, it is really zero, otherwise return an error
contentLength := resp.Header.Get("Content-Length")
cl, err := strconv.ParseUint(contentLength, 10, 63)
if err != nil {
return fmt.Errorf("unable to parse Content-Length %q: %w", contentLength, err)
}

if cl != 0 {
return errors.Errorf("unexpected EOF: got 0 instead of %v bytes", cl)
}

return nil
}

func (b *Backend) openReader(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
debug.Log("Load %v, length %v, offset %v", h, length, offset)
if err := h.Valid(); err != nil {
Expand Down Expand Up @@ -301,14 +261,6 @@ func (b *Backend) openReader(ctx context.Context, h restic.Handle, length int, o
return nil, errors.Errorf("unexpected HTTP response (%v): %v", resp.StatusCode, resp.Status)
}

// workaround https://github.com/golang/go/issues/46071
// see also https://forum.restic.net/t/http2-stream-closed-connection-reset-context-canceled/3743/10
err = checkContentLength(resp)
if err != nil {
_ = resp.Body.Close()
return nil, err
}

return resp.Body, nil
}

Expand Down
72 changes: 0 additions & 72 deletions internal/backend/rest/rest_int_go114_test.go

This file was deleted.

0 comments on commit 4300f79

Please sign in to comment.