openuri

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 23, 2023 License: MIT Imports: 15 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

GoDoc

openuri

This is a library that enables opening files and urls transparently.

It supports:

  • Direct file access (no protocol)
  • file:// protocol
  • http:// and https:// trought net/http
  • data: uri
  • Custom protocols

Usage

f, err := openuri.Open(fn)
if err != nil {
	return err
}
defer f.Close()
...

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultClient = &Client{}
	AllowLocal    = &Client{AllowLocal: true}
)
View Source
var (
	ErrNotAbsolute          = errors.New("Provided URL is not absolute")
	ErrProtocolNotSupported = errors.New("requested protocol is not supported")
	ErrLocalInvalidHost     = errors.New("invalid file: hostname provided")
)
View Source
var Data = &dataProtocol{}
View Source
var Local = &localProtocol{}
View Source
var Protocols = map[string]Handler{
	"file":  Local,
	"http":  Http,
	"https": Http,
	"data":  Data,
}

Functions

func Open

func Open(fn string) (io.ReadCloser, error)

default handlers

func OpenContext

func OpenContext(ctx context.Context, fn string) (io.ReadCloser, error)

func OpenURL

func OpenURL(u *url.URL) (io.ReadCloser, error)

func OpenURLContext

func OpenURLContext(ctx context.Context, u *url.URL) (io.ReadCloser, error)

func Stat added in v0.1.1

func Stat(fn string) (fs.FileInfo, error)

func StatContext added in v0.1.1

func StatContext(ctx context.Context, fn string) (fs.FileInfo, error)

func StatURL added in v0.1.1

func StatURL(u *url.URL) (fs.FileInfo, error)

func StatURLContext added in v0.1.1

func StatURLContext(ctx context.Context, u *url.URL) (fs.FileInfo, error)

Types

type Client

type Client struct {
	AllowLocal bool // if false, file:// and local access will be disabled. If true, even removing the file:// protocol will still allow direct file opening
}

func (*Client) Open

func (c *Client) Open(fn string) (io.ReadCloser, error)

func (*Client) OpenContext

func (c *Client) OpenContext(ctx context.Context, fn string) (io.ReadCloser, error)

func (*Client) OpenURL

func (c *Client) OpenURL(u *url.URL) (io.ReadCloser, error)

func (*Client) OpenURLContext

func (c *Client) OpenURLContext(ctx context.Context, u *url.URL) (io.ReadCloser, error)

func (*Client) Stat added in v0.1.1

func (c *Client) Stat(fn string) (fs.FileInfo, error)

func (*Client) StatContext added in v0.1.1

func (c *Client) StatContext(ctx context.Context, fn string) (fs.FileInfo, error)

func (*Client) StatURL added in v0.1.1

func (c *Client) StatURL(u *url.URL) (fs.FileInfo, error)

func (*Client) StatURLContext added in v0.1.1

func (c *Client) StatURLContext(ctx context.Context, u *url.URL) (fs.FileInfo, error)

type Handler

type Handler interface {
	OpenURI(ctx context.Context, c *Client, u *url.URL) (io.ReadCloser, error)
	StatURI(ctx context.Context, c *Client, u *url.URL) (fs.FileInfo, error)
}

type HttpProtocol

type HttpProtocol struct {
	Client *http.Client
}

func (*HttpProtocol) OpenURI

func (h *HttpProtocol) OpenURI(ctx context.Context, c *Client, u *url.URL) (io.ReadCloser, error)

func (*HttpProtocol) StatURI

func (h *HttpProtocol) StatURI(ctx context.Context, c *Client, u *url.URL) (fs.FileInfo, error)

Jump to

Keyboard shortcuts

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