mirror of
https://github.com/go-gitea/gitea
synced 2024-09-17 17:26:05 +00:00
15a475b7db
* Some changes to fix recovery * Move Recovery to middlewares * Remove trace code * Fix lint * add session middleware and remove dependent on macaron for sso * Fix panic 500 page rendering * Fix bugs * Fix fmt * Fix vendor * recover unnecessary change * Fix lint and addd some comments about the copied codes. * Use util.StatDir instead of com.StatDir Co-authored-by: 6543 <6543@obermui.de>
50 lines
1.3 KiB
Go
Vendored
50 lines
1.3 KiB
Go
Vendored
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// SetHeader is a convenience handler to set a response header key/value
|
|
func SetHeader(key, value string) func(next http.Handler) http.Handler {
|
|
return func(next http.Handler) http.Handler {
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set(key, value)
|
|
next.ServeHTTP(w, r)
|
|
}
|
|
return http.HandlerFunc(fn)
|
|
}
|
|
}
|
|
|
|
// AllowContentType enforces a whitelist of request Content-Types otherwise responds
|
|
// with a 415 Unsupported Media Type status.
|
|
func AllowContentType(contentTypes ...string) func(next http.Handler) http.Handler {
|
|
allowedContentTypes := make(map[string]struct{}, len(contentTypes))
|
|
for _, ctype := range contentTypes {
|
|
allowedContentTypes[strings.TrimSpace(strings.ToLower(ctype))] = struct{}{}
|
|
}
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
if r.ContentLength == 0 {
|
|
// skip check for empty content body
|
|
next.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
|
|
s := strings.ToLower(strings.TrimSpace(r.Header.Get("Content-Type")))
|
|
if i := strings.Index(s, ";"); i > -1 {
|
|
s = s[0:i]
|
|
}
|
|
|
|
if _, ok := allowedContentTypes[s]; ok {
|
|
next.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusUnsupportedMediaType)
|
|
}
|
|
return http.HandlerFunc(fn)
|
|
}
|
|
}
|