httpgzip: github.com/shurcooL/httpgzip Index | Examples | Files

package httpgzip

import "github.com/shurcooL/httpgzip"

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

Index

Examples

Package Files

doc.go fs.go gzip.go

Variables

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, fmt.Sprintf("404 Not Found\n\n%v", err), http.StatusNotFound)
        case os.IsPermission(err):
            http.Error(w, fmt.Sprintf("403 Forbidden\n\n%v", err), http.StatusForbidden)
        default:
            http.Error(w, fmt.Sprintf("500 Internal Server Error\n\n%v", err), http.StatusInternalServerError)
        }
    }
)

func FileServer Uses

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.

Code:

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

func ServeContent Uses

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. 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.

type FileServerOptions Uses

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 Uses

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 Uses

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).

Package httpgzip imports 15 packages (graph) and is imported by 13 packages. Updated 2017-01-20. Refresh now. Tools for package owners.