From 9504fedfc26135971f91b927ba9c2c23ea77501b Mon Sep 17 00:00:00 2001 From: itsumura-h <39766805+itsumura-h@users.noreply.github.com> Date: Tue, 5 Mar 2024 23:57:04 +0900 Subject: [PATCH] fix 404 error message (#288) --- src/basolato/core/libservers/nostd/server.nim | 20 ++++++++++++++++--- src/basolato/core/libservers/std/server.nim | 13 +++++++----- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/basolato/core/libservers/nostd/server.nim b/src/basolato/core/libservers/nostd/server.nim index a6711ee8..352609ec 100644 --- a/src/basolato/core/libservers/nostd/server.nim +++ b/src/basolato/core/libservers/nostd/server.nim @@ -78,7 +78,8 @@ proc serve*(seqRoutes:seq[Routes], port=5000) = let msg = getCurrentExceptionMsg() let status = Http500 response = Response.new(status, errorPage(status, msg), headers) - echoErrorMsg(&"{$response.status} {req.hostname} {$req.httpMethod} {req.path}") + let userAgent = req.headers["User-Agent"] + echoErrorMsg(&"{$response.status} {$req.httpMethod} {req.path} {req.hostname} {userAgent}") echoErrorMsg(msg) # except: @@ -104,11 +105,24 @@ proc serve*(seqRoutes:seq[Routes], port=5000) = # echoErrorMsg(&"{$response.status} {$req.httpMethod} {req.path}") # echoErrorMsg(exception.msg) - if response.status == HttpCode(0): + if response.status.is4xx: var headers = newHttpHeaders() headers["content-type"] = "text/html; charset=utf-8" + let userAgent = req.headers["User-Agent"] + echoErrorMsg(&"{$response.status} {$req.httpMethod} {req.path} {req.hostname} {userAgent}") + response = Response.new(response.status, errorPage(response.status, response.body), headers) + elif response.status.is5xx: + var headers = newHttpHeaders() + headers["content-type"] = "text/html; charset=utf-8" + let userAgent = req.headers["User-Agent"] + echoErrorMsg(&"{$response.status} {$req.httpMethod} {req.path} {req.hostname} {userAgent}") + response = Response.new(response.status, errorPage(response.status, response.body), headers) + elif response.status == HttpCode(0): + var headers = newHttpHeaders() + headers["content-type"] = "text/html; charset=utf-8" + let userAgent = req.headers["User-Agent"] + echoErrorMsg(&"{$Http404} {$req.httpMethod} {req.path} {req.hostname} {userAgent}") response = Response.new(Http404, errorPage(Http404, ""), headers) - echoErrorMsg(&"{$response.status} {$req.httpMethod} {req.path}") when defined(httpbeast): req.send(response.status, response.body, response.headers.format().toString()) diff --git a/src/basolato/core/libservers/std/server.nim b/src/basolato/core/libservers/std/server.nim index d16ac414..058c55ff 100644 --- a/src/basolato/core/libservers/std/server.nim +++ b/src/basolato/core/libservers/std/server.nim @@ -71,7 +71,8 @@ proc serveCore(params:(Routes, int)){.async.} = let msg = getCurrentExceptionMsg() let status = Http500 response = Response.new(status, errorPage(status, msg), headers) - echoErrorMsg(&"{$response.status} {req.hostname} {$req.httpMethod} {req.path}") + let userAgent = req.headers["User-Agent"] + echoErrorMsg(&"{$response.status} {$req.httpMethod} {req.path} {req.hostname} {userAgent}") echoErrorMsg(msg) # except: @@ -101,17 +102,20 @@ proc serveCore(params:(Routes, int)){.async.} = if response.status.is4xx: var headers = newHttpHeaders() headers["content-type"] = "text/html; charset=utf-8" - echoErrorMsg(&"{$response.status} {req.hostname} {$req.httpMethod} {req.path}") + let userAgent = req.headers["User-Agent"] + echoErrorMsg(&"{$response.status} {$req.httpMethod} {req.path} {req.hostname} {userAgent}") response = Response.new(response.status, errorPage(response.status, response.body), headers) elif response.status.is5xx: var headers = newHttpHeaders() headers["content-type"] = "text/html; charset=utf-8" - echoErrorMsg(&"{$response.status} {req.hostname} {$req.httpMethod} {req.path}") + let userAgent = req.headers["User-Agent"] + echoErrorMsg(&"{$response.status} {$req.httpMethod} {req.path} {req.hostname} {userAgent}") response = Response.new(response.status, errorPage(response.status, response.body), headers) elif response.status == HttpCode(0): var headers = newHttpHeaders() headers["content-type"] = "text/html; charset=utf-8" - echoErrorMsg(&"{$response.status} {req.hostname} {$req.httpMethod} {req.path}") + let userAgent = req.headers["User-Agent"] + echoErrorMsg(&"{$Http404} {$req.httpMethod} {req.path} {req.hostname} {userAgent}") response = Response.new(Http404, errorPage(Http404, ""), headers) response.headers.setDefaultHeaders() @@ -129,7 +133,6 @@ proc serveCore(params:(Routes, int)){.async.} = # wait 500ms for FDs to be closed await sleepAsync(500) - # asyncCheck server.serve(Port(port), cb, HOST_ADDR) proc serve*(seqRoutes: seq[Routes]) = var routes = Routes.new()