httprs

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2023 License: MIT Imports: 6 Imported by: 3

README

httprs

A ReadSeeker for http.Response.Body CircleCI

Usage

import "github.com/jfbus/httprs"

resp, err := http.Get(url)

rs := httprs.NewHttpReadSeeker(resp)
defer rs.Close()
io.ReadFull(rs, buf) // reads the first bytes from the response
rs.Seek(1024, io.SeekStart) // moves the position
io.ReadFull(rs, buf) // does an additional range request and reads the first bytes from the second response

if you use a specific http.Client :

rs := httprs.NewHttpReadSeeker(resp, client)

Doc

See https://pkg.go.dev/github.com/jfbus/httprs

LICENSE

MIT - See LICENSE

Documentation

Overview

Package httprs provides a ReadSeeker for http.Response.Body.

Usage :

resp, err := http.Get(url)
rs := httprs.NewHttpReadSeeker(resp)
defer rs.Close()
io.ReadFull(rs, buf) // reads the first bytes from the response body
rs.Seek(1024, 0) // moves the position, but does no range request
io.ReadFull(rs, buf) // does a range request and reads from the response body

If you want to use a specific http.Client for additional range requests :

rs := httprs.NewHttpReadSeeker(resp, client)

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoContentLength is returned by Seek when the initial http response did not include a Content-Length header
	ErrNoContentLength = errors.New("Content-Length was not set")
	// ErrRangeRequestsNotSupported is returned by Seek and Read
	// when the remote server does not allow range requests (Accept-Ranges was not set)
	ErrRangeRequestsNotSupported = errors.New("Range requests are not supported by the remote server")
	// ErrInvalidRange is returned by Read when trying to read past the end of the file
	ErrInvalidRange = errors.New("Invalid range")
	// ErrContentHasChanged is returned by Read when the content has changed since the first request
	ErrContentHasChanged = errors.New("Content has changed since first request")
)

Functions

This section is empty.

Types

type HttpReadSeeker

type HttpReadSeeker struct {
	Requests int
	// contains filtered or unexported fields
}

A HttpReadSeeker reads from a http.Response.Body. It can Seek by doing range requests.

func NewHttpReadSeeker

func NewHttpReadSeeker(res *http.Response, client ...*http.Client) *HttpReadSeeker

NewHttpReadSeeker returns a HttpReadSeeker, using the http.Response and, optionaly, the http.Client that needs to be used for future range requests. If no http.Client is given, http.DefaultClient will be used.

res.Request will be reused for range requests, headers may be added/removed

func (*HttpReadSeeker) Clone

func (r *HttpReadSeeker) Clone() (*HttpReadSeeker, error)

Clone clones the reader to enable parallel downloads of ranges

func (*HttpReadSeeker) Close

func (r *HttpReadSeeker) Close() error

Close closes the response body

func (*HttpReadSeeker) Read

func (r *HttpReadSeeker) Read(p []byte) (n int, err error)

Read reads from the response body. It does a range request if Seek was called before.

May return ErrRangeRequestsNotSupported, ErrInvalidRange or ErrContentHasChanged

func (*HttpReadSeeker) ReadAt

func (r *HttpReadSeeker) ReadAt(p []byte, off int64) (n int, err error)

ReadAt reads from the response body starting at offset off.

May return ErrRangeRequestsNotSupported, ErrInvalidRange or ErrContentHasChanged

func (*HttpReadSeeker) Seek

func (r *HttpReadSeeker) Seek(offset int64, whence int) (int64, error)

Seek moves the reader position to a new offset.

It does not send http requests, allowing for multiple seeks without overhead. The http request will be sent by the next Read call.

May return ErrNoContentLength or ErrRangeRequestsNotSupported

Jump to

Keyboard shortcuts

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