Skip to content

Commit fe269b3

Browse files
committed
fixed ##743
Signed-off-by: Vishal Rana <[email protected]>
1 parent d4dff98 commit fe269b3

File tree

9 files changed

+91
-65
lines changed

9 files changed

+91
-65
lines changed

middleware/cors.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ type (
1515
Skipper Skipper
1616

1717
// AllowOrigin defines a list of origins that may access the resource.
18-
// Optional. If request header `Origin` is set, value is []string{"<Origin>"}
19-
// else []string{"*"}.
18+
// Optional. Default value []string{"*"}.
2019
AllowOrigins []string `json:"allow_origins"`
2120

2221
// AllowMethods defines a list methods allowed when accessing the resource.
@@ -52,6 +51,7 @@ var (
5251
// DefaultCORSConfig is the default CORS middleware config.
5352
DefaultCORSConfig = CORSConfig{
5453
Skipper: defaultSkipper,
54+
AllowOrigins: []string{"*"},
5555
AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE},
5656
}
5757
)
@@ -69,11 +69,13 @@ func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
6969
if config.Skipper == nil {
7070
config.Skipper = DefaultCORSConfig.Skipper
7171
}
72+
if len(config.AllowOrigins) == 0 {
73+
config.AllowOrigins = DefaultCORSConfig.AllowOrigins
74+
}
7275
if len(config.AllowMethods) == 0 {
7376
config.AllowMethods = DefaultCORSConfig.AllowMethods
7477
}
7578

76-
allowedOrigins := strings.Join(config.AllowOrigins, ",")
7779
allowMethods := strings.Join(config.AllowMethods, ",")
7880
allowHeaders := strings.Join(config.AllowHeaders, ",")
7981
exposeHeaders := strings.Join(config.ExposeHeaders, ",")
@@ -88,21 +90,20 @@ func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
8890
req := c.Request()
8991
res := c.Response()
9092
origin := req.Header.Get(echo.HeaderOrigin)
93+
allowOrigin := ""
9194

