Skip to content

Commit

Permalink
Merge pull request #57 from guzba/master
Browse files Browse the repository at this point in the history
1.5.1
  • Loading branch information
treeform authored Jan 16, 2022
2 parents 66bf86b + fc34d97 commit 0182117
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion puppy.nimble
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "1.5.0"
version = "1.5.1"
author = "Andre von Houck"
description = "Puppy fetches HTML pages for Nim."
license = "MIT"
Expand Down
14 changes: 5 additions & 9 deletions src/puppy.nim
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,7 @@ proc fetch*(req: Request): Response =
discard WinHttpCloseHandle(hSession)

elif defined(macosx) and not defined(puppyLibcurl):
let pool = NSAutoreleasePool.allocInit()

try:
autoreleasepool:
let request = NSMutableURLRequest.requestWithURL(
NSURL.URLWithString(@($req.url)),
NSURLRequestReloadIgnoringLocalCacheData,
Expand Down Expand Up @@ -344,15 +342,13 @@ proc fetch*(req: Request): Response =
let value = dictionary.objectForKey(key)
result.headers[$(key.NSString)] = $(value.NSString)

result.body.setLen(data.length)
copyMem(result.body[0].addr, data.bytes, result.body.len)
if data.length > 0:
result.body.setLen(data.length)
copyMem(result.body[0].addr, data.bytes, result.body.len)

if error.int != 0 and error.code != NSURLErrorUserCancelledAuthentication:
raise newException(PuppyError, $error)

finally:
pool.release()

else:
type
StringWrap = object
Expand Down Expand Up @@ -405,7 +401,7 @@ proc fetch*(req: Request): Response =

discard curl.easy_setopt(OPT_HTTPHEADER, headerList)

if req.body.len > 0:
if req.verb.toUpperAscii() == "POST" or req.body.len > 0:
discard curl.easy_setopt(OPT_POSTFIELDSIZE, req.body.len)
discard curl.easy_setopt(OPT_POSTFIELDS, req.body.cstring)

Expand Down
9 changes: 8 additions & 1 deletion src/puppy/macdefs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const
NSURLRequestReloadRevalidatingCacheData* = 5.NSURLRequestCachePolicy
NSURLErrorUserCancelledAuthentication* = -1012

proc allocInit*(_: typedesc[NSAutoreleasePool]): NSAutoreleasePool =
proc new*(_: typedesc[NSAutoreleasePool]): NSAutoreleasePool =
objc_msgSend(
objc_getClass("NSAutoreleasePool".cstring).ID,
sel_registerName("new".cstring)
Expand All @@ -61,6 +61,13 @@ proc release*(pool: NSAutoreleasePool) =
sel_registerName("release".cstring)
)

template autoreleasepool*(body: untyped) =
let pool = NSAutoreleasePool.new()
try:
body
finally:
pool.release()

proc `@`*(s: string): NSString =
objc_msgSend(
objc_getClass("NSString".cstring).ID,
Expand Down
12 changes: 12 additions & 0 deletions tests/test.nim
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ try:
doAssert res.code == 200
doAssert res.body == "some data"

block:
# test empty post
let req = Request(
url: parseUrl("http://localhost:8080/post"),
verb: "post",
body: ""
)
let res = fetch(req)
doAssert ($req).startsWith("POST http://localhost:8080/post")
doAssert res.code == 200
doAssert res.body == ""

block:
# test post + gzip
let res = fetch(Request(
Expand Down

0 comments on commit 0182117

Please sign in to comment.