about summary refs log tree commit diff stats
path: root/src/io
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-05-09 12:34:58 +0200
committerbptato <nincsnevem662@gmail.com>2023-05-09 12:34:58 +0200
commit87f9bd656b2a8a8d4ebd029ba6a78f1dc93558eb (patch)
treec3b7e4f3500512baef2064986cfdf5701c988e29 /src/io
parente0a8b27068ac8fc61a243bae685c23a14976f619 (diff)
downloadchawan-87f9bd656b2a8a8d4ebd029ba6a78f1dc93558eb.tar.gz
Improve debugging, reduce crashes
Loader no longer dies when not everything is read from the stream.
Diffstat (limited to 'src/io')
-rw-r--r--src/io/http.nim6
-rw-r--r--src/io/loader.nim14
2 files changed, 15 insertions, 5 deletions
diff --git a/src/io/http.nim b/src/io/http.nim
index bd880445..60387faf 100644
--- a/src/io/http.nim
+++ b/src/io/http.nim
@@ -72,8 +72,10 @@ proc curlWriteHeader(p: cstring, size: csize_t, nitems: csize_t, userdata: point
 proc curlWriteBody(p: cstring, size: csize_t, nmemb: csize_t, userdata: pointer): csize_t {.cdecl.} =
   let handleData = cast[HandleData](userdata)
   if nmemb > 0:
-    handleData.ostream.writeData(p, int(nmemb))
-    handleData.ostream.flush()
+    try:
+      handleData.ostream.writeData(p, int(nmemb))
+    except IOError: # Broken pipe
+      return 0
   return nmemb
 
 proc applyPostBody(curl: CURL, request: Request, handleData: HandleData) =
diff --git a/src/io/loader.nim b/src/io/loader.nim
index 2cb2b295..15b8e3e2 100644
--- a/src/io/loader.nim
+++ b/src/io/loader.nim
@@ -94,6 +94,7 @@ proc onLoad(ctx: LoaderContext, stream: Stream) =
   if not ctx.config.filter.match(request.url):
     stream.swrite(-1) # error
     stream.flush()
+    stream.close()
   else:
     for k, v in ctx.config.defaultHeaders.table:
       if k notin request.headers.table:
@@ -131,8 +132,11 @@ proc acceptConnection(ctx: LoaderContext) =
 
 proc finishCurlTransfer(ctx: LoaderContext, handleData: HandleData, res: int) =
   if res != int(CURLE_OK):
-    handleData.ostream.swrite(int(res))
-    handleData.ostream.flush()
+    try:
+      handleData.ostream.swrite(int(res))
+      handleData.ostream.flush()
+    except IOError: # Broken pipe
+      discard
   discard curl_multi_remove_handle(ctx.curlm, handleData.curl)
   handleData.ostream.close()
   handleData.cleanup()
@@ -234,7 +238,11 @@ proc fetch*(loader: FileLoader, input: Request): Promise[Response] =
   let fd = int(stream.source.getFd())
   loader.registerFun(fd)
   let promise = Promise[Response]()
-  loader.connecting[fd] = ConnectData(promise: promise, request: input)
+  loader.connecting[fd] = ConnectData(
+    promise: promise,
+    request: input,
+    stream: stream
+  )
 
 proc newResponse(res: int, request: Request, stream: Stream = nil): Response =
   return Response(