about summary refs log tree commit diff stats
path: root/src/loader
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-12-21 18:06:49 +0100
committerbptato <nincsnevem662@gmail.com>2023-12-21 18:15:42 +0100
commit98fd959625c2f9227066fdd43590af12c8f40e91 (patch)
tree989981cc35d94999030d4d822dbfa72cd469da7e /src/loader
parent140e1c494d4fb340c73144656f270e4a4d363698 (diff)
downloadchawan-98fd959625c2f9227066fdd43590af12c8f40e91.tar.gz
loader: refactor onRead/onError
It was originally written this way to accomodate for the broken std
file API. We no longer use that in buffer, so we can use a more correct
version now.
Diffstat (limited to 'src/loader')
-rw-r--r--src/loader/loader.nim41
1 files changed, 22 insertions, 19 deletions
diff --git a/src/loader/loader.nim b/src/loader/loader.nim
index 8c39fe94..44d6034b 100644
--- a/src/loader/loader.nim
+++ b/src/loader/loader.nim
@@ -70,7 +70,6 @@ type
 
   OngoingData = object
     buf: string
-    readbufsize: int
     response: Response
     bodyRead: Promise[string]
 
@@ -455,7 +454,6 @@ proc onConnected*(loader: FileLoader, fd: int) =
       realCloseImpl(stream)
     loader.ongoing[fd] = OngoingData(
       response: response,
-      readbufsize: BufferSize,
       bodyRead: response.bodyRead
     )
     SocketStream(stream).source.getFd().setBlocking(false)
@@ -474,28 +472,33 @@ proc onConnected*(loader: FileLoader, fd: int) =
 proc onRead*(loader: FileLoader, fd: int) =
   loader.ongoing.withValue(fd, buffer):
     let response = buffer[].response
-    while true:
-      let olen = buffer[].buf.len
-      buffer[].buf.setLen(olen + buffer.readbufsize)
+    while not response.body.atEnd():
       try:
-        let n = response.body.readData(addr buffer[].buf[olen],
-          buffer.readbufsize)
-        if n != 0:
-          if buffer[].readbufsize < BufferSize:
-            buffer[].readbufsize = min(BufferSize, buffer[].readbufsize * 2)
+        let olen = buffer[].buf.len
+        buffer[].buf.setLen(olen + BufferSize)
+        let n = response.body.readData(addr buffer[].buf[olen], BufferSize)
         buffer[].buf.setLen(olen + n)
-        if response.body.atEnd():
-          buffer[].bodyRead.resolve(buffer[].buf)
-          buffer[].bodyRead = nil
-          buffer[].buf = ""
-          response.unregisterFun()
-        break
       except ErrorAgain, ErrorWouldBlock:
-        assert buffer.readbufsize > 1
-        buffer.readbufsize = buffer.readbufsize div 2
+        break
+    if response.body.atEnd():
+      buffer[].bodyRead.resolve(buffer[].buf)
+      buffer[].bodyRead = nil
+      buffer[].buf = ""
+      response.unregisterFun()
 
 proc onError*(loader: FileLoader, fd: int) =
-  loader.onRead(fd)
+  loader.ongoing.withValue(fd, buffer):
+    let response = buffer[].response
+    when defined(debug):
+      var lbuf {.noInit.}: array[BufferSize, char]
+      if not response.body.atEnd():
+        let n = response.body.readData(addr lbuf[0], lbuf.len)
+        assert n == 0
+      assert response.body.atEnd()
+    buffer[].bodyRead.resolve(buffer[].buf)
+    buffer[].bodyRead = nil
+    buffer[].buf = ""
+    response.unregisterFun()
 
 proc doRequest*(loader: FileLoader, request: Request, blocking = true,
     canredir = false): Response =