Skip to content

Commit

Permalink
groot/internal/httpio: first import
Browse files Browse the repository at this point in the history
Updates #142.
  • Loading branch information
sbinet committed Mar 11, 2022
1 parent e6d5f90 commit 53a256f
Show file tree
Hide file tree
Showing 4 changed files with 384 additions and 0 deletions.
78 changes: 78 additions & 0 deletions groot/internal/httpio/httpio.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright ©2022 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package httpio provides types for basic I/O interfaces over HTTP.
package httpio // import "go-hep.org/x/hep/groot/internal/httpio"

import (
"context"
"errors"
"net/http"
"time"
)

var (
defaultClient *http.Client

errAcceptRange = errors.New("httpio: accept-range not supported")
)

type config struct {
ctx context.Context
cli *http.Client
auth struct {
usr string
pwd string
}
}

func newConfig() *config {
return &config{
ctx: context.Background(),
cli: defaultClient,
}
}

type Option func(*config) error

// WithClient sets up Reader to use a user-provided HTTP client.
//
// By default, Reader uses an httpio-local default client.
func WithClient(cli *http.Client) Option {
return func(c *config) error {
c.cli = cli
return nil
}
}

// WithBasicAuth sets up a basic authentification scheme.
func WithBasicAuth(usr, pwd string) Option {
return func(c *config) error {
c.auth.usr = usr
c.auth.pwd = pwd
return nil
}
}

// WithContext configures Reader to use a user-provided context.
//
// By default, Reader uses context.Background.
func WithContext(ctx context.Context) Option {
return func(c *config) error {
c.ctx = ctx
return nil
}
}

func init() {
t := http.DefaultTransport.(*http.Transport).Clone()
t.MaxIdleConns = 100
t.MaxConnsPerHost = 100
t.MaxIdleConnsPerHost = 100

defaultClient = &http.Client{
Timeout: 10 * time.Second,
Transport: t,
}
}
150 changes: 150 additions & 0 deletions groot/internal/httpio/reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Copyright ©2022 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package httpio

import (
"context"
"fmt"
"io"
"net/http"
"strconv"
)

// Reader presents an HTTP resource as an io.Reader and io.ReaderAt.
type Reader struct {
cli *http.Client
req *http.Request
ctx context.Context
cancel context.CancelFunc

r *io.SectionReader
len int64
etag string
}

// Open returns a Reader from the provided URL.
func Open(uri string, opts ...Option) (r *Reader, err error) {
cfg := newConfig()
for _, opt := range opts {
err := opt(cfg)
if err != nil {
return nil, fmt.Errorf("httpio: could not open %q: %w", uri, err)
}
}

r = &Reader{
cli: cfg.cli,
}
r.ctx, r.cancel = context.WithCancel(cfg.ctx)

req, err := http.NewRequestWithContext(r.ctx, http.MethodGet, uri, nil)
if err != nil {
r.cancel()
return nil, fmt.Errorf("httpio: could not create HTTP request: %w", err)
}
if cfg.auth.usr != "" || cfg.auth.pwd != "" {
req.SetBasicAuth(cfg.auth.usr, cfg.auth.pwd)
}
r.req = req.Clone(r.ctx)

hdr, err := r.cli.Head(r.req.URL.String())
if err != nil {
r.cancel()
return nil, fmt.Errorf("httpio: could not send HEAD request: %w", err)
}
defer hdr.Body.Close()
_, _ = io.Copy(io.Discard, hdr.Body)

if hdr.StatusCode != http.StatusOK {
return nil, fmt.Errorf("httpio: invalid HEAD response code=%v", hdr.StatusCode)
}

if hdr.Header.Get("accept-ranges") != "bytes" {
return nil, fmt.Errorf("httpio: invalid HEAD response: %w", errAcceptRange)
}

r.len = hdr.ContentLength
r.etag = hdr.Header.Get("Etag")
r.r = io.NewSectionReader(r, 0, r.len)

return r, nil
}

// Size returns the number of bytes available for reading via ReadAt.
func (r *Reader) Size() int64 {
return r.len
}

// Name returns the name of the file as presented to Open.
func (r *Reader) Name() string {
return r.req.URL.String()
}

// Close implements the io.Closer interface.
func (r *Reader) Close() error {
r.cancel()
r.cli = nil
r.req = nil
return nil
}

// Read implements the io.Reader interface.
func (r *Reader) Read(p []byte) (int, error) {
return r.r.Read(p)
}

// Seek implements the io.Seeker interface.
func (r *Reader) Seek(offset int64, whence int) (int64, error) {
return r.r.Seek(offset, whence)
}

