httpreaderat

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2026 License: ISC Imports: 12 Imported by: 1

README

httpreaderat

CI Go Reference Go Report Card

-- import "vimagination.zapto.org/httpreaderat"

Package httpreaderat allows opening a URL as a io.ReaderAt.

Highlights

  • Simple library to allow random-reading from Range capable HTTP servers.
  • Customisable block-based caching for better performance.

Usage

package main

import (
	"embed"
	"fmt"
	"net/http"
	"net/http/httptest"

	"vimagination.zapto.org/httpreaderat"
)

//go:embed example_test.go
var f embed.FS

func main() {
	srv := httptest.NewServer(http.FileServerFS(f))

	r, err := httpreaderat.NewRequest(srv.URL + "/example_test.go")
	if err != nil {
		fmt.Println(err)

		return
	}

	buf := make([]byte, 17)

	n, err := r.ReadAt(buf, 8)
	fmt.Printf("Bytes: %d\nErr: %v\nRead: %s", n, err, buf)

	// Output:
	// Bytes: 17
	// Err: <nil>
	// Read: httpreaderat_test
}

Documentation

Full API docs can be found at:

https://pkg.go.dev/vimagination.zapto.org/httpreaderat

Documentation

Overview

Package httpreaderat allows opening a URL as a io.ReaderAt.

Example
package main

import (
	"embed"
	"fmt"
	"net/http"
	"net/http/httptest"

	"vimagination.zapto.org/httpreaderat"
)

//go:embed example_test.go
var f embed.FS

func main() {
	srv := httptest.NewServer(http.FileServerFS(f))

	r, err := httpreaderat.NewRequest(srv.URL + "/example_test.go")
	if err != nil {
		fmt.Println(err)

		return
	}

	buf := make([]byte, 17)

	n, err := r.ReadAt(buf, 8)
	fmt.Printf("Bytes: %d\nErr: %v\nRead: %s", n, err, buf)

}
Output:
Bytes: 17
Err: <nil>
Read: httpreaderat_test

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNoRange = errors.New("no range header")

Errors.

Functions

This section is empty.

Types

type Client added in v1.0.2

type Client interface {
	Do(req *http.Request) (*http.Response, error)
}

Client is an interface representing the method required when performing HTTP requests, usually implemented by *http.Client.

type Option

type Option func(*Request)

Option is an option that can be parsed to NewRequest.

func BlockSize

func BlockSize(size int64) Option

BlockSize changes the store block size from the default 4KB.

func CacheCount

func CacheCount(count uint64) Option

CacheCount changes the number of cached blocks from 256.

func HTTPClient added in v1.0.2

func HTTPClient(client Client) Option

HTTPClient allows the setting of a custom HTTP client, instead of http.DefaultClient allowing the setting of custom HTTP options.

func SetLength

func SetLength(length int64) Option

SetLength sets the maximum length of the remote object, bypassing the automatic detection.

type Request

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

Request represents an io.ReaderAt for an HTTP URL.

func NewRequest

func NewRequest(url string, opts ...Option) (*Request, error)

NewRequest creates a new Request object, for the given URL, that implements io.ReaderAt.

Options can be passed in to modify how the maximum length is determined, and the characteristics of the block caching.

By default, up to 256 4KB blocks will be cached.

func (*Request) Clear

func (r *Request) Clear()

Clear clears the block cache.

func (*Request) Length

func (r *Request) Length() int64

func (*Request) ReadAt

func (r *Request) ReadAt(p []byte, n int64) (int, error)

ReadAt implements the io.ReaderAt interface.

Jump to

Keyboard shortcuts

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