mirror of
https://github.com/bluenviron/mediamtx.git
synced 2025-12-20 02:00:05 -08:00
support multiple CORS origins (#5150)
Co-authored-by: aler9 <46489434+aler9@users.noreply.github.com>
This commit is contained in:
parent
14ab95f39c
commit
ade0cddeb3
24 changed files with 441 additions and 180 deletions
|
|
@ -76,7 +76,7 @@ type httpServer struct {
|
|||
encryption bool
|
||||
serverKey string
|
||||
serverCert string
|
||||
allowOrigin string
|
||||
allowOrigins []string
|
||||
trustedProxies conf.IPNetworks
|
||||
readTimeout conf.Duration
|
||||
writeTimeout conf.Duration
|
||||
|
|
@ -90,12 +90,13 @@ func (s *httpServer) initialize() error {
|
|||
router := gin.New()
|
||||
router.SetTrustedProxies(s.trustedProxies.ToTrustedProxies()) //nolint:errcheck
|
||||
|
||||
router.Use(s.middlewareOrigin)
|
||||
router.Use(s.middlewarePreflightRequests)
|
||||
|
||||
router.Use(s.onRequest)
|
||||
|
||||
s.inner = &httpp.Server{
|
||||
Address: s.address,
|
||||
AllowOrigins: s.allowOrigins,
|
||||
ReadTimeout: time.Duration(s.readTimeout),
|
||||
WriteTimeout: time.Duration(s.writeTimeout),
|
||||
Encryption: s.encryption,
|
||||
|
|
@ -319,11 +320,7 @@ func (s *httpServer) onPage(ctx *gin.Context, pathName string, publish bool) {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *httpServer) middlewareOrigin(ctx *gin.Context) {
|
||||
ctx.Header("Access-Control-Allow-Origin", s.allowOrigin)
|
||||
ctx.Header("Access-Control-Allow-Credentials", "true")
|
||||
|
||||
// preflight requests
|
||||
func (s *httpServer) middlewarePreflightRequests(ctx *gin.Context) {
|
||||
if ctx.Request.Method == http.MethodOptions &&
|
||||
ctx.Request.Header.Get("Access-Control-Request-Method") != "" {
|
||||
ctx.Header("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PATCH, DELETE")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue