[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: [ANN] lua-handlers - async. tcp/udp sockets and HTTP requests.
- From: "Robert G. Jakabosky" <bobby@...>
- Date: Tue, 28 Dec 2010 08:14:47 -0700
Hi Everyone,
I would like to announce my latest project lua-handlers [1], an asynchronous
socket & HTTP networking library that uses the lua-ev event loop.
lua-handlers provides an HTTP client object that make it easy to send many
parallel HTTP requests. The library provides a form & file class to simplify
sending form posts and uploading files. I have attached two examples of
using the HTTP interface.
There are async. wrapper classes for LuaSocket's TCP & UDP sockets.
Also for more advanced networking there are wrapper classes for ZeroMQ's [2]
sockets. The project includes a lot of examples so take a look at the
test_*.lua files in the project.
There are three LuaRocks spec files in the project, since I wanted to keep the
HTTP & ZeroMQ parts seperate. See the project's README [3] file for
instructions on how to install it.
Please let me know what you think.
1. https://github.com/Neopallium/lua-handlers
2. http://www.zeromq.org/
3. https://github.com/Neopallium/lua-handlers/blob/master/README.md
--
Robert G. Jakabosky
-- Copyright (c) 2010 by Robert G. Jakabosky <bobby@neoawareness.com>
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.
local httpclient = require'handler.http.client'
local ev = require'ev'
local loop = ev.Loop.default
local client = httpclient.new(loop,{
user_agent = "HTTPClient tester",
})
local function on_response(req, resp)
print('---- status code =' .. resp.status_code .. ', for url:', req.url)
end
local function on_data(req, resp, data)
print('---- got response data: ' .. #data .. ' bytes, for url:', req.url)
end
local count = 0
local function on_finished(req, resp, data)
count = count - 1
if count == 0 then
-- finished processing urls
loop:unloop()
end
end
local function request_url(url)
count = count + 1
print('---- start request of url: ' .. url)
-- start next request.
client:request{
url = url,
on_response = on_response,
on_data = on_data,
on_finished = on_finished,
}
end
-- start parallel request of all urls from command line.
for i,url in ipairs(arg) do
request_url(url)
end
loop:loop()
-- Copyright (c) 2010 by Robert G. Jakabosky <bobby@neoawareness.com>
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.
local httpclient = require'handler.http.client'
local form = require'handler.http.form'
local file = require'handler.http.file'
local ev = require'ev'
local loop = ev.Loop.default
local verbose = false
local client = httpclient.new(loop,{
user_agent = "HTTPClient tester"
})
local function on_response(req, resp)
print('---- status code =' .. resp.status_code .. ', for url:', req.url)
end
local function on_data(req, resp, data)
print('---- got response data: ' .. #data .. ' bytes, for url:', req.url)
print(data)
end
local function on_finished(req, resp)
loop:unloop()
print('====================== Finished POST request =================')
end
local filename = arg[1]
if not filename then
print('Usage: ' .. arg[0] .. ' <filename>')
os.exit(-1)
end
local upload_form = form.new{
-- string fields
first_name = "john",
last_name = "doe",
-- file field
upload_file = file.new(filename)
}
local req = client:request{
method = 'POST',
url = 'http://localhost/upload.php',
body = upload_form,
on_response = on_response,
on_data = on_data,
on_finished = on_finished,
}
loop:loop()