httphandlers

package module
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: May 5, 2021 License: MIT Imports: 17 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyRequestBody = errors.New("empty request body")

ErrEmptyRequestBody indicate request body 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 DecodeJSONRequest added in v0.10.0

func DecodeJSONRequest(w http.ResponseWriter, r *http.Request, v interface{}) error

DecodeJSONRequest parse JSON in the request body with given reference v. The HTTP error status code will be respond if decoding failed.

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 JSONResponseWithStatusBadRequest added in v0.11.0

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

JSONResponseWithStatusBadRequest is shortcut of JSONResponseWithStatusCode(w, v, http.StatusBadRequest) call.

func JSONResponseWithStatusCode added in v0.10.1

func JSONResponseWithStatusCode(w http.ResponseWriter, v interface{}, statusCode int) (err error)

JSONResponseWithStatusCode similar to JSONResponse but with given statusCode and no-cache headers enabled.

func JSONResponseWithStatusConflict added in v0.11.0

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

JSONResponseWithStatusConflict is shortcut of JSONResponseWithStatusCode(w, v, http.StatusConflict) call.

func JSONResponseWithStatusForbidden added in v0.11.0

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

JSONResponseWithStatusForbidden is shortcut of JSONResponseWithStatusCode(w, v, http.StatusForbidden) call.

func JSONResponseWithStatusInternalServerError added in v0.11.0

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

JSONResponseWithStatusInternalServerError is shortcut of JSONResponseWithStatusCode(w, v, http.StatusInternalServerError) call.

func JSONResponseWithStatusMethodNotAllowed added in v0.11.0

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

JSONResponseWithStatusMethodNotAllowed is shortcut of JSONResponseWithStatusCode(w, v, http.StatusMethodNotAllowed) call.

func JSONResponseWithStatusNotFound added in v0.11.0

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

JSONResponseWithStatusNotFound is shortcut of JSONResponseWithStatusCode(w, v, http.StatusNotFound) call.

func JSONResponseWithStatusNotImplemented added in v0.11.0

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

JSONResponseWithStatusNotImplemented is shortcut of JSONResponseWithStatusCode(w, v, http.StatusNotImplemented) call.

func JSONResponseWithStatusOK added in v0.12.0

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

JSONResponseWithStatusOK is shortcut of JSONResponseWithStatusCode(w, v, http.StatusOK) call.

func JSONResponseWithStatusServiceUnavailable added in v0.11.0

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

JSONResponseWithStatusServiceUnavailable is shortcut of JSONResponseWithStatusCode(w, v, http.StatusServiceUnavailable) call.

func JSONResponseWithStatusTooManyRequests added in v0.11.0

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

JSONResponseWithStatusTooManyRequests is shortcut of JSONResponseWithStatusCode(w, v, http.StatusTooManyRequests) call.

func JSONResponseWithStatusUnauthorized added in v0.11.0

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

JSONResponseWithStatusUnauthorized is shortcut of JSONResponseWithStatusCode(w, v, http.StatusUnauthorized) call.

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.

func UTF8HTMLResponseConditional added in v0.13.0

func UTF8HTMLResponseConditional(w http.ResponseWriter, r *http.Request, htmlContent []byte, eTag string, modifyTime time.Time, acceptableAge time.Duration) (err error)

UTF8HTMLResponseConditional generate HTML response based on given `htmlContent` and handle conditional GET request. The given `htmlContent` must encoded with UTF-8 charset.

func UTF8HTMLResponseWithStatusCode added in v0.13.0

func UTF8HTMLResponseWithStatusCode(w http.ResponseWriter, htmlContent []byte, statusCode int) (err error)

UTF8HTMLResponseWithStatusCode generate HTML response based on given `htmlContent` which must encoded with UTF-8 charset. The no-cache headers enabled.

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