Skip to content

Commit

Permalink
update CORS middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
bughou committed Dec 21, 2021
1 parent b9cf445 commit 79975d2
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A golang http router with regexp and document generation support.
[![Build Status](https://github.com/lovego/goa/actions/workflows/go.yml/badge.svg)](https://github.com/lovego/goa/actions/workflows/go.yml)
[![Coverage Status](https://coveralls.io/repos/github/lovego/goa/badge.svg?branch=master&1)](https://coveralls.io/github/lovego/goa)
[![Go Report Card](https://goreportcard.com/badge/github.com/lovego/goa)](https://goreportcard.com/report/github.com/lovego/goa)
[![Documentation](https://pkg.go.dev/badge/github.com/lovego/goa)](https://pkg.go.dev/github.com/lovego/goa@v0.3.2)
[![Documentation](https://pkg.go.dev/badge/github.com/lovego/goa)](https://pkg.go.dev/github.com/lovego/goa@v0.3.3)


## Usage
Expand Down Expand Up @@ -75,7 +75,7 @@ func main() {
server.ListenAndServe(router)
}

func allowOrigin(origin string) bool {
func allowOrigin(origin string, c *goa.Context) bool {
u, err := url.Parse(origin)
if err != nil {
return false
Expand Down
2 changes: 1 addition & 1 deletion benchmark/example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func main() {
server.ListenAndServe(router)
}

func allowOrigin(origin string) bool {
func allowOrigin(origin string, c *goa.Context) bool {
u, err := url.Parse(origin)
if err != nil {
return false
Expand Down
6 changes: 3 additions & 3 deletions middlewares/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import (

// crosss origin resource share
type CORS struct {
allow func(origin string) bool
allow func(origin string, c *goa.Context) bool
SetHeader func(http.Header)
}

func NewCORS(allow func(origin string) bool) CORS {
func NewCORS(allow func(origin string, c *goa.Context) bool) CORS {
return CORS{allow: allow}
}

func (cors CORS) Check(c *goa.Context) {
if c.Request.Header.Get(`Sec-Fetch-Site`) != "same-origin" {
if origin := c.Request.Header.Get(`Origin`); origin != `` && origin != c.Origin() {
if !cors.allow(origin) {
if !cors.allow(origin, c) {
c.WriteHeader(http.StatusForbidden)
c.Write([]byte(`origin not allowed.`))
return
Expand Down

0 comments on commit 79975d2

Please sign in to comment.