Skip to content

blizzy78/conditional-http

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 

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.