-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into gsora/fix-RolesAt-dv
- Loading branch information
Showing
424 changed files
with
30,743 additions
and
28,444 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
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
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
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
load("@prysm//tools/go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = [ | ||
"middleware.go", | ||
"util.go", | ||
], | ||
importpath = "github.com/prysmaticlabs/prysm/v5/api/server/middleware", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"@com_github_gorilla_mux//:go_default_library", | ||
"@com_github_rs_cors//:go_default_library", | ||
], | ||
) | ||
|
||
go_test( | ||
name = "go_default_test", | ||
srcs = [ | ||
"middleware_test.go", | ||
"util_test.go", | ||
], | ||
embed = [":go_default_library"], | ||
deps = [ | ||
"//api:go_default_library", | ||
"//testing/assert:go_default_library", | ||
"//testing/require:go_default_library", | ||
], | ||
) |
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 |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package middleware | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/rs/cors" | ||
) | ||
|
||
// NormalizeQueryValuesHandler normalizes an input query of "key=value1,value2,value3" to "key=value1&key=value2&key=value3" | ||
func NormalizeQueryValuesHandler(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
query := r.URL.Query() | ||
NormalizeQueryValues(query) | ||
r.URL.RawQuery = query.Encode() | ||
|
||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
|
||
// CorsHandler sets the cors settings on api endpoints | ||
func CorsHandler(allowOrigins []string) mux.MiddlewareFunc { | ||
c := cors.New(cors.Options{ | ||
AllowedOrigins: allowOrigins, | ||
AllowedMethods: []string{http.MethodPost, http.MethodGet, http.MethodDelete, http.MethodOptions}, | ||
AllowCredentials: true, | ||
MaxAge: 600, | ||
AllowedHeaders: []string{"*"}, | ||
}) | ||
|
||
return c.Handler | ||
} | ||
|
||
// ContentTypeHandler checks request for the appropriate media types otherwise returning a http.StatusUnsupportedMediaType error | ||
func ContentTypeHandler(acceptedMediaTypes []string) mux.MiddlewareFunc { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// skip the GET request | ||
if r.Method == http.MethodGet { | ||
next.ServeHTTP(w, r) | ||
return | ||
} | ||
contentType := r.Header.Get("Content-Type") | ||
if contentType == "" { | ||
http.Error(w, "Content-Type header is missing", http.StatusUnsupportedMediaType) | ||
return | ||
} | ||
|
||
accepted := false | ||
for _, acceptedType := range acceptedMediaTypes { | ||
if strings.Contains(strings.TrimSpace(contentType), strings.TrimSpace(acceptedType)) { | ||
accepted = true | ||
break | ||
} | ||
} | ||
|
||
if !accepted { | ||
http.Error(w, fmt.Sprintf("Unsupported media type: %s", contentType), http.StatusUnsupportedMediaType) | ||
return | ||
} | ||
|
||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
} | ||
|
||
// AcceptHeaderHandler checks if the client's response preference is handled | ||
func AcceptHeaderHandler(serverAcceptedTypes []string) mux.MiddlewareFunc { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
acceptHeader := r.Header.Get("Accept") | ||
// header is optional and should skip if not provided | ||
if acceptHeader == "" { | ||
next.ServeHTTP(w, r) | ||
return | ||
} | ||
|
||
accepted := false | ||
acceptTypes := strings.Split(acceptHeader, ",") | ||
// follows rules defined in https://datatracker.ietf.org/doc/html/rfc2616#section-14.1 | ||
for _, acceptType := range acceptTypes { | ||
acceptType = strings.TrimSpace(acceptType) | ||
if acceptType == "*/*" { | ||
accepted = true | ||
break | ||
} | ||
for _, serverAcceptedType := range serverAcceptedTypes { | ||
if strings.HasPrefix(acceptType, serverAcceptedType) { | ||
accepted = true | ||
break | ||
} | ||
if acceptType != "/*" && strings.HasSuffix(acceptType, "/*") && strings.HasPrefix(serverAcceptedType, acceptType[:len(acceptType)-2]) { | ||
accepted = true | ||
break | ||
} | ||
} | ||
if accepted { | ||
break | ||
} | ||
} | ||
|
||
if !accepted { | ||
http.Error(w, fmt.Sprintf("Not Acceptable: %s", acceptHeader), http.StatusNotAcceptable) | ||
return | ||
} | ||
|
||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
} |
Oops, something went wrong.