deduplicate

This commit is contained in:
6543 2022-11-12 13:10:07 +01:00
parent f13feec8bf
commit 5f2f073a73
No known key found for this signature in database
GPG key ID: C99B82E40B027BAE
2 changed files with 13 additions and 13 deletions

View file

@ -42,10 +42,6 @@ func (c *Context) String(raw string, status ...int) {
_, _ = c.RespWriter.Write([]byte(raw)) _, _ = c.RespWriter.Write([]byte(raw))
} }
func (c *Context) IsMethod(m string) bool {
return c.Req.Method == m
}
func (c *Context) Redirect(uri string, statusCode int) { func (c *Context) Redirect(uri string, statusCode int) {
http.Redirect(c.RespWriter, c.Req, uri, statusCode) http.Redirect(c.RespWriter, c.Req, uri, statusCode)
} }

View file

@ -49,9 +49,19 @@ func Handler(mainDomainSuffix, rawDomain string,
ctx.RespWriter.Header().Set("Strict-Transport-Security", hsts) ctx.RespWriter.Header().Set("Strict-Transport-Security", hsts)
} }
// Block all methods not required for static pages // Handle all http methods
if !ctx.IsMethod(http.MethodGet) && !ctx.IsMethod(http.MethodHead) && !ctx.IsMethod(http.MethodOptions) { ctx.RespWriter.Header().Set("Allow", http.MethodGet+", "+http.MethodHead+", "+http.MethodOptions)
ctx.RespWriter.Header().Set("Allow", http.MethodGet+", "+http.MethodHead+", "+http.MethodOptions) // duplic 1 switch ctx.Req.Method {
case http.MethodOptions:
// return Allow header
ctx.RespWriter.WriteHeader(http.StatusNoContent)
return
case http.MethodGet,
http.MethodHead:
// end switch case and handle allowed requests
break
default:
// Block all methods not required for static pages
ctx.String("Method not allowed", http.StatusMethodNotAllowed) ctx.String("Method not allowed", http.StatusMethodNotAllowed)
return return
} }
@ -77,12 +87,6 @@ func Handler(mainDomainSuffix, rawDomain string,
ctx.RespWriter.Header().Set(headerAccessControlAllowMethods, http.MethodGet+", "+http.MethodHead) ctx.RespWriter.Header().Set(headerAccessControlAllowMethods, http.MethodGet+", "+http.MethodHead)
} }
ctx.RespWriter.Header().Set("Allow", http.MethodGet+", "+http.MethodHead+", "+http.MethodOptions) // duplic 1
if ctx.IsMethod(http.MethodOptions) {
ctx.RespWriter.WriteHeader(http.StatusNoContent)
return
}
// Prepare request information to Gitea // Prepare request information to Gitea
targetOptions := &upstream.Options{ targetOptions := &upstream.Options{
TryIndexPages: true, TryIndexPages: true,