Skip to content

Commit

Permalink
feat: access http response headers
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko committed Oct 15, 2024
1 parent dba2fc3 commit d2a2eb7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/Extism/PDK/Bindings.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ foreign import ccall "extism_http_request" extismHTTPRequest :: MemoryOffset ->

foreign import ccall "extism_http_status_code" extismHTTPStatusCode :: IO Int32

foreign import ccall "extism_http_headers" extismHTTPHeaders :: IO MemoryOffset

foreign import ccall "__wasm_call_ctors" wasmConstructor :: IO ()

foreign import ccall "__wasm_call_dtors" wasmDestructor :: IO ()
Expand Down
32 changes: 24 additions & 8 deletions src/Extism/PDK/HTTP.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ data Request = Request
-- | HTTP Response
data Response = Response
{ statusCode :: Int,
responseData :: ByteString
responseData :: ByteString,
responseHeaders :: [(String, String)]
}

-- | Creates a new 'Request'
Expand All @@ -46,11 +47,11 @@ withHeaders h req =

-- | Get the 'Response' body as a 'ByteString'
responseByteString :: Response -> ByteString
responseByteString (Response _ mem) = mem
responseByteString (Response _ mem _) = mem

-- | Get the 'Response' body as a 'String'
responseString :: Response -> String
responseString (Response _ mem) = fromByteString mem
responseString (Response _ mem _) = fromByteString mem

-- | Get the 'Response' body as JSON
responseJSON :: (Text.JSON.Generic.Data a) => Response -> IO (Either String a)
Expand All @@ -67,7 +68,20 @@ responseJSON res = do

-- | Get the 'Response' body and decode it
response :: (FromBytes a) => Response -> Either String a
response (Response _ mem) = fromBytes mem
response (Response _ mem _) = fromBytes mem

getHeaders = do
offs <- extismHTTPHeaders
if offs == 0
then
return []
else do
mem <- Extism.PDK.Memory.findMemory offs
h <- Extism.PDK.Memory.load mem
() <- Extism.PDK.Memory.free mem
case h of
Left _ -> return []
Right x -> return x

-- | Send HTTP request with an optional request body
sendRequestWithBody :: (ToBytes a) => Request -> a -> IO Response
Expand All @@ -83,13 +97,14 @@ sendRequestWithBody req b = do
j <- allocString json
res <- extismHTTPRequest (memoryOffset j) (memoryOffset body)
code <- extismHTTPStatusCode
h <- getHeaders
if res == 0
then return (Response (fromIntegral code) empty)
then return (Response (fromIntegral code) empty h)
else do
mem <- findMemory res
bs <- loadByteString mem
free mem
return (Response (fromIntegral code) bs)
return (Response (fromIntegral code) bs h)

-- | Send HTTP request with an optional request body
sendRequest :: (ToBytes a) => Request -> Maybe a -> IO Response
Expand All @@ -109,11 +124,12 @@ sendRequest req b =
j <- allocString json
res <- extismHTTPRequest (memoryOffset j) (memoryOffset body)
code <- extismHTTPStatusCode
h <- getHeaders
if res == 0
then return (Response (fromIntegral code) empty)
then return (Response (fromIntegral code) empty h)
else do
len <- extismLengthUnsafe res
let mem = Memory res len
bs <- loadByteString mem
free mem
return (Response (fromIntegral code) bs)
return (Response (fromIntegral code) bs h)
3 changes: 3 additions & 0 deletions src/extism-pdk.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ ExtismPointer extism_http_request(ExtismPointer req, ExtismPointer body) {
DEFINE(http_status_code, int32_t)
int32_t extism_http_status_code() { return _http_status_code(); }

DEFINE(http_headers, int32_t)
int32_t extism_http_headers() { return _http_headers(); }

DEFINE(log_info, void, ExtismPointer)
void extism_log_info(ExtismPointer p) { return _log_info(p); }
DEFINE(log_debug, void, ExtismPointer)
Expand Down

0 comments on commit d2a2eb7

Please sign in to comment.