httpgzip

package module
v0.0.0-...-d1585fc Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2023 License: MIT Imports: 15 Imported by: 88

README

httpgzip

Go Reference

Package httpgzip provides net/http-like primitives that use gzip compression when serving HTTP requests.

Installation

go get github.com/shurcooL/httpgzip

License

Documentation

Overview

Package httpgzip provides net/http-like primitives that use gzip compression when serving HTTP requests.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// NonSpecific serves a non-specific HTTP error message and status code
	// for a given non-nil error value. It's important that NonSpecific does not
	// actually include err.Error(), since its goal is to not leak information
	// in error messages to users.
	NonSpecific = func(w http.ResponseWriter, req *http.Request, err error) {
		switch {
		case os.IsNotExist(err):
			http.Error(w, "404 Not Found", http.StatusNotFound)
		case os.IsPermission(err):
			http.Error(w, "403 Forbidden", http.StatusForbidden)
		default:
			http.Error(w, "500 Internal Server Error", http.StatusInternalServerError)
		}
	}

	// Detailed serves detailed HTTP error message and status code for a given
	// non-nil error value. Because err.Error() is displayed to users, it should
	// be used in development only, or if you're confident there won't be sensitive
	// information in the underlying file system error messages.
	Detailed = func(w http.ResponseWriter, req *http.Request, err error) {
		switch {
		case os.IsNotExist(err):
			http.Error(w, "404 Not Found\n\n"+err.Error(), http.StatusNotFound)
		case os.IsPermission(err):
			http.Error(w, "403 Forbidden\n\n"+err.Error(), http.StatusForbidden)
		default:
			http.Error(w, "500 Internal Server Error\n\n"+err.Error(), http.StatusInternalServerError)
		}
	}
)

Functions

func FileServer

func FileServer(root http.FileSystem, opt FileServerOptions) http.Handler

FileServer returns a handler that serves HTTP requests with the contents of the file system rooted at root. Additional optional behaviors can be controlled via opt.

Example
package main

import (
	"net/http"

	"github.com/shurcooL/httpgzip"
)

func main() {
	http.Handle("/assets/", http.StripPrefix("/assets", httpgzip.FileServer(
		http.Dir("assets"),
		httpgzip.FileServerOptions{
			IndexHTML: true,
		},
	)))
}
Output:

func ServeContent

func ServeContent(w http.ResponseWriter, req *http.Request, name string, modTime time.Time, content io.ReadSeeker)

ServeContent is like http.ServeContent, except it applies gzip compression if compression hasn't already been done (i.e., the "Content-Encoding" header is set). It's aware of GzipByter and NotWorthGzipCompressing interfaces, and uses them to improve performance when the provided content implements them. Otherwise, it applies gzip compression on the fly, if it's found to be beneficial.

Types

type FileServerOptions

type FileServerOptions struct {
	// IndexHTML controls special handling of "index.html" file.
	IndexHTML bool

	// ServeError is used to serve errors coming from underlying file system.
	// If called, it's guaranteed to be before anything has been written
	// to w by FileServer, so it's safe to use http.Error.
	// If nil, then NonSpecific is used.
	ServeError func(w http.ResponseWriter, req *http.Request, err error)
}

FileServerOptions specifies options for FileServer.

type GzipByter

type GzipByter interface {
	// GzipBytes returns gzip compressed contents of the file.
	GzipBytes() []byte
}

GzipByter is implemented by compressed files for efficient direct access to the internal compressed bytes.

type NotWorthGzipCompressing

type NotWorthGzipCompressing interface {
	// NotWorthGzipCompressing is a noop. It's implemented in order to indicate
	// the file is not worth gzip compressing.
	NotWorthGzipCompressing()
}

NotWorthGzipCompressing is implemented by files that were determined not to be worth gzip compressing (the file size did not decrease as a result).

Jump to

Keyboard shortcuts

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