92-
if allowedOrigins == "" {
93-
if origin != "" {
94-
allowedOrigins = origin
95-
} else {
96-
if !config.AllowCredentials {
97-
allowedOrigins = "*"
98-
}
95+
// Check allowed origins
96+
for _, o := range config.AllowOrigins {
97+
if o == "*" || o == origin {
98+
allowOrigin = o
99+
break
99100
}
100101
}
101102

102103
// Simple request
103104
if req.Method != echo.OPTIONS {
104105
res.Header().Add(echo.HeaderVary, echo.HeaderOrigin)
105-
res.Header().Set(echo.HeaderAccessControlAllowOrigin, allowedOrigins)
106+
res.Header().Set(echo.HeaderAccessControlAllowOrigin, allowOrigin)
106107
if config.AllowCredentials {
107108
res.Header().Set(echo.HeaderAccessControlAllowCredentials, "true")
108109
}
@@ -116,7 +117,7 @@ func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
116117
res.Header().Add(echo.HeaderVary, echo.HeaderOrigin)
117118
res.Header().Add(echo.HeaderVary, echo.HeaderAccessControlRequestMethod)
118119
res.Header().Add(echo.HeaderVary, echo.HeaderAccessControlRequestHeaders)
119-
res.Header().Set(echo.HeaderAccessControlAllowOrigin, allowedOrigins)
120+
res.Header().Set(echo.HeaderAccessControlAllowOrigin, allowOrigin)
120121
res.Header().Set(echo.HeaderAccessControlAllowMethods, allowMethods)
121122
if config.AllowCredentials {
122123
res.Header().Set(echo.HeaderAccessControlAllowCredentials, "true")

middleware/cors_test.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,22 @@ import (
1212
func TestCORS(t *testing.T) {
1313
e := echo.New()
1414

15-
// Origin origin
15+
// Wildcard origin
1616
req, _ := http.NewRequest(echo.GET, "/", nil)
1717
rec := httptest.NewRecorder()
1818
c := e.NewContext(req, rec)
1919
h := CORS()(echo.NotFoundHandler)
20-
req.Header.Set(echo.HeaderOrigin, "localhost")
21-
h(c)
22-
assert.Equal(t, "localhost", rec.Header().Get(echo.HeaderAccessControlAllowOrigin))
23-
24-
// Wildcard origin
25-
req, _ = http.NewRequest(echo.GET, "/", nil)
26-
rec = httptest.NewRecorder()
27-
c = e.NewContext(req, rec)
28-
h = CORS()(echo.NotFoundHandler)
2920
h(c)
3021
assert.Equal(t, "*", rec.Header().Get(echo.HeaderAccessControlAllowOrigin))
3122

32-
// Simple request
23+
// Allow origins
3324
req, _ = http.NewRequest(echo.GET, "/", nil)
3425
rec = httptest.NewRecorder()
3526
c = e.NewContext(req, rec)
27+
h = CORSWithConfig(CORSConfig{
28+
AllowOrigins: []string{"localhost"},
29+
})(echo.NotFoundHandler)
3630
req.Header.Set(echo.HeaderOrigin, "localhost")
37-
h = CORS()(echo.NotFoundHandler)
3831
h(c)
3932
assert.Equal(t, "localhost", rec.Header().Get(echo.HeaderAccessControlAllowOrigin))
4033

website/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"baseurl": "https://echo.labstack.com/",
2+
"baseurl": "https://echo.labstack.com",
33
"languageCode": "en-us",
44
"title": "Echo - Fast and unfancy HTTP server framework for Go (Golang)",
55
"canonifyurls": true,

website/content/middleware/cors.md

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,41 +30,41 @@ e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
3030
## Configuration
3131

3232
```go
33+
// CORSConfig defines the config for CORS middleware.
3334
CORSConfig struct {
34-
// Skipper defines a function to skip middleware.
35-
Skipper Skipper
36-
37-
// AllowOrigin defines a list of origins that may access the resource.
38-
// Optional. If request header `Origin` is set, value is []string{"<Origin>"}
39-
// else []string{"*"}.
40-
AllowOrigins []string `json:"allow_origins"`
41-
42-
// AllowMethods defines a list methods allowed when accessing the resource.
43-
// This is used in response to a preflight request.
44-
// Optional. Default value DefaultCORSConfig.AllowMethods.
45-
AllowMethods []string `json:"allow_methods"`
46-
47-
// AllowHeaders defines a list of request headers that can be used when
48-
// making the actual request. This in response to a preflight request.
49-
// Optional. Default value []string{}.
50-
AllowHeaders []string `json:"allow_headers"`
51-
52-
// AllowCredentials indicates whether or not the response to the request
53-
// can be exposed when the credentials flag is true. When used as part of
54-
// a response to a preflight request, this indicates whether or not the
55-
// actual request can be made using credentials.
56-
// Optional. Default value false.
57-
AllowCredentials bool `json:"allow_credentials"`
58-
59-
// ExposeHeaders defines a whitelist headers that clients are allowed to
60-
// access.
61-
// Optional. Default value []string{}.
62-
ExposeHeaders []string `json:"expose_headers"`
63-
64-
// MaxAge indicates how long (in seconds) the results of a preflight request
65-
// can be cached.
66-
// Optional. Default value 0.
67-
MaxAge int `json:"max_age"`
35+
// Skipper defines a function to skip middleware.
36+
Skipper Skipper
37+
38+
// AllowOrigin defines a list of origins that may access the resource.
39+
// Optional. Default value []string{"*"}.
40+
AllowOrigins []string `json:"allow_origins"`
41+
42+
// AllowMethods defines a list methods allowed when accessing the resource.
43+
// This is used in response to a preflight request.
44+
// Optional. Default value DefaultCORSConfig.AllowMethods.
45+
AllowMethods []string `json:"allow_methods"`
46+
47+
// AllowHeaders defines a list of request headers that can be used when
48+
// making the actual request. This in response to a preflight request.
49+
// Optional. Default value []string{}.
50+
AllowHeaders []string `json:"allow_headers"`
51+
52+
// AllowCredentials indicates whether or not the response to the request
53+
// can be exposed when the credentials flag is true. When used as part of
54+
// a response to a preflight request, this indicates whether or not the
55+
// actual request can be made using credentials.
56+
// Optional. Default value false.
57+
AllowCredentials bool `json:"allow_credentials"`
58+
59+
// ExposeHeaders defines a whitelist headers that clients are allowed to
60+
// access.
61+
// Optional. Default value []string{}.
62+
ExposeHeaders []string `json:"expose_headers"`
63+
64+
// MaxAge indicates how long (in seconds) the results of a preflight request
65+
// can be cached.
66+
// Optional. Default value 0.
67+
MaxAge int `json:"max_age"`
6868
}
6969
```
7070

website/data/index.toml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,27 @@
11
h1 = "Echo"
22
h2 = "High performance, extensible, minimalist web framework for Go"
3+
[[features]]
4+
icon = "rocket"
5+
title = "Optimized Router"
6+
text = "Highly optimized HTTP router which smartly prioritize routes"
7+
[[features]]
8+
icon = "cloud"
9+
title = "RESTful API"
10+
text = "Build robust and scalable RESTful API"
11+
[[features]]
12+
icon = "license"
13+
title = "Automatic TLS"
14+
text = "Automatically install TLS certificates from Let's Encrypt"
15+
[[features]]
16+
icon = "funnel"
17+
title = "Middleware Levels"
18+
text = "Define middleware at root, group or route level"
19+
[[features]]
20+
icon = "sync"
21+
title = "Data Binding"
22+
text = "Data binding for JSON, XML and form payload"
23+
[[features]]
24+
icon = "code"
25+
title = "Templates"
26+
text = "Template rendering with any template engine"
27+

website/layouts/_default/single.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<div class="w3-main w3-padding-64">
66
{{ partial "ad.html" }}
77
<div class="w3-row-padding">
8-
<div class="w3-col m9 l9">
8+
<div class="w3-col m10 l10">
99
{{ partial "notice.html" }}
1010
<article class="content">
1111
<section>

website/layouts/index.html

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<div class="w3-container w3-content w3-padding-64">
88
{{ partial "ad.html" }}
99
<div class="w3-row-padding">
10-
<div class="w3-col m9 l9">
10+
<div class="w3-col m10 l10">
1111
<div class="hero">
1212
<h1>{{ .Site.Data.index.h1 }}</h1>
1313
<h2>{{ .Site.Data.index.h2 }}</h2>
@@ -23,8 +23,15 @@ <h2>{{ .Site.Data.index.h2 }}</h2>
2323

2424
<div class="features">
2525
{{ range .Site.Data.index.features }}
26-
<div class="feature">
27-
</div>
26+
<div class="feature">
27+
<img src="/images/{{ .icon }}.svg">
28+
<h3>
29+
{{ .title }}
30+
</h3>
31+
<p>
32+
{{ .text | safeHTML }}
33+
</p>
34+
</div>
2835
{{ end }}
2936
</div>
3037
</div>

website/layouts/partials/sidenav.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<nav id="sidenav" class="w3-sidenav w3-collapse">
1+
<nav id="sidenav" class="w3-sidenav w3-collapse w3-card-2">
22
<span class="w3-closenav w3-xxlarge w3-hide-large" onclick="closeSidenav()">
33
&times;
44
</span>

website/layouts/single/single.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{{ partial "topnav.html" . }}
44
<div class="w3-container w3-content w3-padding-64">
55
<div class="w3-row-padding">
6-
<div class="w3-col m9 l9">
6+
<div class="w3-col m10 l10">
77
<h1>{{ .Title }}</h1>
88
{{ .Content }}
99
</div>

0 commit comments

Comments
 (0)