about summary refs log tree commit diff stats
path: root/adapter/protocol
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-12-21 17:37:33 +0100
committerbptato <nincsnevem662@gmail.com>2023-12-21 17:49:46 +0100
commit770a7116c39ebde9433b973c2fa881f60f8804a9 (patch)
treea1750d50c46ff014b9694554e21a0e03688ee1ac /adapter/protocol
parent5e4e35899029c91597ac78eb27be488fdcd7f69b (diff)
downloadchawan-770a7116c39ebde9433b973c2fa881f60f8804a9.tar.gz
file: do not use streams
Diffstat (limited to 'adapter/protocol')
-rw-r--r--adapter/protocol/file.nim42
1 files changed, 19 insertions, 23 deletions
diff --git a/adapter/protocol/file.nim b/adapter/protocol/file.nim
index 3513ab3c..67bea420 100644
--- a/adapter/protocol/file.nim
+++ b/adapter/protocol/file.nim
@@ -1,6 +1,5 @@
 import std/algorithm
 import std/os
-import std/streams
 import std/times
 
 import dirlist
@@ -79,20 +78,18 @@ Symbolic link to <A HREF="""" & sl & """">""" & sl & """</A></br>
 </BODY>
 </HTML>""")
 
-proc loadFile(istream: Stream) =
+proc loadFile(f: File) =
   # No headers, we'll let the browser figure out the file type.
   stdout.write("\n")
-  let outs = newFileStream(stdout)
-  while not istream.atEnd:
-    const BufferSize = 16384
-    var buffer {.noinit.}: array[BufferSize, char]
-    while true:
-      let n = readData(istream, addr buffer[0], BufferSize)
-      if n == 0:
-        break
-      outs.writeData(addr buffer[0], n)
-      if n < BufferSize:
-        break
+  const BufferSize = 16384
+  var buffer {.noinit.}: array[BufferSize, char]
+  while true:
+    let n = readBuffer(f, addr buffer[0], BufferSize)
+    if n == 0:
+      break
+    let n2 = stdout.writeBuffer(addr buffer[0], n)
+    if n2 < n or n < BufferSize:
+      break
 
 proc main() =
   if getEnv("MAPPED_URI_HOST") != "":
@@ -102,16 +99,15 @@ proc main() =
       return
   let opath = getEnv("MAPPED_URI_PATH")
   let path = percentDecode(opath)
-  let istream = newFileStream(path, fmRead)
-  if istream == nil:
-    if dirExists(path):
-      loadDir(path, opath)
-    elif symlinkExists(path):
-      loadSymlink(path)
-    else:
-      let code = int(ERROR_FILE_NOT_FOUND)
-      stdout.write("Cha-Control: ConnectionError " & $code)
+  var f: File
+  if f.open(path, fmRead):
+    loadFile(f)
+  elif dirExists(path):
+    loadDir(path, opath)
+  elif symlinkExists(path):
+    loadSymlink(path)
   else:
-    loadFile(istream)
+    let code = int(ERROR_FILE_NOT_FOUND)
+    stdout.write("Cha-Control: ConnectionError " & $code)
 
 main()