daurnimator / lua-http

HTTP Library for Lua. Supports HTTP(S) 1.0, 1.1 and 2.0; client and server.
https://daurnimator.github.io/lua-http/
MIT License
807 stars 82 forks source link

asynchronous http_request #170

Closed cleoold closed 4 years ago

cleoold commented 4 years ago

Hello thanks for your library! I just have a question about making http requests, after seeing the following code snippet:

local http_request = require "http.request"
local headers, stream = assert(http_request.new_from_uri("http://example.com"):go())
local body = assert(stream:get_body_as_string())
if headers:get ":status" ~= "200" then
    error(body)
end
-- final effect
print(body)

If this code runs standalone, it is blocking right? How can you achieve the same effect as running this code snippet asynchronously?

PS: if I paste it inside the onstream callback in http_server, will it not block future incoming requests?

daurnimator commented 4 years ago

How can you achieve the same effect as running this code snippet asynchronously?

run it from inside of a cqueues managed coroutine

PS: if I paste it inside the onstream callback in http_server, will it not block future incoming requests?

Yes. http_server sets up a cqueues coroutine for you.