contentdigest

package
v0.0.26 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: MIT Imports: 7 Imported by: 0

README

Content-Digest middleware

contentdigest verifies RFC 9530 Content-Digest request fields and can add a digest to responses. Supported algorithms are sha-256 and sha-512.

app.Post("/artifacts",
    contentdigest.New(contentdigest.Config{
        RequireRequest: true,
        Response: contentdigest.ResponseWhenRequested,
    }),
    upload,
)

Place response compression before this middleware when the digest must cover the compressed message content. Verification uses constant-time comparison.

Documentation

Overview

Package contentdigest implements RFC 9530 Content-Digest verification and response generation. It is opt-in and does not affect fh's default hot path.

Index

Constants

View Source
const (
	SHA256 = "sha-256"
	SHA512 = "sha-512"
)

Variables

View Source
var (
	ErrMalformedDigest   = errors.New("content-digest: malformed field")
	ErrUnsupportedDigest = errors.New("content-digest: no supported algorithm")
	ErrDigestMismatch    = errors.New("content-digest: digest mismatch")
)

Functions

func Format

func Format(algorithm string, content []byte) (string, error)

Format returns an RFC 9530 dictionary member containing a Byte Sequence.

func New

func New(config ...Config) fh.HandlerFunc

New verifies a request Content-Digest whenever the field is present. It can require the field and can add a response digest always or when requested by Want-Content-Digest.

func Verify

func Verify(field string, content []byte) error

Verify accepts an RFC 9530 dictionary with one or more members and succeeds when at least one supported digest matches content.

func Wants

func Wants(field, algorithm string) bool

Wants reports whether an RFC 9530 preference dictionary mentions algorithm with a non-zero preference.

Types

type Config

type Config struct {
	RequireRequest          bool
	SkipRequestVerification bool
	Response                ResponseMode
	ResponseAlgorithm       string
	Next                    func(fh.Ctx) bool
}

type ResponseMode

type ResponseMode uint8
const (
	ResponseNone ResponseMode = iota
	ResponseWhenRequested
	ResponseAlways
)

Jump to

Keyboard shortcuts

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