From 2a5095505f771610f9559d2e774b2a9561f01101 Mon Sep 17 00:00:00 2001 From: Eugene Kabanov Date: Fri, 17 Jun 2022 12:17:49 +0300 Subject: [PATCH] Add headers argument to redirect() calls. (#287) * Add headers argument to redirect() calls. * Fix mistype. --- chronos/apps/http/httpserver.nim | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/chronos/apps/http/httpserver.nim b/chronos/apps/http/httpserver.nim index e09b532..104c9be 100644 --- a/chronos/apps/http/httpserver.nim +++ b/chronos/apps/http/httpserver.nim @@ -1322,17 +1322,35 @@ proc respond*(req: HttpRequestRef, code: HttpCode): Future[HttpResponseRef] = ## Responds to the request with specified ``HttpCode`` only. respond(req, code, "", HttpTable.init()) +proc redirect*(req: HttpRequestRef, code: HttpCode, + location: string, headers: HttpTable): Future[HttpResponseRef] = + ## Responds to the request with redirection to location ``location`` and + ## additional headers ``headers``. + ## + ## Note, ``location`` argument's value has priority over "Location" header's + ## value in ``headers`` argument. + var mheaders = headers + mheaders.set("location", location) + respond(req, code, "", mheaders) + +proc redirect*(req: HttpRequestRef, code: HttpCode, + location: Uri, headers: HttpTable): Future[HttpResponseRef] = + ## Responds to the request with redirection to location ``location`` and + ## additional headers ``headers``. + ## + ## Note, ``location`` argument's value has priority over "Location" header's + ## value in ``headers`` argument. + redirect(req, code, $location, headers) + proc redirect*(req: HttpRequestRef, code: HttpCode, location: Uri): Future[HttpResponseRef] = ## Responds to the request with redirection to location ``location``. - let headers = HttpTable.init([("location", $location)]) - respond(req, code, "", headers) + redirect(req, code, location, HttpTable.init()) proc redirect*(req: HttpRequestRef, code: HttpCode, location: string): Future[HttpResponseRef] = ## Responds to the request with redirection to location ``location``. - let headers = HttpTable.init([("location", location)]) - respond(req, code, "", headers) + redirect(req, code, location, HttpTable.init()) proc responded*(req: HttpRequestRef): bool = ## Returns ``true`` if request ``req`` has been responded or responding.