conditional-http

module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2021 License: MIT

README

Build Status Coverage Status GoDoc

Conditional HTTP middleware for Go

This package for Go provides middleware for conditional HTTP requests supporting the ETag, Last-Modified, If-Modified-Since, and If-None-Match headers, according to RFC 7232. When matches are successful, it will automatically send the 304 Not Modified status code.

Usage

import "github.com/blizzy78/conditional-http/handler"
// your regular downstream handler
var h http.Handler = ...

// add Last-Modified header to responses
h, _ = handler.LastModifiedHandler(
	func(w http.ResponseWriter, r *http.Request) (time.Time, bool) {
		// produce last modification date for r and w
		lastMod := ...
		return lastMod, true
	},
	handler.BeforeHeaders, h)

// add ETag header to responses
h = handler.ETagHandler(
	func(w http.ResponseWriter, r *http.Request) (handler.ETag, bool) {
		// produce entity-tag for r and w
		eTag := handler.ETag{
			Tag: "...",
			Weak: true,
		}
		return eTag, true
	},
	handler.AfterHeaders, h)

// check requests for If-Modified-Since and If-None-Match headers,
// and send 304 Not Modified in responses if successful
h = handler.IfNoneMatchIfModifiedSinceHandler(true, h)

License

This package is licensed under the MIT license.

Directories

Path Synopsis
Package handler provides middleware for conditional HTTP requests supporting the ETag, Last-Modified, If-Modified-Since, and If-None-Match headers, according to RFC 7232.
Package handler provides middleware for conditional HTTP requests supporting the ETag, Last-Modified, If-Modified-Since, and If-None-Match headers, according to RFC 7232.

Jump to

Keyboard shortcuts

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