Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ff9ae3e

Browse filesBrowse files
committed
feat(http): add flag/config to allow CORS requests, close #229
1 parent 37bdf46 commit ff9ae3e
Copy full SHA for ff9ae3e

File tree

5 files changed

+62
-0
lines changed
Filter options

5 files changed

+62
-0
lines changed

‎commands/local_server_start.go

Copy file name to clipboardExpand all lines: commands/local_server_start.go
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import (
5252

5353
var localWebServerProdWarningMsg = "The local web server is optimized for local development and MUST never be used in a production setup."
5454
var localWebServerTlsKeyLogWarningMsg = "Logging TLS master key is enabled. It means TLS connections between the client and this server will be INSECURE. This is NOT recommended unless you are debugging the connections."
55+
var localWebServerAllowCorsLogWarningMsg = "Cross-origin resource sharing (CORS) is enabled for every requests.\nYou may want to use https://github.com/nelmio/NelmioCorsBundle to have more control on HTTP headers."
5556

5657
var localServerStartCmd = &console.Command{
5758
Category: "local",
@@ -81,6 +82,7 @@ var localServerStartCmd = &console.Command{
8182
EnvVars: []string{"SSLKEYLOGFILE"},
8283
},
8384
&console.BoolFlag{Name: "no-workers", Usage: "Do not start workers"},
85+
&console.BoolFlag{Name: "allow-cors", Usage: "Allow Cross-origin resource sharing (CORS) requests"},
8486
},
8587
Action: func(c *console.Context) error {
8688
ui := terminal.SymfonyStyle(terminal.Stdout, terminal.Stdin)
@@ -186,6 +188,10 @@ var localServerStartCmd = &console.Command{
186188
ui.Warning(localWebServerTlsKeyLogWarningMsg)
187189
}
188190

191+
if config.AllowCORS {
192+
ui.Warning(localWebServerAllowCorsLogWarningMsg)
193+
}
194+
189195
lw, err := pidFile.LogWriter()
190196
if err != nil {
191197
return err

‎local/http/cors.go

Copy file name to clipboard
+46Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2021-present Fabien Potencier <fabien@symfony.com>
3+
*
4+
* This file is part of Symfony CLI project
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as
8+
* published by the Free Software Foundation, either version 3 of the
9+
* License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
package http
21+
22+
import (
23+
"net/http"
24+
)
25+
26+
func corsWrapper(h http.Handler) http.Handler {
27+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
28+
h.ServeHTTP(w, r)
29+
30+
if r.Method != http.MethodOptions {
31+
return
32+
}
33+
34+
if w.Header().Get("Access-Control-Allow-Origin") == "" {
35+
w.Header().Add("Access-Control-Allow-Origin", "*")
36+
}
37+
38+
if w.Header().Get("Access-Control-Allow-Methods") == "" {
39+
w.Header().Add("Access-Control-Allow-Methods", "*")
40+
}
41+
42+
if w.Header().Get("Access-Control-Allow-Headers") == "" {
43+
w.Header().Add("Access-Control-Allow-Headers", "*")
44+
}
45+
})
46+
}

‎local/http/http.go

Copy file name to clipboardExpand all lines: local/http/http.go
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type Server struct {
5555
Appversion string
5656
UseGzip bool
5757
TlsKeyLogFile string
58+
AllowCORS bool
5859

5960
httpserver *http.Server
6061
httpsserver *http.Server
@@ -97,6 +98,10 @@ func (s *Server) Start(errChan chan error) (int, error) {
9798
proxyHandler = gzipWrapper(proxyHandler)
9899
}
99100

101+
if s.AllowCORS {
102+
proxyHandler = corsWrapper(proxyHandler)
103+
}
104+
100105
s.httpserver = &http.Server{
101106
Handler: proxyHandler,
102107
}

‎local/project/config.go

Copy file name to clipboardExpand all lines: local/project/config.go
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type Config struct {
4848
UseGzip bool `yaml:"use_gzip"`
4949
TlsKeyLogFile string `yaml:"tls_key_log_file"`
5050
NoWorkers bool `yaml:"no_workers"`
51+
AllowCORS bool `yaml:"allow_cors"`
5152
}
5253

5354
type FileConfig struct {
@@ -116,6 +117,9 @@ func NewConfigFromContext(c *console.Context, projectDir string) (*Config, *File
116117
if c.IsSet("no-workers") {
117118
config.NoWorkers = c.Bool("no-workers")
118119
}
120+
if c.IsSet("allow-cors") {
121+
config.AllowCORS = c.Bool("allow-cors")
122+
}
119123

120124
return config, fileConfig, nil
121125
}

‎local/project/project.go

Copy file name to clipboardExpand all lines: local/project/project.go
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func New(c *Config) (*Project, error) {
6262
UseGzip: c.UseGzip,
6363
Appversion: c.AppVersion,
6464
TlsKeyLogFile: c.TlsKeyLogFile,
65+
AllowCORS: c.AllowCORS,
6566
},
6667
}
6768
if err != nil {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.