From 8063d77894ffc3489e69b878d48cae7b2e23bfd8 Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Wed, 17 May 2023 16:13:10 -0500 Subject: [PATCH] add head request --- src/puppy.nim | 7 +++++++ src/puppy/platforms/linux/platform.nim | 4 +++- tests/test.nim | 5 +++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/puppy.nim b/src/puppy.nim index c309f45..3d26022 100644 --- a/src/puppy.nim +++ b/src/puppy.nim @@ -93,6 +93,13 @@ proc delete*( ): Response = fetch(newRequest(url, "DELETE", headers, timeout)) +proc head*( + url: string, + headers = emptyHttpHeaders(), + timeout: float32 = 60 +): Response = + fetch(newRequest(url, "HEAD", headers, timeout)) + proc fetch*(url: string, headers = emptyHttpHeaders()): string = ## Simple fetch that directly returns the GET response body. ## Raises an exception if anything goes wrong or if the response code diff --git a/src/puppy/platforms/linux/platform.nim b/src/puppy/platforms/linux/platform.nim index 11d8926..793998a 100644 --- a/src/puppy/platforms/linux/platform.nim +++ b/src/puppy/platforms/linux/platform.nim @@ -65,7 +65,9 @@ proc internalFetch*(req: Request): Response {.raises: [PuppyError].} = discard curl.easy_setopt(OPT_HTTPHEADER, headerList) - if req.verb.toUpperAscii() == "POST" or req.body.len > 0: + if req.verb.toUpperAscii() == "HEAD": + discard curl.easy_setopt(OPT_NOBODY, 1) + elif 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) diff --git a/tests/test.nim b/tests/test.nim index 4f18b44..1a13225 100644 --- a/tests/test.nim +++ b/tests/test.nim @@ -57,6 +57,11 @@ block: ) ) +block: + let response = head("https://www.google.com") + doAssert response.code == 200 + doAssert response.body.len == 0 + when defined(windows): block: let httpsServer = startProcess(