diff options
author | bptato <nincsnevem662@gmail.com> | 2022-08-18 19:42:08 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2022-08-18 19:42:43 +0200 |
commit | 18273ea45d45558641a1020932b141ed0d165e12 (patch) | |
tree | aac7216cd18430a7539f216c545630a9e2d4ae49 /src/io/loader.nim | |
parent | a78c26198e747d4de887e1f582d29f14fb59391b (diff) | |
download | chawan-18273ea45d45558641a1020932b141ed0d165e12.tar.gz |
Clean up client request methods, add DUPE_BUFFER
Diffstat (limited to 'src/io/loader.nim')
-rw-r--r-- | src/io/loader.nim | 44 |
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. |