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