cors

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MiddlewareBuilder

type MiddlewareBuilder struct {
	AllowOrigin string // URI(s) that are permitted to access the server
}

func (MiddlewareBuilder) Build

func (m MiddlewareBuilder) Build() mist.Middleware

Build constructs a middleware function that applies Cross-Origin Resource Sharing (CORS) headers to HTTP responses, based on the configuration set in the MiddlewareBuilder. This middleware can be attached to an HTTP server or router to ensure that CORS policies are enforced for every incoming request.

Returns: - The middleware function that encapsulates the CORS logic.

The middleware performs the following actions: 1. It resolves the `AllowOrigin` value to set the 'Access-Control-Allow-Origin' header.

  • If the MiddlewareBuilder's `AllowOrigin` is set, it uses that value.

  • Otherwise, it defaults to the origin specified in the request header.

    2. It sets the 'Access-Control-Allow-Credentials' header to "true" to allow the client to send credentials with cross-origin requests.

    3. If the 'Access-Control-Allow-Headers' header is not already set, it adds 'Content-Type' as a permissible header. This is a common header that needs to be accepted during CORS requests.

    4. The middleware handles preflight (OPTIONS) requests by setting a 200 OK response and ends processing. Preflight requests do not require further handling by other middleware or handlers.

5. For all other request methods, it calls the next handler in the chain.

Jump to

Keyboard shortcuts

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