Documentation ¶
Index ¶
- Variables
- func ConditionalGet(w http.ResponseWriter, r *http.Request, eTag string, modifyTime time.Time, ...) (done bool)
- func DecodeJSONRequest(w http.ResponseWriter, r *http.Request, v interface{}) error
- func JSONResponse(w http.ResponseWriter, v interface{}) (err error)
- func JSONResponseConditional(w http.ResponseWriter, r *http.Request, v interface{}, eTag string, ...) (err error)
- func NewDumpRequestHandler() (h http.Handler)
- func NotModify(w http.ResponseWriter, eTag string, modifyTime time.Time)
- type ZipArchiveContentServer
Constants ¶
This section is empty.
Variables ¶
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 NewDumpRequestHandler ¶
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 ¶
func (h *ZipArchiveContentServer) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP fulfill the request with content in zip archive.