about summary refs log tree commit diff stats
path: root/src/io
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-12-13 17:03:17 +0100
committerbptato <nincsnevem662@gmail.com>2022-12-13 17:03:17 +0100
commit4b482418c22ea31729ca94583ffd2a6aa167d811 (patch)
tree9c456555f1b3cfd45d90f28d61d7369ab96e1c5d /src/io
parent672ab553c4a2b10a703ea40e049eda52db149a93 (diff)
downloadchawan-4b482418c22ea31729ca94583ffd2a6aa167d811.tar.gz
Fix stream error handling confusion, title display
Also probably other fixes.
Diffstat (limited to 'src/io')
-rw-r--r--src/io/http.nim5
-rw-r--r--src/io/loader.nim3
-rw-r--r--src/io/posixstream.nim33
-rw-r--r--src/io/urlfilter.nim2
4 files changed, 28 insertions, 15 deletions
diff --git a/src/io/http.nim b/src/io/http.nim
index bfd12a0b..36cd6473 100644
--- a/src/io/http.nim
+++ b/src/io/http.nim
@@ -54,12 +54,9 @@ proc curlWriteHeader(p: cstring, size: csize_t, nitems: csize_t, userdata: point
   return nitems
 
 proc curlWriteBody(p: cstring, size: csize_t, nmemb: csize_t, userdata: pointer): csize_t {.cdecl.} =
-  var s = newString(nmemb)
-  for i in 0..<nmemb:
-    s[i] = p[i]
   let stream = cast[Stream](userdata)
   if nmemb > 0:
-    stream.write(s)
+    stream.writeData(p, int(nmemb))
     stream.flush()
   return nmemb
 
diff --git a/src/io/loader.nim b/src/io/loader.nim
index 0b0f5531..3e8baaf0 100644
--- a/src/io/loader.nim
+++ b/src/io/loader.nim
@@ -127,9 +127,8 @@ proc runFileLoader*(fd: cint, defaultHeaders: HeaderList, filter: URLFilter, coo
       of QUIT:
         stream.close()
         break
-    except IOError:
+    except EOFError:
       # End-of-file, quit.
-      # TODO this should be EOFError
       break
     stream.close()
   curl_global_cleanup()
diff --git a/src/io/posixstream.nim b/src/io/posixstream.nim
index 9a40ce9b..b686be9b 100644
--- a/src/io/posixstream.nim
+++ b/src/io/posixstream.nim
@@ -5,6 +5,7 @@ import streams
 type
   PosixStream* = ref object of Stream
     fd*: FileHandle
+    isend*: bool
 
   ErrorAgain* = object of IOError
   ErrorWouldBlock* = object of IOError
@@ -14,17 +15,29 @@ type
   ErrorInvalid* = object of IOError
 
 proc psReadData(s: Stream, buffer: pointer, len: int): int =
+  assert len != 0
   let s = cast[PosixStream](s)
-  result = read(s.fd, buffer, len)
+  while result < len:
+    let n = read(s.fd, buffer, len)
+    if n < 0:
+      if result == 0:
+        result = n
+      break
+    elif n == 0:
+      s.isend = true
+      break
+    result += n
+  if result == 0:
+    raise newException(EOFError, "eof")
   if result == -1:
     if errno == EAGAIN:
-      raise newException(ErrorAgain, "")
+      raise newException(ErrorAgain, "eagain")
     case errno
-    of EWOULDBLOCK: raise newException(ErrorWouldBlock, "")
-    of EBADF: raise newException(ErrorBadFD, "")
-    of EFAULT: raise newException(ErrorFault, "")
-    of EINVAL: raise newException(ErrorInvalid, "")
-    else: raise newException(IOError, $strerror(errno))
+    of EWOULDBLOCK: raise newException(ErrorWouldBlock, "would block")
+    of EBADF: raise newException(ErrorBadFD, "bad fd")
+    of EFAULT: raise newException(ErrorFault, "fault")
+    of EINVAL: raise newException(ErrorInvalid, "invalid")
+    else: raise newException(IOError, $strerror(errno) & " (" & $errno & ")")
 
 proc psWriteData(s: Stream, buffer: pointer, len: int) =
   let s = cast[PosixStream](s)
@@ -32,9 +45,13 @@ proc psWriteData(s: Stream, buffer: pointer, len: int) =
   if res == -1:
     raise newException(IOError, $strerror(errno))
 
+proc psAtEnd(s: Stream): bool =
+  return cast[PosixStream](s).isend
+
 proc newPosixStream*(fd: FileHandle): PosixStream =
   return PosixStream(
     fd: fd,
     readDataImpl: psReadData,
-    writeDataImpl: psWriteData
+    writeDataImpl: psWriteData,
+    atEndImpl: psAtEnd
   )
diff --git a/src/io/urlfilter.nim b/src/io/urlfilter.nim
index 29dad8c6..28bdef28 100644
--- a/src/io/urlfilter.nim
+++ b/src/io/urlfilter.nim
@@ -6,7 +6,7 @@ import types/url
 #TODO add denyhost/s for blocklists
 type URLFilter* = object
   scheme: Option[string]
-  allowhost: Option[string]
+  allowhost*: Option[string]
   allowhosts: Option[seq[Regex]]
   default: bool