httpreader

package
v1.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 8, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package httpreader implements io.ReaderAt over an http.Client for a resource that implements HTTP Range requests (RFC7233). Various tricks are implemented to maximize compatibility.

Tricks

  • Only use GET requests, to allow for locked-down signed requests.
  • Request last byte to negate weird CDN caching.
  • Try multiple ways to get the resource size.

Handled weirdness

  • Server not handling negative ranges correctly.
  • Server not reporting content length when making Range requests.
  • "200 OK" for a range starting at 0.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContentRange

type ContentRange struct {
	First, Last, Length int64
}

ContentRange is a parsed "bytes" content range.

Unpopulated sections of the header are set to -1; refer to RFC7233 for more information.

func (*ContentRange) Parse

func (r *ContentRange) Parse(data string) error

Parse populates the receiver with the "bytes" content range from the supplied header value or reports an error.

func (*ContentRange) Reset

func (r *ContentRange) Reset()

Reset sets all fields to a known value (-1).

type Option

type Option func(context.Context, *Reader) error

Option is used to set options in New.

func WithHeaders

func WithHeaders(h http.Header) Option

WithHeaders sets additional headers for requests.

func WithSize

func WithSize(sz int64) Option

WithSize sets the size of the HTTP resource and skips rangefinding.

type RangeUnsupported

type RangeUnsupported struct{ URL *url.URL }

RangeUnsupported is reported when Range requests are unsupported by the indicated resource.

func (*RangeUnsupported) Error

func (e *RangeUnsupported) Error() string

func (*RangeUnsupported) Is

func (e *RangeUnsupported) Is(tgt error) bool

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader implements io.ReaderAt over an HTTP resource that implements range requests.

The Reader is guaranteed to only makes GET requests.

Callers can use an io.SectionReader if they require an io.Reader interface.

func New

func New(ctx context.Context, c *http.Client, uri string, opts ...Option) (*Reader, error)

New constructs a reader for the resource at "uri", using the supplied context.Context and http.Client.

The Context is used for the lifetime of the Reader. All ReadAt calls translate to network roundtrips. Callers should arrange for buffering and chunking for the best performance. They may also want to check the discovered size and eagerly fetch the resource if it's under some threshold. This is not handled in this package.

The returned Reader must have Reader.Close called or the program may panic.

func (*Reader) Close

func (r *Reader) Close() error

Close implements io.Closer.

Callers must call Close on any Reader to release resources, or the program may panic.

func (*Reader) ReadAt

func (r *Reader) ReadAt(b []byte, off int64) (int, error)

ReadAt implements io.ReaderAt.

func (*Reader) Size

func (r *Reader) Size() int64

Size reports the discovered size of the HTTP resource.

This function may report a guessed size less than zero. This means the Reader will issue range requests, but will allow arbitrary offsets. Callers should expect something like io.Reader semantics in that case.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL