Skip to content

Commit

Permalink
Merge pull request #26 from guzba/master
Browse files Browse the repository at this point in the history
1.0.6 mac fix
  • Loading branch information
treeform authored Sep 24, 2021
2 parents 11177d2 + 55390f3 commit b0fc6ab
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 63 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.0.5"
version = "1.0.6"
author = "Andre von Houck"
description = "Puppy fetches HTML pages for Nim."
license = "MIT"
Expand Down
124 changes: 62 additions & 62 deletions src/puppy.nim
Original file line number Diff line number Diff line change
Expand Up @@ -153,70 +153,70 @@ else:
strings.add header.key & ": " & header.value

let curl = easy_init()
try:
discard curl.easy_setopt(OPT_URL, strings[0].cstring)
discard curl.easy_setopt(OPT_CUSTOMREQUEST, strings[1].cstring)

if req.timeout == 0:
req.timeout = 60
discard curl.easy_setopt(OPT_TIMEOUT, req.timeout.int)

# Create the Pslist for passing headers to curl manually. This is to
# avoid needing to call slist_free_all which creates problems
var slists: seq[Slist]
for i, header in req.headers:
slists.add Slist(data: strings[2 + i].cstring, next: nil)
# Do this in two passes so the slists index addresses are stable
var headerList: Pslist
for i, header in req.headers:
if i == 0:
headerList = slists[0].addr
else:
var tail = headerList
while tail.next != nil:
tail = tail.next
tail.next = slists[i].addr

discard curl.easy_setopt(OPT_HTTPHEADER, headerList)

if req.body.len > 0:
discard curl.easy_setopt(OPT_POSTFIELDS, req.body.cstring)

# Setup writers.
var headerWrap, bodyWrap: StringWrap
discard curl.easy_setopt(OPT_WRITEDATA, bodyWrap.addr)
discard curl.easy_setopt(OPT_WRITEFUNCTION, curlWriteFn)
discard curl.easy_setopt(OPT_HEADERDATA, headerWrap.addr)
discard curl.easy_setopt(OPT_HEADERFUNCTION, curlWriteFn)

# On windows look for cacert.pem.
when defined(windows):
discard curl.easy_setopt(OPT_CAINFO, "cacert.pem".cstring)
# Follow redirects by default.
discard curl.easy_setopt(OPT_FOLLOWLOCATION, 1)

let
ret = curl.easy_perform()
headerData = headerWrap.str

result.url = req.url

if ret == E_OK:
var httpCode: uint32
discard curl.easy_getinfo(INFO_RESPONSE_CODE, httpCode.addr)
result.code = httpCode.int
for headerLine in headerData.split(CRLF):
let arr = headerLine.split(":", 1)
if arr.len == 2:
result.headers[arr[0].strip()] = arr[1].strip()
result.body = bodyWrap.str
if result.headers["Content-Encoding"] == "gzip":
result.body = uncompress(result.body, dfGzip)
discard curl.easy_setopt(OPT_URL, strings[0].cstring)
discard curl.easy_setopt(OPT_CUSTOMREQUEST, strings[1].cstring)

if req.timeout == 0:
req.timeout = 60
discard curl.easy_setopt(OPT_TIMEOUT, req.timeout.int)

# Create the Pslist for passing headers to curl manually. This is to
# avoid needing to call slist_free_all which creates problems
var slists: seq[Slist]
for i, header in req.headers:
slists.add Slist(data: strings[2 + i].cstring, next: nil)
# Do this in two passes so the slists index addresses are stable
var headerList: Pslist
for i, header in req.headers:
if i == 0:
headerList = slists[0].addr
else:
result.error = $easy_strerror(ret)
finally:
curl.easy_cleanup()
strings.setLen(0) # Make sure strings sticks around until now
var tail = headerList
while tail.next != nil:
tail = tail.next
tail.next = slists[i].addr

discard curl.easy_setopt(OPT_HTTPHEADER, headerList)

if req.body.len > 0:
discard curl.easy_setopt(OPT_POSTFIELDS, req.body.cstring)

# Setup writers.
var headerWrap, bodyWrap: StringWrap
discard curl.easy_setopt(OPT_WRITEDATA, bodyWrap.addr)
discard curl.easy_setopt(OPT_WRITEFUNCTION, curlWriteFn)
discard curl.easy_setopt(OPT_HEADERDATA, headerWrap.addr)
discard curl.easy_setopt(OPT_HEADERFUNCTION, curlWriteFn)

# On windows look for cacert.pem.
when defined(windows):
discard curl.easy_setopt(OPT_CAINFO, "cacert.pem".cstring)
# Follow redirects by default.
discard curl.easy_setopt(OPT_FOLLOWLOCATION, 1)

let
ret = curl.easy_perform()
headerData = headerWrap.str

curl.easy_cleanup()
strings.setLen(0) # Make sure strings sticks around until now

result.url = req.url

if ret == E_OK:
var httpCode: uint32
discard curl.easy_getinfo(INFO_RESPONSE_CODE, httpCode.addr)
result.code = httpCode.int
for headerLine in headerData.split(CRLF):
let arr = headerLine.split(":", 1)
if arr.len == 2:
result.headers[arr[0].strip()] = arr[1].strip()
result.body = bodyWrap.str
if result.headers["Content-Encoding"] == "gzip":
result.body = uncompress(result.body, dfGzip)
else:
result.error = $easy_strerror(ret)

proc newRequest*(
url: string,
Expand Down

0 comments on commit b0fc6ab

Please sign in to comment.