about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-02-13 15:38:51 +0100
committerbptato <nincsnevem662@gmail.com>2023-02-13 15:38:51 +0100
commite4ecff6f3ff5e0ab617627f8637e4c3179cc4caf (patch)
treed2efd123885e835fc18812bf17a22984690d5b49 /src
parent81290d7e577701f37a1c4559f0a7777b904e7ed7 (diff)
downloadchawan-e4ecff6f3ff5e0ab617627f8637e4c3179cc4caf.tar.gz
buffer: adapt onload to PosixStream and SocketStream
Diffstat (limited to 'src')
-rw-r--r--src/buffer/buffer.nim29
1 files changed, 14 insertions, 15 deletions
diff --git a/src/buffer/buffer.nim b/src/buffer/buffer.nim
index 51c09616..04304afb 100644
--- a/src/buffer/buffer.nim
+++ b/src/buffer/buffer.nim
@@ -652,21 +652,20 @@ proc onload(buffer: Buffer) =
   try:
     buffer.sstream.setPosition(op + buffer.available)
     let n = buffer.istream.readData(addr s[0], buffer.readbufsize)
-    assert n != 0
-    s.setLen(n)
-    buffer.sstream.setPosition(op)
-    if buffer.readbufsize < BufferSize:
-      buffer.readbufsize = min(BufferSize, buffer.readbufsize * 2)
-    buffer.available += s.len
-    case buffer.contenttype
-    of "text/html":
-      res.bytes = buffer.available
-    else:
-      buffer.do_reshape()
-    buffer.resolveTask(LOAD, res)
-  except EOFError:
-    res.atend = true
-    buffer.finishLoad()
+    if n != 0: # n can be 0 if we get EOF. (in which case we shouldn't reshape unnecessarily.)
+      s.setLen(n)
+      buffer.sstream.setPosition(op)
+      if buffer.readbufsize < BufferSize:
+        buffer.readbufsize = min(BufferSize, buffer.readbufsize * 2)
+      buffer.available += s.len
+      case buffer.contenttype
+      of "text/html":
+        res.bytes = buffer.available
+      else:
+        buffer.do_reshape()
+    if buffer.istream.atEnd():
+      res.atend = true
+      buffer.finishLoad()
     buffer.resolveTask(LOAD, res)
   except ErrorAgain, ErrorWouldBlock:
     if buffer.readbufsize > 1: