Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
itsumura-h committed Feb 14, 2024
1 parent 9ab6b16 commit 164ebf4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
62 changes: 40 additions & 22 deletions src/basolato/core/libservers/std/server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import std/strformat
import std/tables
import std/times
import std/mimetypes
import ../../base
import ../../baseEnv
import ../../error_page
import ../../header
Expand Down Expand Up @@ -57,29 +58,46 @@ proc serveCore(params:(Routes, int)){.async.} =

if req.httpMethod == HttpHead:
response.setBody("")
except:
except DD:
var headers = newHttpHeaders()
headers["content-type"] = "text/html; charset=utf-8"
let exception = getCurrentException()
echo "exception.name: ",exception.name
if exception.name == "DD".cstring:
var msg = exception.msg
msg = msg.replace(re"Async traceback:[.\s\S]*")
response = Response.new(Http200, ddPage(msg), headers)
elif exception.name == "ErrorAuthRedirect".cstring:
headers["location"] = exception.msg
headers["set-cookie"] = "session_id=; expires=31-Dec-1999 23:59:59 GMT" # Delete session id
response = Response.new(Http302, "", headers)
elif exception.name == "ErrorRedirect".cstring:
headers["location"] = exception.msg
response = Response.new(Http302, "", headers)
elif exception.name == "ErrorHttpParse".cstring:
response = Response.new(Http501, "", headers)
else:
let status = checkHttpCode(exception)
response = Response.new(status, errorPage(status, exception.msg), headers)
echoErrorMsg(&"{$response.status} {req.hostname} {$req.httpMethod} {req.path}")
echoErrorMsg(exception.msg)
var msg = getCurrentExceptionMsg()
msg = msg.replace(re"Async traceback:[.\s\S]*")
response = Response.new(Http200, ddPage(msg), headers)
except ErrorHttpParse:
var headers = newHttpHeaders()
response = Response.new(Http501, "", headers)
except:
var headers = newHttpHeaders()
let msg = getCurrentExceptionMsg()
let status = Http500
response = Response.new(status, errorPage(status, msg), headers)
echoErrorMsg(&"{$response.status} {req.hostname} {$req.httpMethod} {req.path}")
echoErrorMsg(msg)

# except:
# var headers = newHttpHeaders()
# headers["content-type"] = "text/html; charset=utf-8"
# let exception = getCurrentException()
# echo "exception.name: ",exception.name
# if exception.name == "DD".cstring:
# var msg = exception.msg
# msg = msg.replace(re"Async traceback:[.\s\S]*")
# response = Response.new(Http200, ddPage(msg), headers)
# elif exception.name == "ErrorAuthRedirect".cstring:
# headers["location"] = exception.msg
# headers["set-cookie"] = "session_id=; expires=31-Dec-1999 23:59:59 GMT" # Delete session id
# response = Response.new(Http302, "", headers)
# elif exception.name == "ErrorRedirect".cstring:
# headers["location"] = exception.msg
# response = Response.new(Http302, "", headers)
# elif exception.name == "ErrorHttpParse".cstring:
# response = Response.new(Http501, "", headers)
# else:
# let status = checkHttpCode(exception)
# response = Response.new(status, errorPage(status, exception.msg), headers)
# echoErrorMsg(&"{$response.status} {req.hostname} {$req.httpMethod} {req.path}")
# echoErrorMsg(exception.msg)

if response.status.is4xx:
var headers = newHttpHeaders()
Expand Down Expand Up @@ -111,7 +129,7 @@ proc serveCore(params:(Routes, int)){.async.} =
# too many concurrent connections, `maxFDs` exceeded
# wait 500ms for FDs to be closed
await sleepAsync(500)

# asyncCheck server.serve(Port(port), cb, HOST_ADDR)

proc serve*(seqRoutes: seq[Routes]) =
Expand Down
2 changes: 1 addition & 1 deletion src/basolato/middleware.nim
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func next*(status=HttpCode(0), body="", headers:HttpHeaders=newHttpHeaders()):Re

proc checkCsrfToken*(request:Request, params:Params):Future[MiddlewareResult] {.async.} =
result = MiddlewareResult()
if request.httpMethod == HttpPost and not request.headers["Content-Type"].contains("application/json"):
if request.httpMethod == HttpPost and not (request.headers.hasKey("content-type") and request.headers["content-type"].contains("application/json")):
try:
if not params.hasKey("csrf_token"):
raise newException(Exception, "csrf token is missing")
Expand Down

0 comments on commit 164ebf4

Please sign in to comment.