// ReadAt implements the io.ReaderAt interface.
func (r *Reader) ReadAt(p []byte, off int64) (int, error) {
if len(p) == 0 {
return 0, nil
}

rng := rng(off, off+int64(len(p))-1)
req := r.req.Clone(r.ctx)
req.Header.Set("Range", rng)

resp, err := r.cli.Do(req)
if err != nil {
return 0, fmt.Errorf("httpio: could not send GET request: %w", err)
}
defer resp.Body.Close()

n, _ := io.ReadFull(resp.Body, p)

if etag := resp.Header.Get("Etag"); etag != r.etag {
return n, fmt.Errorf("httpio: resource changed")
}

switch resp.StatusCode {
case http.StatusPartialContent:
// ok.
case http.StatusRequestedRangeNotSatisfiable:
return 0, io.EOF
default:
return n, fmt.Errorf("httpio: invalid GET response: code=%v", resp.StatusCode)
}

if int64(len(p)) > r.len {
return n, io.EOF
}

return n, nil
}

func rng(beg, end int64) string {
return "bytes=" + strconv.Itoa(int(beg)) + "-" + strconv.Itoa(int(end))
}

var (
_ io.Reader = (*Reader)(nil)
_ io.Seeker = (*Reader)(nil)
_ io.ReaderAt = (*Reader)(nil)
_ io.Closer = (*Reader)(nil)
)
153 changes: 153 additions & 0 deletions groot/internal/httpio/reader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Copyright ©2022 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package httpio

import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"testing/iotest"
)

func TestOpen(t *testing.T) {
srv := httptest.NewServer(http.FileServer(http.Dir("./testdata")))

for _, tc := range []struct {
url string
opts []Option
err error
}{
{
url: srv.URL + "/data.txt",
opts: []Option{
func(c *config) error { return fmt.Errorf("option error") },
},
err: fmt.Errorf("httpio: could not open %q: %w", srv.URL+"/data.txt", fmt.Errorf("option error")),
},
{
url: srv.URL + "xxx/not-there.txt",
err: fmt.Errorf("httpio: could not create HTTP request: "),
},
{
url: srv.URL + "000/not-there.txt",
err: fmt.Errorf("httpio: could not send HEAD request: "),
},
{
url: srv.URL + "/not-there.txt",
err: fmt.Errorf("httpio: invalid HEAD response code=404"),
},
} {
t.Run(tc.url, func(t *testing.T) {
_, err := Open(tc.url, tc.opts...)
switch {
case err == nil:
t.Fatalf("expected an error")
default:
if got, want := err.Error(), tc.err.Error(); !strings.HasPrefix(got, want) {
t.Fatalf("invalid error:\ngot= %s\nwant=%s", got, want)
}
}
})
}

t.Run("no-range", func(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("accept-range", "none")
}))

want := fmt.Errorf("httpio: invalid HEAD response: httpio: accept-range not supported")

_, err := Open(srv.URL)
if got, want := err.Error(), want.Error(); got != want {
t.Fatalf("invalid error:\ngot= %s\nwant=%s", got, want)
}
})
}

func TestReader(t *testing.T) {
dir := "./testdata"
srv := httptest.NewServer(http.FileServer(http.Dir(dir)))

want, err := os.ReadFile("./testdata/data.txt")
if err != nil {
t.Fatal(err)
}

r, err := Open(
srv.URL+"/data.txt",
WithBasicAuth("gopher", "s3cr3t"),
WithContext(context.Background()),
WithClient(http.DefaultClient),
)
if err != nil {
t.Fatal(err)
}
defer r.Close()

if got, want := r.Name(), srv.URL+"/data.txt"; got != want {
t.Fatalf("invalid name:\ngot= %q\nwant=%q", got, want)
}

if got, want := r.Size(), int64(len(want)); got != want {
t.Fatalf("invalid size: got=%d, want=%d", got, want)
}

got := make([]byte, len(want))
n, err := r.ReadAt(got, 0)
if err != nil && !errors.Is(err, io.EOF) {
t.Fatalf("could not read-at: %+v", err)
}
if !bytes.Equal(got, want) {
t.Fatalf("invalid read-at:\ngot= %q\nwant=%q", got, want)
}
if len(want) != n {
t.Fatalf("invalid nbytes: got=%d, want=%d", n, len(want))
}

t.Run("iotest", func(t *testing.T) {
r, err := Open(srv.URL + "/data.txt")
if err != nil {
t.Fatal(err)
}
defer r.Close()

err = iotest.TestReader(r, want)
if err != nil {
t.Fatal(err)
}
})

t.Run("etag", func(t *testing.T) {
r, err := Open(srv.URL + "/data.txt")
if err != nil {
t.Fatal(err)
}
defer r.Close()

// fake changing resource
r.etag += "XXX"

p := make([]byte, len(want))
n, err := r.ReadAt(p, 0)
switch {
case err == nil:
t.Fatalf("expected an Etag-related error")
default:
if got, want := err.Error(), "httpio: resource changed"; got != want {
t.Fatalf("invalid error:\ngot= %q\nwant=%q", got, want)
}
}
if n != len(want) {
t.Fatalf("invalid size: got=%d, want=%d", n, len(want))
}
})
}
3 changes: 3 additions & 0 deletions groot/internal/httpio/testdata/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hello world!

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

0 comments on commit 53a256f

Please sign in to comment.