https://github.com/blizzy78/conditional-http
Conditional HTTP middleware for Go
https://github.com/blizzy78/conditional-http
go golang golang-library http middleware
Last synced: 2 months ago
JSON representation
Conditional HTTP middleware for Go
- Host: GitHub
- URL: https://github.com/blizzy78/conditional-http
- Owner: blizzy78
- License: mit
- Created: 2021-04-01T19:43:57.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-04-02T10:55:13.000Z (about 5 years ago)
- Last Synced: 2023-07-27T21:57:52.605Z (over 2 years ago)
- Topics: go, golang, golang-library, http, middleware
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/blizzy78/conditional-http) [](https://coveralls.io/github/blizzy78/conditional-http?branch=master) [](https://pkg.go.dev/github.com/blizzy78/conditional-http)
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
-----
```go
import "github.com/blizzy78/conditional-http/handler"
```
```go
// 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.
[RFC 7232]: https://www.rfc-editor.org/rfc/rfc7232.txt