httphandlers

package module
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2019 License: MIT Imports: 16 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConditionalGet

func ConditionalGet(w http.ResponseWriter, r *http.Request, eTag string, modifyTime time.Time, acceptableAge time.Duration) (done bool)

ConditionalGet perform conditional-get on request r. The "If-None-Match" and "If-Modified-Since" headers will be extracted from request to compare with given ETag eTag and modification time modifyTime.

A time.Duration acceptableAge is passed in for acceptable time difference on modification time comparision.

The boolean true will be return if client content is updated and response is submitted. Otherwise, false will be return and the caller should serving the content to client.

func JSONResponse

func JSONResponse(w http.ResponseWriter, v interface{}) (err error)

JSONResponse generate JSON response based on given `v`. The JSON is encoded with `encoding/json` package.

** CAUTION ** : This function does not handles HEAD method. JSON content body always responded.

func JSONResponseConditional

func JSONResponseConditional(w http.ResponseWriter, r *http.Request, v interface{}, eTag string, modifyTime time.Time, acceptableAge time.Duration) (err error)

JSONResponseConditional generate JSON response based on given `v` and handle conditional GET request. The JSON is encoded with `encoding/json` package.

func NewDumpRequestHandler

func NewDumpRequestHandler() (h http.Handler)

NewDumpRequestHandler create a handler which dumps request to response. The result will be text or JSON depends on request path.

The response will be plain text in default. If the first part of path is `/json` (eg: /json/test...) the response will given in JSON.

func NotModify

func NotModify(w http.ResponseWriter, eTag string, modifyTime time.Time)

NotModify replies to the request with 304 Not Modified to indicate that the requested content is not modified since last request.

The parameters eTag and modifyTime are ETag and modification time of content which will be attached to response headers.

Types

type ZipArchiveContentServer

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

ZipArchiveContentServer is a http.Handler to serve content from Zip archive file.

Limitation: Range request is not supported.

Here is an example of usage:

func main() {
	var err error
	handler, err := utilhttphandlers.NewZipArchiveContentServer("/path/to/file.zip", "web/", "index.html")
	if nil != err {
		log.Fatalf("failed on setting up zip content serving handler: %v", err)
		return
	}
	defer handler.Close()
	err = http.ListenAndServe(":8080", handler)
	log.Fatalf("result of http.ListenAndServe(): %v", err)
}

In above example, if http://localhost:8080/ is requested the content will be served from `web/index.html` of zip archive.

func NewZipArchiveContentServer

func NewZipArchiveContentServer(fileName, pathPrefix, defaultContentPath string) (h *ZipArchiveContentServer, err error)

NewZipArchiveContentServer creates a new instance of ZipArchiveContentServer.

The fileName is the path of zip archive file to be serve. The pathPrefix is the path of content folder inside zip archive. Empty string can be given if content should be serve from root of zip archive. The defaultContentPath is the path to default content with pathPrefix stripped (eg. index.html).

func (*ZipArchiveContentServer) Close

func (h *ZipArchiveContentServer) Close() (err error)

Close the zip archive file.

func (*ZipArchiveContentServer) ServeHTTP

ServeHTTP fulfill the request with content in zip archive.

Jump to

Keyboard shortcuts

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