about summary refs log tree commit diff stats
path: root/src/io
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-01-01 19:52:49 +0100
committerbptato <nincsnevem662@gmail.com>2023-01-01 20:02:22 +0100
commit5da4c6e629dd4a0647fd3fe06906445eea65b867 (patch)
treec116a41e32444512cd13e4c18327f65c8d868fad /src/io
parent1a24350ee44859f6b3370ac15e8875a79fa7ae9b (diff)
downloadchawan-5da4c6e629dd4a0647fd3fe06906445eea65b867.tar.gz
loader: proper redirect handling
Diffstat (limited to 'src/io')
-rw-r--r--src/io/loader.nim17
-rw-r--r--src/io/request.nim2
2 files changed, 16 insertions, 3 deletions
diff --git a/src/io/loader.nim b/src/io/loader.nim
index 71cfd2ff..4931d40b 100644
--- a/src/io/loader.nim
+++ b/src/io/loader.nim
@@ -130,9 +130,22 @@ proc doRequest*(loader: FileLoader, request: Request, blocking = true): Response
     else:
       result.contenttype = guessContentType($request.url.path)
     if "Location" in result.headers.table:
-      if result.status in [201, 301, 302, 303, 307, 308]:
+      if result.status in 301..303 or result.status in 307..308:
         let location = result.headers.table["Location"][0]
-        result.redirect = parseUrl(location, some(request.url))
+        let url = parseUrl(location, some(request.url))
+        if url.isSome:
+          if (result.status == 303 and
+              request.httpmethod notin {HTTP_GET, HTTP_HEAD}) or
+              (result.status == 301 or result.status == 302 and
+              request.httpmethod == HTTP_POST):
+            result.redirect = newRequest(url.get, HTTP_GET,
+              mode = request.mode, credentialsMode = request.credentialsMode,
+              destination = request.destination)
+          else:
+            result.redirect = newRequest(url.get, request.httpmethod,
+              body = request.body, multipart = request.multipart,
+              mode = request.mode, credentialsMode = request.credentialsMode,
+              destination = request.destination)
     # Only a stream of the response body may arrive after this point.
     result.body = stream
     if not blocking:
diff --git a/src/io/request.nim b/src/io/request.nim
index 79ba7c0d..8837a1f9 100644
--- a/src/io/request.nim
+++ b/src/io/request.nim
@@ -51,7 +51,7 @@ type
     contenttype* {.jsget.}: string
     status* {.jsget.}: int
     headers* {.jsget.}: HeaderList
-    redirect* {.jsget.}: Option[Url]
+    redirect*: Request
  
   ReadableStream* = ref object of Stream
     isource*: Stream