forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Allow-Methods and Allow-Headers, refactor isOriginAllowed
- Loading branch information
Showing
3 changed files
with
82 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,56 @@ | ||
package api | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
type CORS struct { | ||
AllowOrigins []string | ||
AllowHeaders []string // Not yet implemented | ||
AllowMethods []string // ditto | ||
AllowOrigins []string | ||
AllowHeaders []string | ||
AllowMethods []string | ||
ContentType string | ||
allowOriginPatterns []string | ||
} | ||
|
||
func NewCORS(allowedOrigins []string) *CORS { | ||
return &CORS{ | ||
cors := &CORS{ | ||
AllowOrigins: allowedOrigins, | ||
AllowMethods: []string{"GET", "POST"}, | ||
AllowHeaders: []string{"Origin", "Content-Type"}, | ||
ContentType: "application/json; charset=utf-8", | ||
} | ||
|
||
cors.generatePatterns() | ||
|
||
return cors | ||
} | ||
|
||
func (c *CORS) isOriginAllowed(currentOrigin string) bool { | ||
for _, allowedOrigin := range c.AllowOrigins { | ||
if "*" == allowedOrigin || currentOrigin == allowedOrigin { | ||
return true | ||
func (c *CORS) isOriginAllowed(origin string) (allowed bool) { | ||
for _, allowedOriginPattern := range c.allowOriginPatterns { | ||
allowed, _ = regexp.MatchString(allowedOriginPattern, origin) | ||
if allowed { | ||
return | ||
} | ||
} | ||
return false | ||
return | ||
} | ||
|
||
func (c *CORS) generatePatterns() { | ||
if c.AllowOrigins != nil { | ||
for _, origin := range c.AllowOrigins { | ||
pattern := regexp.QuoteMeta(origin) | ||
pattern = strings.Replace(pattern, "\\*", ".*", -1) | ||
pattern = strings.Replace(pattern, "\\?", ".", -1) | ||
c.allowOriginPatterns = append(c.allowOriginPatterns, "^"+pattern+"$") | ||
} | ||
} | ||
} | ||
|
||
func (c *CORS) AllowedHeaders() string { | ||
return strings.Join(c.AllowHeaders, ",") | ||
} | ||
|
||
func (c *CORS) AllowedMethods() string { | ||
return strings.Join(c.AllowMethods, ",") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters