about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-06-19 21:08:02 +0200
committerbptato <nincsnevem662@gmail.com>2023-06-19 21:08:02 +0200
commit251b94699e0be4ec9f25257e6200fb2a1285796c (patch)
treeed607a66dd5a0faec30fc8764b1bdbb863686f0e
parentb3e08ac443d5d010938be7ae9b9bf8c04ca55300 (diff)
downloadchawan-251b94699e0be4ec9f25257e6200fb2a1285796c.tar.gz
Add Response.ok, url, fix type confusion with status
-rw-r--r--src/io/http.nim4
-rw-r--r--src/io/loader.nim10
-rw-r--r--src/io/response.nim10
3 files changed, 17 insertions, 7 deletions
diff --git a/src/io/http.nim b/src/io/http.nim
index cd6234fd..eaff6aa7 100644
--- a/src/io/http.nim
+++ b/src/io/http.nim
@@ -59,9 +59,9 @@ proc curlWriteHeader(p: cstring, size: csize_t, nitems: csize_t, userdata: point
       op.ostream.swrite(int(CURLE_OK))
     except IOError: # Broken pipe
       return 0
-    var status: int
+    var status: clong
     op.curl.getinfo(CURLINFO_RESPONSE_CODE, addr status)
-    op.ostream.swrite(status)
+    op.ostream.swrite(cast[int](status))
     return nitems
 
   let k = line.until(':')
diff --git a/src/io/loader.nim b/src/io/loader.nim
index 9f2682d9..acc432ec 100644
--- a/src/io/loader.nim
+++ b/src/io/loader.nim
@@ -234,7 +234,7 @@ proc applyHeaders(request: Request, response: Response) =
   else:
     response.contenttype = guessContentType($response.url.path)
   if "Location" in response.headers.table:
-    if response.status in 301..303 or response.status in 307..308:
+    if response.status in 301u16..303u16 or response.status in 307u16..308u16:
       let location = response.headers.table["Location"][0]
       let url = parseUrl(location, option(request.url))
       if url.isSome:
@@ -286,7 +286,9 @@ proc onConnected*(loader: FileLoader, fd: int) =
       loader.unregistered.add(fd)
       loader.unregisterFun(fd)
       realCloseImpl(stream)
-    stream.sread(response.status)
+    var status: int
+    stream.sread(status)
+    response.status = cast[uint16](status)
     stream.sread(response.headers)
     applyHeaders(request, response)
     response.body = stream
@@ -336,7 +338,9 @@ proc doRequest*(loader: FileLoader, request: Request, blocking = true): Response
   stream.flush()
   stream.sread(result.res)
   if result.res == 0:
-    stream.sread(result.status)
+    var status: int
+    stream.sread(status)
+    result.status = cast[uint16](status)
     stream.sread(result.headers)
     applyHeaders(request, result)
     # Only a stream of the response body may arrive after this point.
diff --git a/src/io/response.nim b/src/io/response.nim
index ae418318..2c336a8c 100644
--- a/src/io/response.nim
+++ b/src/io/response.nim
@@ -8,12 +8,12 @@ import types/url
 
 type
   Response* = ref object
+    res*: int
     fd*: int
     body*: Stream
     bodyUsed* {.jsget.}: bool
-    res*: int
     contenttype* {.jsget.}: string
-    status* {.jsget.}: int
+    status* {.jsget.}: uint16
     headers* {.jsget.}: Headers
     redirect*: Request
     url*: URL #TODO should be urllist?
@@ -30,6 +30,12 @@ proc newResponse*(res: int, request: Request, fd = -1, stream: Stream = nil):
     fd: fd
   )
 
+func sok(response: Response): bool {.jsfget: "ok".} =
+  return response.status in 200u16 .. 299u16
+
+func surl(response: Response): string {.jsfget: "url".} =
+  return $response.url
+
 #TODO: this should be a property of body
 proc close*(response: Response) {.jsfunc.} =
   response.bodyUsed = true