about summary refs log tree commit diff stats
path: root/src/loader/loader.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-01-26 03:01:49 +0100
committerbptato <nincsnevem662@gmail.com>2024-01-26 03:01:49 +0100
commit1c0df44ae9d9ac498ff6335f044ff5294fd62441 (patch)
tree03869a9c9ddb43000987c720322835ebcbd76bc7 /src/loader/loader.nim
parent9b5df91240ea3e38e58d771597cb2a2c3ca95f29 (diff)
downloadchawan-1c0df44ae9d9ac498ff6335f044ff5294fd62441.tar.gz
loader: clean up error handling
* remove pointless exception -> bool conversions; usually they were
  ignored anyway + exceptions are more convenient here
* add EPIPE handler to raisePosixIOError
* fix socketstream to use raisePosixIOError
* fix socketstream sendFileHandle error handling
* cgi: immediately return on file not found error
Diffstat (limited to 'src/loader/loader.nim')
-rw-r--r--src/loader/loader.nim23
1 files changed, 11 insertions, 12 deletions
diff --git a/src/loader/loader.nim b/src/loader/loader.nim
index 2fda709c..02585630 100644
--- a/src/loader/loader.nim
+++ b/src/loader/loader.nim
@@ -152,13 +152,13 @@ proc loadResource(ctx: LoaderContext, request: Request, handle: LoaderHandle) =
         inc tries
         redo = true
       of URI_RESULT_WRONG_URL:
-        discard handle.sendResult(ERROR_INVALID_URI_METHOD_ENTRY)
+        handle.sendResult(ERROR_INVALID_URI_METHOD_ENTRY)
         handle.close()
       of URI_RESULT_NOT_FOUND:
-        discard handle.sendResult(ERROR_UNKNOWN_SCHEME)
+        handle.sendResult(ERROR_UNKNOWN_SCHEME)
         handle.close()
   if tries >= MaxRewrites:
-    discard handle.sendResult(ERROR_TOO_MANY_REWRITES)
+    handle.sendResult(ERROR_TOO_MANY_REWRITES)
     handle.close()
 
 proc onLoad(ctx: LoaderContext, stream: SocketStream) =
@@ -166,7 +166,7 @@ proc onLoad(ctx: LoaderContext, stream: SocketStream) =
   stream.sread(request)
   let handle = newLoaderHandle(stream, request.canredir)
   if not ctx.config.filter.match(request.url):
-    discard handle.sendResult(ERROR_DISALLOWED_URL)
+    handle.sendResult(ERROR_DISALLOWED_URL)
     handle.close()
   else:
     for k, v in ctx.config.defaultheaders.table:
@@ -231,10 +231,8 @@ proc acceptConnection(ctx: LoaderContext) =
     of SET_REFERRER_POLICY:
       stream.sread(ctx.referrerpolicy)
       stream.close()
-  except IOError:
-    # End-of-file, broken pipe, or something else. For now we just
-    # ignore it and pray nothing breaks.
-    # (TODO: this is probably not a very good idea.)
+  except ErrorBrokenPipe:
+    # receiving end died while reading the file; give up.
     stream.close()
 
 proc exitLoader(ctx: LoaderContext) =
@@ -286,10 +284,11 @@ proc runFileLoader*(fd: cint, config: LoaderConfig) =
           while not handle.istream.atEnd:
             try:
               let n = handle.istream.readData(addr buffer[0], buffer.len)
-              if not handle.sendData(addr buffer[0], n):
-                unreg.add(event.fd)
-                break
-            except ErrorAgain, ErrorWouldBlock:
+              handle.sendData(addr buffer[0], n)
+            except ErrorAgain, ErrorWouldBlock: # retry later
+              break
+            except ErrorBrokenPipe: # receiver died; stop streaming
+              unreg.add(event.fd)
               break
       if Error in event.events:
         assert event.fd != ctx.fd