Skip to content

Commit 577e097

Browse files
Kocaltucksaun
authored andcommitted
feat(http): add flag/config to allow CORS requests, close #229
1 parent df780de commit 577e097

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

commands/local_server_start.go

Lines 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 localWebServerAllowsCORSLogWarningMsg = "Cross-origin resource sharing (CORS) is enabled for all requests.\nYou may want to use https://github.com/nelmio/NelmioCorsBundle to have better control over HTTP headers."
5556

5657
var localServerStartCmd = &console.Command{
5758
Category: "local",
@@ -83,6 +84,7 @@ var localServerStartCmd = &console.Command{
8384
EnvVars: []string{"SSLKEYLOGFILE"},
8485
},
8586
&console.BoolFlag{Name: "no-workers", Usage: "Do not start workers"},
87+
&console.BoolFlag{Name: "allow-cors", Usage: "Allow Cross-origin resource sharing (CORS) requests"},
8688
},
8789
Action: func(c *console.Context) error {
8890
ui := terminal.SymfonyStyle(terminal.Stdout, terminal.Stdin)
@@ -188,6 +190,10 @@ var localServerStartCmd = &console.Command{
188190
ui.Warning(localWebServerTlsKeyLogWarningMsg)
189191
}
190192

193+
if config.AllowCORS {
194+
ui.Warning(localWebServerAllowsCORSLogWarningMsg)
195+
}
196+
191197
lw, err := pidFile.LogWriter()
192198
if err != nil {
193199
return err

local/http/cors.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
w.Header().Set("Access-Control-Allow-Origin", "*")
29+
w.Header().Set("Access-Control-Allow-Methods", "*")
30+
w.Header().Set("Access-Control-Allow-Headers", "*")
31+
32+
h.ServeHTTP(w, r)
33+
})
34+
}

local/http/http.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type Server struct {
5656
Appversion string
5757
UseGzip bool
5858
TlsKeyLogFile string
59+
AllowCORS bool
5960

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

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

local/project/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type Config struct {
4949
UseGzip bool `yaml:"use_gzip"`
5050
TlsKeyLogFile string `yaml:"tls_key_log_file"`
5151
NoWorkers bool `yaml:"no_workers"`
52+
AllowCORS bool `yaml:"allow_cors"`
5253
}
5354

5455
type FileConfig struct {
@@ -122,6 +123,9 @@ func NewConfigFromContext(c *console.Context, projectDir string) (*Config, *File
122123
if c.IsSet("no-workers") {
123124
config.NoWorkers = c.Bool("no-workers")
124125
}
126+
if c.IsSet("allow-cors") {
127+
config.AllowCORS = c.Bool("allow-cors")
128+
}
125129

126130
return config, fileConfig, nil
127131
}

local/project/project.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func New(c *Config) (*Project, error) {
6363
UseGzip: c.UseGzip,
6464
Appversion: c.AppVersion,
6565
TlsKeyLogFile: c.TlsKeyLogFile,
66+
AllowCORS: c.AllowCORS,
6667
},
6768
}
6869
if err != nil {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy