about summary refs log tree commit diff stats
path: root/src/io/loader.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-10-19 11:24:45 +0200
committerbptato <nincsnevem662@gmail.com>2022-10-19 11:25:56 +0200
commit08a758ed7a06e1bff8994c01d6c5d317d400ccf9 (patch)
treecb53cf39ebd323491715eb569cc509c00edcae91 /src/io/loader.nim
parentc4e2de9cd8cad7e28b33e68b1b76f9044fe510be (diff)
downloadchawan-08a758ed7a06e1bff8994c01d6c5d317d400ccf9.tar.gz
Implement tree buffers, fix a js bug, refactor
Diffstat (limited to 'src/io/loader.nim')
-rw-r--r--src/io/loader.nim23
1 files changed, 10 insertions, 13 deletions
diff --git a/src/io/loader.nim b/src/io/loader.nim
index 1530bf5b..7e6faab4 100644
--- a/src/io/loader.nim
+++ b/src/io/loader.nim
@@ -76,12 +76,18 @@ proc loadResource(loader: FileLoader, request: Request, ostream: Stream) =
     ostream.swrite(-1) # error
     ostream.flush()
 
-proc runFileLoader(loader: FileLoader, loadcb: proc()) =
+proc runFileLoader(loader: FileLoader, fd: cint) =
   if curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK:
     raise newException(Defect, "Failed to initialize libcurl.")
   let ssock = initServerSocket(getpid())
   # The server has been initialized, so the main process can resume execution.
-  loadcb()
+  var writef: File
+  if not open(writef, FileHandle(fd), fmWrite):
+    raise newException(Defect, "Failed to open input handle.")
+  writef.write(char(0u8))
+  writef.flushFile()
+  close(writef)
+  discard close(fd)
   while true:
     let stream = ssock.acceptSocketStream()
     try:
@@ -111,7 +117,7 @@ proc doRequest*(loader: FileLoader, request: Request): Response =
     if "Content-Type" in result.headers.table:
       result.contenttype = result.headers.table["Content-Type"][0].until(';')
     else:
-      result.contenttype = guessContentType($request.url)
+      result.contenttype = guessContentType($request.url.path)
     if "Location" in result.headers.table:
       let location = result.headers.table["Location"][0]
       result.redirect = parseUrl(location, some(request.url))
@@ -131,15 +137,7 @@ proc newFileLoader*(defaultHeaders: HeaderList): FileLoader =
     elif pid == 0:
       # child process
       discard close(pipefd[0]) # close read
-      var writef: File
-      if not open(writef, FileHandle(pipefd[1]), fmWrite):
-        raise newException(Defect, "Failed to open input handle.")
-      result.runFileLoader((proc() =
-        writef.write(char(0u8))
-        writef.flushFile()
-        close(writef)
-        discard close(pipefd[1])
-      ))
+      result.runFileLoader(pipefd[1])
     else:
       result.process = pid
       let readfd = pipefd[0] # get read
@@ -150,7 +148,6 @@ proc newFileLoader*(defaultHeaders: HeaderList): FileLoader =
       assert readf.readChar() == char(0u8)
       close(readf)
       discard close(pipefd[0])
-      
 
 proc newFileLoader*(): FileLoader =
   newFileLoader(DefaultHeaders)