try another approach to handle HTTP errors

This commit is contained in:
Adrien PONSIN 2025-04-15 21:17:32 +02:00
parent 4f42235730
commit 143e4df5b2
No known key found for this signature in database
GPG Key ID: 7B4D4A32C05C475E

View File

@ -151,8 +151,8 @@ func (ph *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Debug().Str("remote_addr", r.RemoteAddr).Str("http_method", r.Method).Str("path", r.URL.Path).Msg("incoming request") log.Debug().Str("remote_addr", r.RemoteAddr).Str("http_method", r.Method).Str("path", r.URL.Path).Msg("incoming request")
mr, ok := containerMethodRegex["*"] mr, ok := containerMethodRegex["*"]
if ok { if ok {
if err := checkMethodPath(w, r, mr); err != nil { if err := checkMethodPath(r, mr); err != nil {
log.Err(err).Send() handleError(w, err)
return return
} }
} else { } else {
@ -165,38 +165,34 @@ func (ph *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
resolvedIPs, err := net.LookupIP(containerName) resolvedIPs, err := net.LookupIP(containerName)
if err != nil { if err != nil {
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden) http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
log.Err(err).Send() // log.Err(err).Send()
return return
} }
for _, resolvedIP := range resolvedIPs { for _, resolvedIP := range resolvedIPs {
if resolvedIP.Equal(ip) { if resolvedIP.Equal(ip) {
if err = checkMethodPath(w, r, mr); err != nil { if err = checkMethodPath(r, mr); err != nil {
log.Err(err).Send() handleError(w, err)
return return
} }
ph.rp.ServeHTTP(w, r) ph.rp.ServeHTTP(w, r)
return
} }
} }
} }
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
return
} }
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
return
} }
// checkMethodPath executes the regular expression on the path of the HTTP request if and only if // checkMethodPath executes the regular expression on the path of the HTTP request if and only if
// the latter's HTTP method is actually present in the list of authorized HTTP methods. // the latter's HTTP method is actually present in the list of authorized HTTP methods.
func checkMethodPath(w http.ResponseWriter, r *http.Request, mr methodRegex) error { func checkMethodPath(r *http.Request, mr methodRegex) error {
req, ok := mr[r.Method] req, ok := mr[r.Method]
if !ok { if !ok {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return ErrHTTPMethodNotAllowed{httpMethod: r.Method} return ErrHTTPMethodNotAllowed{httpMethod: r.Method}
} }
if !req.MatchString(r.URL.Path) { if !req.MatchString(r.URL.Path) {
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden) return ErrNoMatch{path: r.URL.Path, httpMethod: r.Method}
return ErrNoMatch{
path: r.URL.Path,
httpMethod: r.Method,
}
} }
return nil return nil
} }
@ -467,3 +463,15 @@ func registerMethodRegex(containerName, urlRegex string, httpMethods []string) e
} }
return nil return nil
} }
func handleError(w http.ResponseWriter, err error) {
var methodNotAllowedErr ErrHTTPMethodNotAllowed
var noMatchErr ErrNoMatch
if errors.As(err, &methodNotAllowedErr) {
http.Error(w, err.Error(), http.StatusMethodNotAllowed)
} else if errors.As(err, &noMatchErr) {
http.Error(w, err.Error(), http.StatusForbidden)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}