treeform / puppy

Puppy fetches via HTTP and HTTPS
MIT License
184 stars 27 forks source link

"index out of bounds, the container is empty" on mac OS when POST endpoint returns empty body #56

Closed oakes closed 2 years ago

oakes commented 2 years ago

On mac OS, puppy 1.5.0, when a POST endpoint returns an empty body, puppy throws an exception. This was not caught in the unit test because the /post test returns data. If i modify it slightly to return an empty string...

diff --git a/tests/test.nim b/tests/test.nim
index 0794690..9c3a64b 100644
--- a/tests/test.nim
+++ b/tests/test.nim
@@ -111,12 +111,12 @@ try:
       let req = Request(
         url: parseUrl("http://localhost:8080/post"),
         verb: "post",
-        body: "some data"
+        body: ""
       )
       let res = fetch(req)
       doAssert ($req).startsWith("POST http://localhost:8080/post")
       doAssert res.code == 200
-      doAssert res.body == "some data"
+      doAssert res.body == ""

     block:
       # test post + gzip

I get the following:

/Users/sekao/Documents/puppy/tests/test.nim(116) test
/Users/sekao/Documents/puppy/src/puppy.nim(348) fetch
/Users/sekao/.choosenim/toolchains/nim-1.6.2/lib/system/fatal.nim(53) sysFatal
Error: unhandled exception: index out of bounds, the container is empty [IndexDefect]

The error is happening on this line so it looks like it needs to check the string length before trying to copy. This code only runs on mac OS so it wouldn't affect other OSes, but there may be a similar problem elsewhere, i haven't checked.

% nim -v
Nim Compiler Version 1.6.2 [MacOSX: amd64]
Compiled at 2021-12-17
Copyright (c) 2006-2021 by Andreas Rumpf

active boot switches: -d:release
treeform commented 2 years ago

Fixed in1.5.1. Feel free to re open if its still an issue.