2020-04-30 02:03:05 +00:00
package server
import (
"net"
"github.com/valyala/fasthttp"
2021-08-11 01:04:35 +00:00
"github.com/authelia/authelia/v4/internal/logging"
2020-04-30 02:03:05 +00:00
)
// Replacement for the default error handler in fasthttp.
func autheliaErrorHandler ( ctx * fasthttp . RequestCtx , err error ) {
2021-01-16 23:23:35 +00:00
logger := logging . Logger ( )
2020-04-30 02:03:05 +00:00
if _ , ok := err . ( * fasthttp . ErrSmallBuffer ) ; ok {
// Note: Getting X-Forwarded-For or Request URI is impossible for ths error.
2021-01-16 23:23:35 +00:00
logger . Tracef ( "Request was too large to handle from client %s. Response Code %d." , ctx . RemoteIP ( ) . String ( ) , fasthttp . StatusRequestHeaderFieldsTooLarge )
2021-08-03 23:18:20 +00:00
ctx . Error ( "request header too large" , fasthttp . StatusRequestHeaderFieldsTooLarge )
2020-04-30 02:03:05 +00:00
} else if netErr , ok := err . ( * net . OpError ) ; ok && netErr . Timeout ( ) {
// TODO: Add X-Forwarded-For Check here.
2021-01-16 23:23:35 +00:00
logger . Tracef ( "Request timeout occurred while handling from client %s: %s. Response Code %d." , ctx . RemoteIP ( ) . String ( ) , ctx . RequestURI ( ) , fasthttp . StatusRequestTimeout )
2021-08-03 23:18:20 +00:00
ctx . Error ( "request timeout" , fasthttp . StatusRequestTimeout )
2020-04-30 02:03:05 +00:00
} else {
// TODO: Add X-Forwarded-For Check here.
2021-01-16 23:23:35 +00:00
logger . Tracef ( "An unknown error occurred while handling a request from client %s: %s. Response Code %d." , ctx . RemoteIP ( ) . String ( ) , ctx . RequestURI ( ) , fasthttp . StatusBadRequest )
2021-08-03 23:18:20 +00:00
ctx . Error ( "error when parsing request" , fasthttp . StatusBadRequest )
2020-04-30 02:03:05 +00:00
}
}