webutils

package module
v0.0.0-...-96cf19d Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2023 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrExcludedPath = errors.New("webutils: file server: excluded path")
View Source
var ErrNoUnderlyingFilesystem = errors.New("webutils: file server: no underlying filesystem")
View Source
var RenamedContentEncodings = map[string]string{
	"x-compress": "compress",
	"x-gzip":     "gzip",
}
View Source
var SupportedContentEncodings = []string{
	"br",
	"gzip",
	"compress",
	"deflate",

	"identity",
}

see: https://www.iana.org/assignments/http-parameters/http-parameters.xml#http-parameters-1

Functions

func AcceptedEncodings

func AcceptedEncodings(directiveQValues string) []string

func Chevroned

func Chevroned(raw string, suffix string) string

Used for e.g. escaping `Link: <...>` HTTP headers. Inspired by net/http's Redirect implementation. Ref. https://cs.opensource.google/go/go/+/refs/tags/go1.19.2:src/net/http/http.go;drc=cbd931c9c2705e0e3a44c3b299ac2bd3369f5eb5;l=76 [accessed 2022-10-05]

func Compressed

func Compressed(w io.Writer, encoding string) (io.WriteCloser, error)

func DefinitelyUncompressable

func DefinitelyUncompressable(mimetype string) bool

DefinitelyUncompressable returns if the given MIME type is known to employ compression itself, making transfer compression unlikely to be beneficial.

func NopWriteCloser

func NopWriteCloser(w io.Writer) io.WriteCloser

func OrderByQValues

func OrderByQValues(directiveQValues string, expand func(directive string) []string) []string

func Plain

func Plain(w http.ResponseWriter, r *http.Request, code int) int

func PlainBody

func PlainBody(w http.ResponseWriter, r *http.Request, code int) int

func Plainf

func Plainf(w http.ResponseWriter, code int, format string, a ...any) int

Types

type FileServer

type FileServer struct {
	// Underlying is the file server's underlying file source.
	// Underlying may be nil.
	// Underlying MUST support concurrent Open calls. Returned files MUST
	// support inter-concurrent operations but are themselves accessed
	// sequentially.
	Underlying fs.FS

	// Is the Underlying filesystem static?
	// In this case, data is permanently cached in memory after first access
	// alongside all accepted encodings and a cryptographic hash.
	UnderlyingStatic bool

	// When non-zero, serve this path's page when the requested page was
	// not found. The 404 page's path cannot be served with a status code
	// of 200. If the 404 page's path does not exist, a default 404 is sent.
	// For an HTML page, it is recommended to employ `<base>` since the page
	// URL path and file path are decoupled.
	Page404 string

	// Allow hidden paths (i.e. paths containing "/.") to reach Underlying.Open.
	AllowHidden bool

	// Do not send any statistics. Currently, this only includes compression
	// statistics.
	// [2022-10-05] TODO Add timing statistics (cf. `Server-Timing` HTTP header).
	DisableStatistics bool

	// When non-zero, a `Link: <...>; rel="canonical"` header is sent on
	// requests to canonical paths present in Underlying.
	// It should not contain a trailing slash or schema; "https://" is presumed.
	// Sample Value: "www.example.com"
	CanonicalHost string

	// Only takes effect when UnderlyingStatic is true.
	// When non-zero, emit a `Cache-Control: public, maxage=...` header.
	CacheControlMaxAge time.Duration

	// Do not try to deduce a `Content-Type` header; probably because one
	// has been already set as an extra header.
	ProhibitMimeTypeSniffing bool

	ExtraHeaders map[string][]string
	// contains filtered or unexported fields
}

func FileServerEmbedded

func FileServerEmbedded(fsys embed.FS, dir string) *FileServer

func (*FileServer) Canonicalize

func (fsrv *FileServer) Canonicalize(name string) (string, bool)

Canonicalize computes a canonical name for a ressource. An HTML index file represents its parent directory; directories are marked with a trailing slash. If it no canonical name exists, name is returned unaltered.

func (*FileServer) IsDir

func (fsrv *FileServer) IsDir(path string) (isdir bool, ok bool)

func (*FileServer) IsExcluded

func (fsrv *FileServer) IsExcluded(path string) bool

func (*FileServer) Open

func (fsrv *FileServer) Open(path string) (fs.File, error)

func (*FileServer) Retrieve

func (fsrv *FileServer) Retrieve(req request) representation

func (*FileServer) ServeHTTP

func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*FileServer) SetExtraHeaders

func (fsrv *FileServer) SetExtraHeaders(path string, headers map[string][]string)

type Range

type Range struct {
	Index, Length int64
}

func ParseRanges

func ParseRanges(length int64, directives string) ([]Range, bool)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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