2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2001-08-07 21:50:04 +02:00
|
|
|
-- HTTP/1.1 client support for the Lua language.
|
2003-06-26 20:47:49 +02:00
|
|
|
-- LuaSocket toolkit.
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Author: Diego Nehab
|
2001-08-07 21:50:04 +02:00
|
|
|
-- Conforming to: RFC 2616, LTN7
|
|
|
|
-- RCS ID: $Id$
|
2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2004-01-16 08:06:31 +01:00
|
|
|
-- make sure LuaSocket is loaded
|
|
|
|
if not LUASOCKET_LIBNAME then error('module requires LuaSocket') end
|
|
|
|
-- get LuaSocket namespace
|
|
|
|
local socket = _G[LUASOCKET_LIBNAME]
|
|
|
|
if not socket then error('module requires LuaSocket') end
|
2004-03-16 07:42:53 +01:00
|
|
|
-- create namespace inside LuaSocket namespace
|
|
|
|
socket.http = socket.http or {}
|
|
|
|
-- make all module globals fall into namespace
|
|
|
|
setmetatable(socket.http, { __index = _G })
|
|
|
|
setfenv(1, socket.http)
|
2001-07-29 05:51:36 +02:00
|
|
|
|
2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Program constants
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- connection timeout in seconds
|
2004-01-16 08:06:31 +01:00
|
|
|
TIMEOUT = 60
|
2000-12-29 23:15:09 +01:00
|
|
|
-- default port for document retrieval
|
2004-01-16 08:06:31 +01:00
|
|
|
PORT = 80
|
2000-12-29 23:15:09 +01:00
|
|
|
-- user agent field sent in request
|
2004-01-19 19:22:51 +01:00
|
|
|
USERAGENT = socket.version
|
2001-06-06 22:55:45 +02:00
|
|
|
-- block size used in transfers
|
2004-03-16 07:42:53 +01:00
|
|
|
BLOCKSIZE = 2048
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Function return value selectors
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
local function second(a, b)
|
|
|
|
return b
|
|
|
|
end
|
|
|
|
|
|
|
|
local function third(a, b, c)
|
|
|
|
return c
|
|
|
|
end
|
2001-07-29 05:51:36 +02:00
|
|
|
|
2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2001-01-25 23:01:37 +01:00
|
|
|
-- Tries to get a pattern from the server and closes socket on error
|
2000-12-29 23:15:09 +01:00
|
|
|
-- sock: socket connected to the server
|
2003-05-25 03:54:13 +02:00
|
|
|
-- pattern: pattern to receive
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Returns
|
2003-05-25 03:54:13 +02:00
|
|
|
-- received pattern on success
|
|
|
|
-- nil followed by error message on error
|
2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2004-01-16 08:06:31 +01:00
|
|
|
local function try_receiving(sock, pattern)
|
2003-05-25 03:54:13 +02:00
|
|
|
local data, err = sock:receive(pattern)
|
|
|
|
if not data then sock:close() end
|
2004-01-19 01:24:41 +01:00
|
|
|
--print(data)
|
2003-05-25 03:54:13 +02:00
|
|
|
return data, err
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2001-01-25 23:01:37 +01:00
|
|
|
-- Tries to send data to the server and closes socket on error
|
2000-12-29 23:15:09 +01:00
|
|
|
-- sock: socket connected to the server
|
2004-03-16 07:42:53 +01:00
|
|
|
-- ...: data to send
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Returns
|
2001-01-25 23:01:37 +01:00
|
|
|
-- err: error message if any, nil if successfull
|
2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2004-01-16 08:06:31 +01:00
|
|
|
local function try_sending(sock, ...)
|
|
|
|
local sent, err = sock:send(unpack(arg))
|
2003-05-25 03:54:13 +02:00
|
|
|
if not sent then sock:close() end
|
2004-01-19 01:24:41 +01:00
|
|
|
--io.write(unpack(arg))
|
2001-01-25 23:01:37 +01:00
|
|
|
return err
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2001-08-07 21:50:04 +02:00
|
|
|
-- Receive server reply messages, parsing for status code
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Input
|
2001-01-25 23:01:37 +01:00
|
|
|
-- sock: socket connected to the server
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Returns
|
2001-01-25 23:01:37 +01:00
|
|
|
-- code: server status code or nil if error
|
2001-08-07 21:50:04 +02:00
|
|
|
-- line: full HTTP status line
|
2000-12-29 23:15:09 +01:00
|
|
|
-- err: error message if any
|
|
|
|
-----------------------------------------------------------------------------
|
2004-01-16 08:06:31 +01:00
|
|
|
local function receive_status(sock)
|
2004-03-16 07:42:53 +01:00
|
|
|
local line, err = try_receiving(sock)
|
2004-01-16 08:06:31 +01:00
|
|
|
if not err then
|
2004-03-16 07:42:53 +01:00
|
|
|
local code = third(string.find(line, "HTTP/%d*%.%d* (%d%d%d)"))
|
2004-01-16 08:06:31 +01:00
|
|
|
return tonumber(code), line
|
2000-12-29 23:15:09 +01:00
|
|
|
else return nil, nil, err end
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2001-07-29 05:51:36 +02:00
|
|
|
-- Receive and parse response header fields
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Input
|
2001-01-25 23:01:37 +01:00
|
|
|
-- sock: socket connected to the server
|
2001-07-29 05:51:36 +02:00
|
|
|
-- headers: a table that might already contain headers
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Returns
|
2001-07-29 05:51:36 +02:00
|
|
|
-- headers: a table with all headers fields in the form
|
2000-12-29 23:15:09 +01:00
|
|
|
-- {name_1 = "value_1", name_2 = "value_2" ... name_n = "value_n"}
|
|
|
|
-- all name_i are lowercase
|
|
|
|
-- nil and error message in case of error
|
|
|
|
-----------------------------------------------------------------------------
|
2004-01-16 08:06:31 +01:00
|
|
|
local function receive_headers(sock, headers)
|
2000-12-29 23:15:09 +01:00
|
|
|
local line, err
|
2001-07-29 05:51:36 +02:00
|
|
|
local name, value, _
|
2004-01-16 08:06:31 +01:00
|
|
|
headers = headers or {}
|
2001-01-25 23:01:37 +01:00
|
|
|
-- get first line
|
2004-01-16 08:06:31 +01:00
|
|
|
line, err = try_receiving(sock)
|
2001-01-25 23:01:37 +01:00
|
|
|
if err then return nil, err end
|
2000-12-29 23:15:09 +01:00
|
|
|
-- headers go until a blank line is found
|
|
|
|
while line ~= "" do
|
|
|
|
-- get field-name and value
|
2002-12-03 00:34:41 +01:00
|
|
|
_,_, name, value = string.find(line, "^(.-):%s*(.*)")
|
2001-01-25 23:01:37 +01:00
|
|
|
if not name or not value then
|
|
|
|
sock:close()
|
|
|
|
return nil, "malformed reponse headers"
|
|
|
|
end
|
2002-12-03 00:34:41 +01:00
|
|
|
name = string.lower(name)
|
2000-12-29 23:15:09 +01:00
|
|
|
-- get next line (value might be folded)
|
2004-01-16 08:06:31 +01:00
|
|
|
line, err = try_receiving(sock)
|
2001-01-25 23:01:37 +01:00
|
|
|
if err then return nil, err end
|
2000-12-29 23:15:09 +01:00
|
|
|
-- unfold any folded values
|
2002-12-03 00:34:41 +01:00
|
|
|
while not err and string.find(line, "^%s") do
|
2000-12-29 23:15:09 +01:00
|
|
|
value = value .. line
|
2004-01-16 08:06:31 +01:00
|
|
|
line, err = try_receiving(sock)
|
2001-01-25 23:01:37 +01:00
|
|
|
if err then return nil, err end
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
-- save pair in table
|
2001-07-29 05:51:36 +02:00
|
|
|
if headers[name] then headers[name] = headers[name] .. ", " .. value
|
|
|
|
else headers[name] = value end
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
2001-07-29 05:51:36 +02:00
|
|
|
return headers
|
2001-01-25 23:01:37 +01:00
|
|
|
end
|
|
|
|
|
2004-01-16 08:06:31 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2004-03-16 07:42:53 +01:00
|
|
|
-- Aborts a sink with an error message
|
2004-01-16 08:06:31 +01:00
|
|
|
-- Input
|
|
|
|
-- cb: callback function
|
|
|
|
-- err: error message to pass to callback
|
|
|
|
-- Returns
|
|
|
|
-- callback return or if nil err
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
local function abort(cb, err)
|
2004-03-16 07:42:53 +01:00
|
|
|
local go, cb_err = cb(nil, err)
|
|
|
|
return cb_err or err
|
2004-01-16 08:06:31 +01:00
|
|
|
end
|
|
|
|
|
2001-01-25 23:01:37 +01:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Receives a chunked message body
|
|
|
|
-- Input
|
|
|
|
-- sock: socket connected to the server
|
2001-07-29 05:51:36 +02:00
|
|
|
-- headers: header set in which to include trailer headers
|
2004-03-16 07:42:53 +01:00
|
|
|
-- sink: response message body sink
|
2001-01-25 23:01:37 +01:00
|
|
|
-- Returns
|
2001-05-21 20:12:20 +02:00
|
|
|
-- nil if successfull or an error message in case of error
|
2001-01-25 23:01:37 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2004-03-16 07:42:53 +01:00
|
|
|
local function receive_body_bychunks(sock, headers, sink)
|
|
|
|
local chunk, size, line, err, go
|
2001-07-29 05:51:36 +02:00
|
|
|
while 1 do
|
2001-01-25 23:01:37 +01:00
|
|
|
-- get chunk size, skip extention
|
2004-01-16 08:06:31 +01:00
|
|
|
line, err = try_receiving(sock)
|
2004-03-16 07:42:53 +01:00
|
|
|
if err then return abort(sink, err) end
|
2002-12-03 00:34:41 +01:00
|
|
|
size = tonumber(string.gsub(line, ";.*", ""), 16)
|
2004-03-16 07:42:53 +01:00
|
|
|
if not size then return abort(sink, "invalid chunk size") end
|
2001-09-18 22:22:59 +02:00
|
|
|
-- was it the last chunk?
|
|
|
|
if size <= 0 then break end
|
2001-01-25 23:01:37 +01:00
|
|
|
-- get chunk
|
2004-01-16 08:06:31 +01:00
|
|
|
chunk, err = try_receiving(sock, size)
|
2004-03-16 07:42:53 +01:00
|
|
|
if err then return abort(sink, err) end
|
2001-06-06 22:55:45 +02:00
|
|
|
-- pass chunk to callback
|
2004-03-16 07:42:53 +01:00
|
|
|
go, err = sink(chunk)
|
2004-01-16 08:06:31 +01:00
|
|
|
-- see if callback aborted
|
2004-03-16 07:42:53 +01:00
|
|
|
if not go then return err or "aborted by callback" end
|
2001-07-29 05:51:36 +02:00
|
|
|
-- skip CRLF on end of chunk
|
2004-03-16 07:42:53 +01:00
|
|
|
err = second(try_receiving(sock))
|
|
|
|
if err then return abort(sink, err) end
|
2001-07-29 05:51:36 +02:00
|
|
|
end
|
2004-03-16 07:42:53 +01:00
|
|
|
-- servers shouldn't send trailer headers, but who trusts them?
|
|
|
|
err = second(receive_headers(sock, headers))
|
|
|
|
if err then return abort(sink, err) end
|
2001-06-06 22:55:45 +02:00
|
|
|
-- let callback know we are done
|
2004-03-16 07:42:53 +01:00
|
|
|
return second(sink(nil))
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2001-05-21 20:12:20 +02:00
|
|
|
-- Receives a message body by content-length
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Input
|
2001-01-25 23:01:37 +01:00
|
|
|
-- sock: socket connected to the server
|
2001-08-07 21:50:04 +02:00
|
|
|
-- length: message body length
|
2004-03-16 07:42:53 +01:00
|
|
|
-- sink: response message body sink
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Returns
|
2001-05-21 20:12:20 +02:00
|
|
|
-- nil if successfull or an error message in case of error
|
|
|
|
-----------------------------------------------------------------------------
|
2004-03-16 07:42:53 +01:00
|
|
|
local function receive_body_bylength(sock, length, sink)
|
2001-06-06 22:55:45 +02:00
|
|
|
while length > 0 do
|
2004-01-16 08:06:31 +01:00
|
|
|
local size = math.min(BLOCKSIZE, length)
|
2001-06-06 22:55:45 +02:00
|
|
|
local chunk, err = sock:receive(size)
|
2004-03-16 07:42:53 +01:00
|
|
|
local go, cb_err = sink(chunk)
|
2004-01-16 08:06:31 +01:00
|
|
|
length = length - string.len(chunk)
|
|
|
|
-- see if callback aborted
|
2004-03-16 07:42:53 +01:00
|
|
|
if not go then return cb_err or "aborted by callback" end
|
2004-01-16 08:06:31 +01:00
|
|
|
-- see if there was an error
|
2004-03-16 07:42:53 +01:00
|
|
|
if err and length > 0 then return abort(sink, err) end
|
2001-06-06 22:55:45 +02:00
|
|
|
end
|
2004-03-16 07:42:53 +01:00
|
|
|
return second(sink(nil))
|
2001-05-21 20:12:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2004-03-16 07:42:53 +01:00
|
|
|
-- Receives a message body until the conection is closed
|
2001-05-21 20:12:20 +02:00
|
|
|
-- Input
|
|
|
|
-- sock: socket connected to the server
|
2004-03-16 07:42:53 +01:00
|
|
|
-- sink: response message body sink
|
2001-05-21 20:12:20 +02:00
|
|
|
-- Returns
|
|
|
|
-- nil if successfull or an error message in case of error
|
|
|
|
-----------------------------------------------------------------------------
|
2004-03-16 07:42:53 +01:00
|
|
|
local function receive_body_untilclosed(sock, sink)
|
2001-06-06 22:55:45 +02:00
|
|
|
while 1 do
|
2004-01-16 08:06:31 +01:00
|
|
|
local chunk, err = sock:receive(BLOCKSIZE)
|
2004-03-16 07:42:53 +01:00
|
|
|
local go, cb_err = sink(chunk)
|
2004-01-16 08:06:31 +01:00
|
|
|
-- see if callback aborted
|
2004-03-16 07:42:53 +01:00
|
|
|
if not go then return cb_err or "aborted by callback" end
|
2004-01-16 08:06:31 +01:00
|
|
|
-- see if we are done
|
2004-03-16 07:42:53 +01:00
|
|
|
if err == "closed" then return chunk and second(sink(nil)) end
|
2004-01-16 08:06:31 +01:00
|
|
|
-- see if there was an error
|
2004-03-16 07:42:53 +01:00
|
|
|
if err then return abort(sink, err) end
|
2001-06-06 22:55:45 +02:00
|
|
|
end
|
2001-05-21 20:12:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2004-03-16 07:42:53 +01:00
|
|
|
-- Receives the HTTP response body
|
2001-05-21 20:12:20 +02:00
|
|
|
-- Input
|
|
|
|
-- sock: socket connected to the server
|
2001-07-29 05:51:36 +02:00
|
|
|
-- headers: response header fields
|
2004-03-16 07:42:53 +01:00
|
|
|
-- sink: response message body sink
|
2001-05-21 20:12:20 +02:00
|
|
|
-- Returns
|
|
|
|
-- nil if successfull or an error message in case of error
|
|
|
|
-----------------------------------------------------------------------------
|
2004-03-16 07:42:53 +01:00
|
|
|
local function receive_body(sock, headers, sink)
|
|
|
|
-- make sure sink is not fancy
|
|
|
|
sink = ltn12.sink.simplify(sink)
|
2001-09-18 22:22:59 +02:00
|
|
|
local te = headers["transfer-encoding"]
|
2001-07-29 05:51:36 +02:00
|
|
|
if te and te ~= "identity" then
|
2001-05-21 20:12:20 +02:00
|
|
|
-- get by chunked transfer-coding of message body
|
2004-03-16 07:42:53 +01:00
|
|
|
return receive_body_bychunks(sock, headers, sink)
|
2001-07-29 05:51:36 +02:00
|
|
|
elseif tonumber(headers["content-length"]) then
|
2001-05-21 20:12:20 +02:00
|
|
|
-- get by content-length
|
2001-07-29 05:51:36 +02:00
|
|
|
local length = tonumber(headers["content-length"])
|
2004-03-16 07:42:53 +01:00
|
|
|
return receive_body_bylength(sock, length, sink)
|
2000-12-29 23:15:09 +01:00
|
|
|
else
|
2001-05-21 20:12:20 +02:00
|
|
|
-- get it all until connection closes
|
2004-03-16 07:42:53 +01:00
|
|
|
return receive_body_untilclosed(sock, sink)
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2004-03-16 07:42:53 +01:00
|
|
|
-- Sends the HTTP request message body in chunks
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Input
|
2004-01-16 08:06:31 +01:00
|
|
|
-- data: data connection
|
2004-03-16 07:42:53 +01:00
|
|
|
-- source: request message body source
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Returns
|
2004-01-16 08:06:31 +01:00
|
|
|
-- nil if successfull, or an error message in case of error
|
2001-07-29 05:51:36 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2004-03-16 07:42:53 +01:00
|
|
|
local function send_body_bychunks(data, source)
|
2004-01-16 08:06:31 +01:00
|
|
|
while 1 do
|
2004-03-16 07:42:53 +01:00
|
|
|
local chunk, cb_err = source()
|
2004-01-16 08:06:31 +01:00
|
|
|
-- check if callback aborted
|
2004-03-16 07:42:53 +01:00
|
|
|
if not chunk then return cb_err or "aborted by callback" end
|
2004-01-16 08:06:31 +01:00
|
|
|
-- if we are done, send last-chunk
|
|
|
|
if chunk == "" then return try_sending(data, "0\r\n\r\n") end
|
|
|
|
-- else send middle chunk
|
|
|
|
local err = try_sending(data,
|
|
|
|
string.format("%X\r\n", string.len(chunk)),
|
|
|
|
chunk,
|
|
|
|
"\r\n"
|
|
|
|
)
|
|
|
|
if err then return err end
|
|
|
|
end
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
2001-06-06 22:55:45 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2004-03-16 07:42:53 +01:00
|
|
|
-- Sends the HTTP request message body
|
2001-06-06 22:55:45 +02:00
|
|
|
-- Input
|
|
|
|
-- data: data connection
|
2004-03-16 07:42:53 +01:00
|
|
|
-- source: request message body source
|
2001-06-06 22:55:45 +02:00
|
|
|
-- Returns
|
|
|
|
-- nil if successfull, or an error message in case of error
|
|
|
|
-----------------------------------------------------------------------------
|
2004-03-16 07:42:53 +01:00
|
|
|
local function send_body(data, source)
|
2001-06-06 22:55:45 +02:00
|
|
|
while 1 do
|
2004-03-16 07:42:53 +01:00
|
|
|
local chunk, cb_err = source()
|
2004-01-16 08:06:31 +01:00
|
|
|
-- check if callback is done
|
2004-03-16 07:42:53 +01:00
|
|
|
if not chunk then return cb_err end
|
2004-01-16 08:06:31 +01:00
|
|
|
-- send data
|
|
|
|
local err = try_sending(data, chunk)
|
|
|
|
if err then return err end
|
2001-06-06 22:55:45 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2001-09-12 20:16:09 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2004-03-16 07:42:53 +01:00
|
|
|
-- Sends request headers
|
2001-09-12 20:16:09 +02:00
|
|
|
-- Input
|
|
|
|
-- sock: server socket
|
2004-03-16 07:42:53 +01:00
|
|
|
-- headers: table with headers to be sent
|
2001-09-12 20:16:09 +02:00
|
|
|
-- Returns
|
|
|
|
-- err: error message if any
|
|
|
|
-----------------------------------------------------------------------------
|
2004-01-16 08:06:31 +01:00
|
|
|
local function send_headers(sock, headers)
|
2001-09-12 20:16:09 +02:00
|
|
|
local err
|
|
|
|
headers = headers or {}
|
|
|
|
-- send request headers
|
|
|
|
for i, v in headers do
|
2004-01-16 08:06:31 +01:00
|
|
|
err = try_sending(sock, i .. ": " .. v .. "\r\n")
|
2001-09-12 20:16:09 +02:00
|
|
|
if err then return err end
|
|
|
|
end
|
|
|
|
-- mark end of request headers
|
2004-01-16 08:06:31 +01:00
|
|
|
return try_sending(sock, "\r\n")
|
2001-09-12 20:16:09 +02:00
|
|
|
end
|
|
|
|
|
2001-01-25 23:01:37 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2001-08-07 21:50:04 +02:00
|
|
|
-- Sends a HTTP request message through socket
|
2001-01-25 23:01:37 +01:00
|
|
|
-- Input
|
|
|
|
-- sock: socket connected to the server
|
|
|
|
-- method: request method to be used
|
2001-08-07 21:50:04 +02:00
|
|
|
-- uri: request uri
|
2001-07-29 05:51:36 +02:00
|
|
|
-- headers: request headers to be sent
|
2004-03-16 07:42:53 +01:00
|
|
|
-- source: request message body source
|
2001-01-25 23:01:37 +01:00
|
|
|
-- Returns
|
|
|
|
-- err: nil in case of success, error message otherwise
|
|
|
|
-----------------------------------------------------------------------------
|
2004-03-16 07:42:53 +01:00
|
|
|
local function send_request(sock, method, uri, headers, source)
|
2001-06-06 22:55:45 +02:00
|
|
|
local chunk, size, done, err
|
|
|
|
-- send request line
|
2004-01-16 08:06:31 +01:00
|
|
|
err = try_sending(sock, method .. " " .. uri .. " HTTP/1.1\r\n")
|
2001-01-25 23:01:37 +01:00
|
|
|
if err then return err end
|
2004-03-16 07:42:53 +01:00
|
|
|
if source and not headers["content-length"] then
|
2004-01-16 08:06:31 +01:00
|
|
|
headers["transfer-encoding"] = "chunked"
|
2003-08-31 02:58:07 +02:00
|
|
|
end
|
2001-06-06 22:55:45 +02:00
|
|
|
-- send request headers
|
2004-01-16 08:06:31 +01:00
|
|
|
err = send_headers(sock, headers)
|
2001-05-21 20:12:20 +02:00
|
|
|
if err then return err end
|
2001-06-06 22:55:45 +02:00
|
|
|
-- send request message body, if any
|
2004-03-16 07:42:53 +01:00
|
|
|
if source then
|
|
|
|
-- make sure source is not fancy
|
|
|
|
source = ltn12.source.simplify(source)
|
|
|
|
if headers["content-length"] then
|
|
|
|
return send_body(sock, source)
|
2004-01-16 08:06:31 +01:00
|
|
|
else
|
2004-03-16 07:42:53 +01:00
|
|
|
return send_body_bychunks(sock, source)
|
2004-01-16 08:06:31 +01:00
|
|
|
end
|
2001-06-06 22:55:45 +02:00
|
|
|
end
|
2001-01-25 23:01:37 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Determines if we should read a message body from the server response
|
|
|
|
-- Input
|
2004-01-16 08:06:31 +01:00
|
|
|
-- reqt: a table with the original request information
|
|
|
|
-- respt: a table with the server response information
|
2001-01-25 23:01:37 +01:00
|
|
|
-- Returns
|
|
|
|
-- 1 if a message body should be processed, nil otherwise
|
|
|
|
-----------------------------------------------------------------------------
|
2004-01-16 08:06:31 +01:00
|
|
|
local function should_receive_body(reqt, respt)
|
|
|
|
if reqt.method == "HEAD" then return nil end
|
|
|
|
if respt.code == 204 or respt.code == 304 then return nil end
|
|
|
|
if respt.code >= 100 and respt.code < 200 then return nil end
|
2001-01-25 23:01:37 +01:00
|
|
|
return 1
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2001-06-06 22:55:45 +02:00
|
|
|
-- Converts field names to lowercase and adds a few needed headers
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Input
|
2001-07-29 05:51:36 +02:00
|
|
|
-- headers: request header fields
|
2001-08-07 21:50:04 +02:00
|
|
|
-- parsed: parsed request URL
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Returns
|
2001-01-25 23:01:37 +01:00
|
|
|
-- lower: a table with the same headers, but with lowercase field names
|
|
|
|
-----------------------------------------------------------------------------
|
2004-01-16 08:06:31 +01:00
|
|
|
local function fill_headers(headers, parsed)
|
2001-01-25 23:01:37 +01:00
|
|
|
local lower = {}
|
2001-07-29 05:51:36 +02:00
|
|
|
headers = headers or {}
|
|
|
|
-- set default headers
|
2004-01-16 08:06:31 +01:00
|
|
|
lower["user-agent"] = USERAGENT
|
2001-07-29 05:51:36 +02:00
|
|
|
-- override with user values
|
|
|
|
for i,v in headers do
|
2002-12-03 00:34:41 +01:00
|
|
|
lower[string.lower(i)] = v
|
2001-01-25 23:01:37 +01:00
|
|
|
end
|
2001-09-26 22:40:13 +02:00
|
|
|
lower["host"] = parsed.host
|
2001-07-29 05:51:36 +02:00
|
|
|
-- this cannot be overriden
|
2001-01-25 23:01:37 +01:00
|
|
|
lower["connection"] = "close"
|
|
|
|
return lower
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2001-07-29 05:51:36 +02:00
|
|
|
-- Decides wether we should follow retry with authorization formation
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Input
|
2004-01-16 08:06:31 +01:00
|
|
|
-- reqt: a table with the original request information
|
2001-08-07 21:50:04 +02:00
|
|
|
-- parsed: parsed request URL
|
2004-01-16 08:06:31 +01:00
|
|
|
-- respt: a table with the server response information
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Returns
|
2001-07-29 05:51:36 +02:00
|
|
|
-- 1 if we should retry, nil otherwise
|
|
|
|
-----------------------------------------------------------------------------
|
2004-01-16 08:06:31 +01:00
|
|
|
local function should_authorize(reqt, parsed, respt)
|
2001-07-29 05:51:36 +02:00
|
|
|
-- if there has been an authorization attempt, it must have failed
|
2004-01-16 08:06:31 +01:00
|
|
|
if reqt.headers["authorization"] then return nil end
|
2001-07-29 05:51:36 +02:00
|
|
|
-- if we don't have authorization information, we can't retry
|
|
|
|
if parsed.user and parsed.password then return 1
|
|
|
|
else return nil end
|
2001-05-21 20:12:20 +02:00
|
|
|
end
|
|
|
|
|
2001-06-06 22:55:45 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2001-07-29 05:51:36 +02:00
|
|
|
-- Returns the result of retrying a request with authorization information
|
|
|
|
-- Input
|
2004-01-16 08:06:31 +01:00
|
|
|
-- reqt: a table with the original request information
|
2001-08-07 21:50:04 +02:00
|
|
|
-- parsed: parsed request URL
|
2001-07-29 05:51:36 +02:00
|
|
|
-- Returns
|
2004-01-16 08:06:31 +01:00
|
|
|
-- respt: result of target authorization
|
|
|
|
-----------------------------------------------------------------------------
|
2004-03-16 07:42:53 +01:00
|
|
|
local function authorize(reqt, parsed)
|
2004-01-16 08:06:31 +01:00
|
|
|
reqt.headers["authorization"] = "Basic " ..
|
2004-03-16 07:42:53 +01:00
|
|
|
(mime.b64(parsed.user .. ":" .. parsed.password))
|
2004-01-16 08:06:31 +01:00
|
|
|
local autht = {
|
|
|
|
nredirects = reqt.nredirects,
|
|
|
|
method = reqt.method,
|
|
|
|
url = reqt.url,
|
2004-03-16 07:42:53 +01:00
|
|
|
source = reqt.source,
|
|
|
|
sink = reqt.sink,
|
2004-01-19 01:24:41 +01:00
|
|
|
headers = reqt.headers,
|
|
|
|
timeout = reqt.timeout,
|
2004-01-19 19:22:51 +01:00
|
|
|
proxy = reqt.proxy,
|
2001-07-29 05:51:36 +02:00
|
|
|
}
|
2004-03-16 07:42:53 +01:00
|
|
|
return request_cb(autht)
|
2001-07-29 05:51:36 +02:00
|
|
|
end
|
2001-06-06 22:55:45 +02:00
|
|
|
|
2001-05-21 20:12:20 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2001-07-29 05:51:36 +02:00
|
|
|
-- Decides wether we should follow a server redirect message
|
2001-05-21 20:12:20 +02:00
|
|
|
-- Input
|
2004-01-16 08:06:31 +01:00
|
|
|
-- reqt: a table with the original request information
|
|
|
|
-- respt: a table with the server response information
|
2001-05-21 20:12:20 +02:00
|
|
|
-- Returns
|
2001-07-29 05:51:36 +02:00
|
|
|
-- 1 if we should redirect, nil otherwise
|
|
|
|
-----------------------------------------------------------------------------
|
2004-01-16 08:06:31 +01:00
|
|
|
local function should_redirect(reqt, respt)
|
2004-03-16 07:42:53 +01:00
|
|
|
return (reqt.redirect ~= false) and
|
|
|
|
(respt.code == 301 or respt.code == 302) and
|
2004-01-19 01:24:41 +01:00
|
|
|
(reqt.method == "GET" or reqt.method == "HEAD") and
|
|
|
|
not (reqt.nredirects and reqt.nredirects >= 5)
|
2001-01-25 23:01:37 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2001-07-29 05:51:36 +02:00
|
|
|
-- Returns the result of a request following a server redirect message.
|
2001-01-25 23:01:37 +01:00
|
|
|
-- Input
|
2004-01-16 08:06:31 +01:00
|
|
|
-- reqt: a table with the original request information
|
2004-03-16 07:42:53 +01:00
|
|
|
-- respt: response table of previous attempt
|
2001-01-25 23:01:37 +01:00
|
|
|
-- Returns
|
2004-01-16 08:06:31 +01:00
|
|
|
-- respt: result of target redirection
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
local function redirect(reqt, respt)
|
|
|
|
local nredirects = reqt.nredirects or 0
|
|
|
|
nredirects = nredirects + 1
|
|
|
|
local redirt = {
|
|
|
|
nredirects = nredirects,
|
|
|
|
method = reqt.method,
|
2001-08-07 21:50:04 +02:00
|
|
|
-- the RFC says the redirect URL has to be absolute, but some
|
2001-07-29 05:51:36 +02:00
|
|
|
-- servers do not respect that
|
2004-01-16 08:06:31 +01:00
|
|
|
url = socket.url.absolute(reqt.url, respt.headers["location"]),
|
2004-03-16 07:42:53 +01:00
|
|
|
source = reqt.source,
|
|
|
|
sink = reqt.sink,
|
2004-01-19 01:24:41 +01:00
|
|
|
headers = reqt.headers,
|
|
|
|
timeout = reqt.timeout,
|
2004-01-19 19:22:51 +01:00
|
|
|
proxy = reqt.proxy
|
2001-07-29 05:51:36 +02:00
|
|
|
}
|
2004-03-16 07:42:53 +01:00
|
|
|
respt = request_cb(redirt)
|
2001-09-26 22:40:13 +02:00
|
|
|
-- we pass the location header as a clue we tried to redirect
|
2004-01-16 08:06:31 +01:00
|
|
|
if respt.headers then respt.headers.location = redirt.url end
|
|
|
|
return respt
|
2001-07-29 05:51:36 +02:00
|
|
|
end
|
|
|
|
|
2001-01-25 23:01:37 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2001-08-07 21:50:04 +02:00
|
|
|
-- Computes the request URI from the parsed request URL
|
2004-01-19 06:41:30 +01:00
|
|
|
-- If we are using a proxy, we use the absoluteURI format.
|
|
|
|
-- Otherwise, we use the abs_path format.
|
2001-07-29 05:51:36 +02:00
|
|
|
-- Input
|
2001-08-07 21:50:04 +02:00
|
|
|
-- parsed: parsed URL
|
2001-07-29 05:51:36 +02:00
|
|
|
-- Returns
|
|
|
|
-- uri: request URI for parsed URL
|
|
|
|
-----------------------------------------------------------------------------
|
2004-01-19 01:24:41 +01:00
|
|
|
local function request_uri(reqt, parsed)
|
|
|
|
local url
|
2004-01-19 19:22:51 +01:00
|
|
|
if not reqt.proxy then
|
2004-01-19 01:24:41 +01:00
|
|
|
url = {
|
|
|
|
path = parsed.path,
|
|
|
|
params = parsed.params,
|
|
|
|
query = parsed.query,
|
|
|
|
fragment = parsed.fragment
|
|
|
|
}
|
|
|
|
else url = parsed end
|
|
|
|
return socket.url.build(url)
|
2001-01-25 23:01:37 +01:00
|
|
|
end
|
|
|
|
|
2001-09-12 20:16:09 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Builds a request table from a URL or request table
|
|
|
|
-- Input
|
|
|
|
-- url_or_request: target url or request table (a table with the fields:
|
|
|
|
-- url: the target URL
|
|
|
|
-- user: account user name
|
|
|
|
-- password: account password)
|
|
|
|
-- Returns
|
2004-01-16 08:06:31 +01:00
|
|
|
-- reqt: request table
|
2001-09-12 20:16:09 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2004-01-16 08:06:31 +01:00
|
|
|
local function build_request(data)
|
|
|
|
local reqt = {}
|
2003-08-31 02:58:07 +02:00
|
|
|
if type(data) == "table" then
|
|
|
|
for i, v in data
|
2004-01-16 08:06:31 +01:00
|
|
|
do reqt[i] = v
|
2003-08-31 02:58:07 +02:00
|
|
|
end
|
2004-01-16 08:06:31 +01:00
|
|
|
else reqt.url = data end
|
|
|
|
return reqt
|
2001-09-12 20:16:09 +02:00
|
|
|
end
|
|
|
|
|
2004-01-19 19:22:51 +01:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Connects to a server, be it a proxy or not
|
|
|
|
-- Input
|
|
|
|
-- reqt: the request table
|
|
|
|
-- parsed: the parsed request url
|
|
|
|
-- Returns
|
|
|
|
-- sock: connection socket, or nil in case of error
|
|
|
|
-- err: error message
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
local function try_connect(reqt, parsed)
|
|
|
|
reqt.proxy = reqt.proxy or PROXY
|
|
|
|
local host, port
|
|
|
|
if reqt.proxy then
|
|
|
|
local pproxy = socket.url.parse(reqt.proxy)
|
|
|
|
if not pproxy.port or not pproxy.host then
|
|
|
|
return nil, "invalid proxy"
|
|
|
|
end
|
|
|
|
host, port = pproxy.host, pproxy.port
|
|
|
|
else
|
|
|
|
host, port = parsed.host, parsed.port
|
|
|
|
end
|
|
|
|
local sock, ret, err
|
|
|
|
sock, err = socket.tcp()
|
|
|
|
if not sock then return nil, err end
|
|
|
|
sock:settimeout(reqt.timeout or TIMEOUT)
|
|
|
|
ret, err = sock:connect(host, port)
|
|
|
|
if not ret then
|
|
|
|
sock:close()
|
|
|
|
return nil, err
|
|
|
|
end
|
|
|
|
return sock
|
|
|
|
end
|
|
|
|
|
2001-01-25 23:01:37 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2001-07-29 05:51:36 +02:00
|
|
|
-- Sends a HTTP request and retrieves the server reply using callbacks to
|
|
|
|
-- send the request body and receive the response body
|
2001-01-25 23:01:37 +01:00
|
|
|
-- Input
|
2004-01-16 08:06:31 +01:00
|
|
|
-- reqt: a table with the following fields
|
2001-07-29 05:51:36 +02:00
|
|
|
-- method: "GET", "PUT", "POST" etc (defaults to "GET")
|
|
|
|
-- url: target uniform resource locator
|
|
|
|
-- user, password: authentication information
|
|
|
|
-- headers: request headers to send, or nil if none
|
2004-03-16 07:42:53 +01:00
|
|
|
-- source: request message body source, or nil if none
|
|
|
|
-- sink: response message body sink
|
2004-01-19 01:24:41 +01:00
|
|
|
-- redirect: should we refrain from following a server redirect message?
|
2001-01-25 23:01:37 +01:00
|
|
|
-- Returns
|
2004-01-16 08:06:31 +01:00
|
|
|
-- respt: a table with the following fields:
|
2001-07-29 05:51:36 +02:00
|
|
|
-- headers: response header fields received, or nil if failed
|
|
|
|
-- status: server response status line, or nil if failed
|
|
|
|
-- code: server status code, or nil if failed
|
|
|
|
-- error: error message, or nil if successfull
|
2001-06-06 22:55:45 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2004-03-16 07:42:53 +01:00
|
|
|
function request_cb(reqt)
|
2004-01-19 06:41:30 +01:00
|
|
|
local sock, ret
|
2004-01-16 08:06:31 +01:00
|
|
|
local parsed = socket.url.parse(reqt.url, {
|
2001-09-18 22:22:59 +02:00
|
|
|
host = "",
|
2004-01-16 08:06:31 +01:00
|
|
|
port = PORT,
|
2001-09-26 22:40:13 +02:00
|
|
|
path ="/",
|
|
|
|
scheme = "http"
|
2001-09-18 22:22:59 +02:00
|
|
|
})
|
2004-03-16 07:42:53 +01:00
|
|
|
local respt = {}
|
2001-09-26 22:40:13 +02:00
|
|
|
if parsed.scheme ~= "http" then
|
2004-01-16 08:06:31 +01:00
|
|
|
respt.error = string.format("unknown scheme '%s'", parsed.scheme)
|
|
|
|
return respt
|
2001-09-26 22:40:13 +02:00
|
|
|
end
|
2001-08-07 21:50:04 +02:00
|
|
|
-- explicit authentication info overrides that given by the URL
|
2004-01-16 08:06:31 +01:00
|
|
|
parsed.user = reqt.user or parsed.user
|
|
|
|
parsed.password = reqt.password or parsed.password
|
2001-07-29 05:51:36 +02:00
|
|
|
-- default method
|
2004-01-16 08:06:31 +01:00
|
|
|
reqt.method = reqt.method or "GET"
|
2001-07-29 05:51:36 +02:00
|
|
|
-- fill default headers
|
2004-01-16 08:06:31 +01:00
|
|
|
reqt.headers = fill_headers(reqt.headers, parsed)
|
2001-07-29 05:51:36 +02:00
|
|
|
-- try to connect to server
|
2004-01-19 19:22:51 +01:00
|
|
|
sock, respt.error = try_connect(reqt, parsed)
|
2004-01-16 08:06:31 +01:00
|
|
|
if not sock then return respt end
|
2001-07-29 05:51:36 +02:00
|
|
|
-- send request message
|
2004-01-16 08:06:31 +01:00
|
|
|
respt.error = send_request(sock, reqt.method,
|
2004-03-16 07:42:53 +01:00
|
|
|
request_uri(reqt, parsed), reqt.headers, reqt.source)
|
2004-01-16 08:06:31 +01:00
|
|
|
if respt.error then
|
|
|
|
sock:close()
|
|
|
|
return respt
|
|
|
|
end
|
2001-07-29 05:51:36 +02:00
|
|
|
-- get server response message
|
2004-01-16 08:06:31 +01:00
|
|
|
respt.code, respt.status, respt.error = receive_status(sock)
|
|
|
|
if respt.error then return respt end
|
|
|
|
-- deal with continue 100
|
2004-01-19 01:24:41 +01:00
|
|
|
-- servers should not send them, but some do!
|
2004-01-16 08:06:31 +01:00
|
|
|
if respt.code == 100 then
|
|
|
|
respt.headers, respt.error = receive_headers(sock, {})
|
|
|
|
if respt.error then return respt end
|
|
|
|
respt.code, respt.status, respt.error = receive_status(sock)
|
|
|
|
if respt.error then return respt end
|
2002-07-08 23:01:18 +02:00
|
|
|
end
|
2001-07-29 05:51:36 +02:00
|
|
|
-- receive all headers
|
2004-01-16 08:06:31 +01:00
|
|
|
respt.headers, respt.error = receive_headers(sock, {})
|
|
|
|
if respt.error then return respt end
|
2001-07-29 05:51:36 +02:00
|
|
|
-- decide what to do based on request and response parameters
|
2004-01-16 08:06:31 +01:00
|
|
|
if should_redirect(reqt, respt) then
|
|
|
|
-- drop the body
|
2004-03-16 07:42:53 +01:00
|
|
|
receive_body(sock, respt.headers, ltn12.sink.null())
|
2004-01-16 08:06:31 +01:00
|
|
|
-- we are done with this connection
|
2001-07-29 05:51:36 +02:00
|
|
|
sock:close()
|
2004-01-16 08:06:31 +01:00
|
|
|
return redirect(reqt, respt)
|
|
|
|
elseif should_authorize(reqt, parsed, respt) then
|
|
|
|
-- drop the body
|
2004-03-16 07:42:53 +01:00
|
|
|
receive_body(sock, respt.headers, ltn12.sink.null())
|
2004-01-16 08:06:31 +01:00
|
|
|
-- we are done with this connection
|
2001-07-29 05:51:36 +02:00
|
|
|
sock:close()
|
2004-01-16 08:06:31 +01:00
|
|
|
return authorize(reqt, parsed, respt)
|
|
|
|
elseif should_receive_body(reqt, respt) then
|
2004-03-16 07:42:53 +01:00
|
|
|
respt.error = receive_body(sock, respt.headers, reqt.sink)
|
2004-01-16 08:06:31 +01:00
|
|
|
if respt.error then return respt end
|
2001-07-29 05:51:36 +02:00
|
|
|
sock:close()
|
2004-01-16 08:06:31 +01:00
|
|
|
return respt
|
2001-07-29 05:51:36 +02:00
|
|
|
end
|
|
|
|
sock:close()
|
2004-01-16 08:06:31 +01:00
|
|
|
return respt
|
2001-06-06 22:55:45 +02:00
|
|
|
end
|
|
|
|
|
2001-01-25 23:01:37 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2001-07-29 05:51:36 +02:00
|
|
|
-- Sends a HTTP request and retrieves the server reply
|
2001-06-06 22:55:45 +02:00
|
|
|
-- Input
|
2004-01-16 08:06:31 +01:00
|
|
|
-- reqt: a table with the following fields
|
2001-07-29 05:51:36 +02:00
|
|
|
-- method: "GET", "PUT", "POST" etc (defaults to "GET")
|
2001-08-07 21:50:04 +02:00
|
|
|
-- url: request URL, i.e. the document to be retrieved
|
2001-07-29 05:51:36 +02:00
|
|
|
-- user, password: authentication information
|
|
|
|
-- headers: request header fields, or nil if none
|
|
|
|
-- body: request message body as a string, or nil if none
|
2004-01-19 01:24:41 +01:00
|
|
|
-- redirect: should we refrain from following a server redirect message?
|
2001-06-06 22:55:45 +02:00
|
|
|
-- Returns
|
2004-01-16 08:06:31 +01:00
|
|
|
-- respt: a table with the following fields:
|
2001-07-29 05:51:36 +02:00
|
|
|
-- body: response message body, or nil if failed
|
|
|
|
-- headers: response header fields, or nil if failed
|
|
|
|
-- status: server response status line, or nil if failed
|
|
|
|
-- code: server response status code, or nil if failed
|
|
|
|
-- error: error message if any
|
|
|
|
-----------------------------------------------------------------------------
|
2004-01-16 08:06:31 +01:00
|
|
|
function request(reqt)
|
2004-03-16 07:42:53 +01:00
|
|
|
reqt.source = reqt.body and ltn12.source.string(reqt.body)
|
|
|
|
local t = {}
|
|
|
|
reqt.sink = ltn12.sink.table(t)
|
|
|
|
local respt = request_cb(reqt)
|
|
|
|
if table.getn(t) > 0 then respt.body = table.concat(t) end
|
2004-01-16 08:06:31 +01:00
|
|
|
return respt
|
2001-07-29 05:51:36 +02:00
|
|
|
end
|
|
|
|
|
2001-06-06 22:55:45 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2001-07-29 05:51:36 +02:00
|
|
|
-- Retrieves a URL by the method "GET"
|
|
|
|
-- Input
|
2001-09-12 20:16:09 +02:00
|
|
|
-- url_or_request: target url or request table (a table with the fields:
|
|
|
|
-- url: the target URL
|
|
|
|
-- user: account user name
|
|
|
|
-- password: account password)
|
2001-07-29 05:51:36 +02:00
|
|
|
-- Returns
|
|
|
|
-- body: response message body, or nil if failed
|
|
|
|
-- headers: response header fields received, or nil if failed
|
2001-09-18 20:41:01 +02:00
|
|
|
-- code: server response status code, or nil if failed
|
2001-07-29 05:51:36 +02:00
|
|
|
-- error: error message if any
|
|
|
|
-----------------------------------------------------------------------------
|
2004-01-16 08:06:31 +01:00
|
|
|
function get(url_or_request)
|
|
|
|
local reqt = build_request(url_or_request)
|
|
|
|
reqt.method = "GET"
|
|
|
|
local respt = request(reqt)
|
|
|
|
return respt.body, respt.headers, respt.code, respt.error
|
2001-06-06 22:55:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Retrieves a URL by the method "POST"
|
|
|
|
-- Input
|
2001-09-12 20:16:09 +02:00
|
|
|
-- url_or_request: target url or request table (a table with the fields:
|
|
|
|
-- url: the target URL
|
|
|
|
-- body: request message body
|
|
|
|
-- user: account user name
|
|
|
|
-- password: account password)
|
2001-07-29 05:51:36 +02:00
|
|
|
-- body: request message body, or nil if none
|
2001-06-06 22:55:45 +02:00
|
|
|
-- Returns
|
2001-07-29 05:51:36 +02:00
|
|
|
-- body: response message body, or nil if failed
|
|
|
|
-- headers: response header fields received, or nil if failed
|
2001-09-18 20:41:01 +02:00
|
|
|
-- code: server response status code, or nil if failed
|
2001-07-29 05:51:36 +02:00
|
|
|
-- error: error message, or nil if successfull
|
|
|
|
-----------------------------------------------------------------------------
|
2004-01-16 08:06:31 +01:00
|
|
|
function post(url_or_request, body)
|
|
|
|
local reqt = build_request(url_or_request)
|
|
|
|
reqt.method = "POST"
|
|
|
|
reqt.body = reqt.body or body
|
|
|
|
reqt.headers = reqt.headers or
|
|
|
|
{ ["content-length"] = string.len(reqt.body) }
|
|
|
|
local respt = request(reqt)
|
|
|
|
return respt.body, respt.headers, respt.code, respt.error
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
2004-01-16 08:06:31 +01:00
|
|
|
|
2004-03-16 07:42:53 +01:00
|
|
|
return socket.http
|