headers

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2025 License: 0BSD Imports: 5 Imported by: 1

README

Go _headers parser

Headers format parser to match Netlify's _headers file format, plus a compatible unparser.

Example

package main

import (
    "encoding/json"
    "fmt"

    "codeberg.org/git-pages/go-headers"
)

func main() {
    rules := headers.Must(headers.ParseString(`
# enable COOP/COEP on index:
/index.html
  cross-origin-opener-policy: same-origin
  # breaks some use cases
  cross-origin-embedder-policy: credentialless
# on the worker too:
/worker.js
  cross-origin-embedder-policy: credentialless
/*
  x-content-type-options: nosniff
`))
    data, _ := json.MarshalIndent(rules, "", "  ")
    fmt.Printf("%s", data)
}
[
  {
    "Path": "/index.html",
    "Headers": {
      "Cross-Origin-Embedder-Policy": [
        "credentialless"
      ],
      "Cross-Origin-Opener-Policy": [
        "same-origin"
      ]
    }
  },
  {
    "Path": "/worker.js",
    "Headers": {
      "Cross-Origin-Embedder-Policy": [
        "credentialless"
      ]
    }
  },
  {
    "Path": "/*",
    "Headers": {
      "X-Content-Type-Options": [
        "nosniff"
      ]
    }
  }
]

License

0-clause BSD

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Must

func Must[T any](value T, err error) T

Panics if there is an error.

func Unparse added in v1.1.0

func Unparse(w io.Writer, rules []Rule) (err error)

Unparse serializes to the given writer.

func UnparseString added in v1.1.0

func UnparseString(rules []Rule) (result string, err error)

UnparseString serializes to a string.

Types

type Rule

type Rule struct {
	Path    string
	Headers http.Header
}

Rule contains headers for a single path.

func Parse

func Parse(r io.Reader) (rules []Rule, err error)

Parse parses the given reader.

func ParseString

func ParseString(str string) ([]Rule, error)

ParseString parses the given string.

Jump to

Keyboard shortcuts

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