cors

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cors is the Fetch standard's CORS protocol as browsers apply it: when a cross-origin fetch() or XHR needs a preflight, whether a preflight's answer or a redirect allows the request, and the preflight cache. Sending a preflight is the root package's.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Check

func Check(h http.Header, origin string, credentialed bool) string

Check returns why a response's headers fail the Fetch standard's CORS check for a request from origin, with credentials or not, or "".

func CheckPreflight

func CheckPreflight(resp *http.Response, need *Need) string

CheckPreflight returns why a preflight's response does not allow the request (the Fetch standard's CORS-preflight fetch), or "".

func PreflightHeaders

func PreflightHeaders(p *profile.Spec, config http.Header) http.Header

PreflightHeaders returns the Config.Header (config) a CORS preflight gets: those its kind's header lists name, such as a User-Agent or Accept-Language override, so it goes as the request does; never the page's own, such as an API key, which a preflight never carries.

func SafelistedMethod

func SafelistedMethod(m string) bool

SafelistedMethod reports a CORS-safelisted method: one a request may use without a preflight.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache remembers successful preflights: per key, until when each method ("m:PUT") and header name ("h:x-a") is allowed. The zero Cache is empty and ready to use.

func (*Cache) Allows

func (c *Cache) Allows(need *Need, now time.Time) bool

Allows reports whether a cached answer allows need's request at now.

func (*Cache) Store

func (c *Cache) Store(need *Need, h http.Header, replace bool, now time.Time)

Store remembers what a successful preflight's response h allows, for its Access-Control-Max-Age (else defaultPreflightAge): replacing the key's entry (Chrome, Safari) or adding to it (Firefox).

type Need

type Need struct {
	Method string
	// Headers are the request's CORS-unsafe header names, lowercase,
	// sorted, once each: Access-Control-Request-Headers.
	Headers []string
	// contains filtered or unexported fields
}

Need is what a request's preflight asks for it.

func Preflight

func Preflight(req *http.Request, origin string, credentialed bool, h http.Header, force bool) *Need

Preflight returns the preflight a browser sends before a CORS request from origin (the root's preflightOrigin; "" for none, which is nil) with a non-safelisted method or header, or nil. With force it ignores method and headers (a hop inheriting its chain's preflight).

Jump to

Keyboard shortcuts

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