try another approach to handle HTTP errors
This commit is contained in:
parent
4f42235730
commit
143e4df5b2
@ -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")
|
||||
mr, ok := containerMethodRegex["*"]
|
||||
if ok {
|
||||
if err := checkMethodPath(w, r, mr); err != nil {
|
||||
log.Err(err).Send()
|
||||
if err := checkMethodPath(r, mr); err != nil {
|
||||
handleError(w, err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
@ -165,38 +165,34 @@ func (ph *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
resolvedIPs, err := net.LookupIP(containerName)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
|
||||
log.Err(err).Send()
|
||||
// log.Err(err).Send()
|
||||
return
|
||||
}
|
||||
for _, resolvedIP := range resolvedIPs {
|
||||
if resolvedIP.Equal(ip) {
|
||||
if err = checkMethodPath(w, r, mr); err != nil {
|
||||
log.Err(err).Send()
|
||||
if err = checkMethodPath(r, mr); err != nil {
|
||||
handleError(w, err)
|
||||
return
|
||||
}
|
||||
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
|
||||
// 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]
|
||||
if !ok {
|
||||
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
||||
return ErrHTTPMethodNotAllowed{httpMethod: r.Method}
|
||||
}
|
||||
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
|
||||
}
|
||||
@ -467,3 +463,15 @@ func registerMethodRegex(containerName, urlRegex string, httpMethods []string) e
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user