about summary refs log tree commit diff stats
path: root/src/io/loader.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-08-18 19:42:08 +0200
committerbptato <nincsnevem662@gmail.com>2022-08-18 19:42:43 +0200
commit18273ea45d45558641a1020932b141ed0d165e12 (patch)
treeaac7216cd18430a7539f216c545630a9e2d4ae49 /src/io/loader.nim
parenta78c26198e747d4de887e1f582d29f14fb59391b (diff)
downloadchawan-18273ea45d45558641a1020932b141ed0d165e12.tar.gz
Clean up client request methods, add DUPE_BUFFER
Diffstat (limited to 'src/io/loader.nim')
-rw-r--r--src/io/loader.nim44
1 files changed, 25 insertions, 19 deletions
diff --git a/src/io/loader.nim b/src/io/loader.nim
index 74a692fe..1fb6e7b7 100644
--- a/src/io/loader.nim
+++ b/src/io/loader.nim
@@ -40,27 +40,30 @@ proc loadFile(url: Url, ostream: Stream) =
   else:
     let path = url.path.serialize_unicode()
   let istream = newFileStream(path, fmRead)
-  ostream.swrite(if istream != nil:
-    200 # ok
-  else:
-    404 # file not found
-  )
-  ostream.swrite(guessContentType(path))
-  ostream.swrite(none(Url))
-  while not istream.atEnd:
-    const bufferSize = 4096
-    var buffer {.noinit.}: array[bufferSize, char]
-    while true:
-      let n = readData(istream, addr buffer[0], bufferSize)
-      if n == 0:
-        break
-      ostream.swrite(n)
-      ostream.writeData(addr buffer[0], n)
-      ostream.flush()
-      if n < bufferSize:
-        break
+  if istream == nil:
+    ostream.swrite(404) # file not found
+    ostream.swrite("")
+    ostream.swrite(none(Url))
     ostream.swrite("")
     ostream.flush()
+  else:
+    ostream.swrite(200) # ok
+    ostream.swrite(guessContentType(path))
+    ostream.swrite(none(Url))
+    while not istream.atEnd:
+      const bufferSize = 4096
+      var buffer {.noinit.}: array[bufferSize, char]
+      while true:
+        let n = readData(istream, addr buffer[0], bufferSize)
+        if n == 0:
+          break
+        ostream.swrite(n)
+        ostream.writeData(addr buffer[0], n)
+        ostream.flush()
+        if n < bufferSize:
+          break
+      ostream.swrite("")
+      ostream.flush()
 
 proc loadResource(loader: FileLoader, request: Request, ostream: Stream) =
   case request.url.scheme
@@ -78,6 +81,9 @@ proc runFileLoader(loader: FileLoader) =
   while true:
     try:
       let request = istream.readRequest()
+      for k, v in loader.defaultHeaders:
+        if k notin request.headers.table:
+          request.headers[k] = v
       loader.loadResource(request, ostream)
     except IOError:
       # End-of-file, quit.