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.
|
2001-09-12 20:16:09 +02:00
|
|
|
-- LuaSocket 1.4 toolkit.
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Author: Diego Nehab
|
|
|
|
-- Date: 26/12/2000
|
2001-08-07 21:50:04 +02:00
|
|
|
-- Conforming to: RFC 2616, LTN7
|
|
|
|
-- RCS ID: $Id$
|
2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
2001-07-29 05:51:36 +02:00
|
|
|
local Public, Private = {}, {}
|
|
|
|
HTTP = Public
|
|
|
|
|
2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Program constants
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- connection timeout in seconds
|
2001-08-07 21:50:04 +02:00
|
|
|
Public.TIMEOUT = 60
|
2000-12-29 23:15:09 +01:00
|
|
|
-- default port for document retrieval
|
2001-08-07 21:50:04 +02:00
|
|
|
Public.PORT = 80
|
2000-12-29 23:15:09 +01:00
|
|
|
-- user agent field sent in request
|
2001-09-12 20:16:09 +02:00
|
|
|
Public.USERAGENT = "LuaSocket 1.4"
|
2001-06-06 22:55:45 +02:00
|
|
|
-- block size used in transfers
|
2001-08-07 21:50:04 +02:00
|
|
|
Public.BLOCKSIZE = 8192
|
2001-07-29 05:51:36 +02:00
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Required libraries
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:16:09 +02:00
|
|
|
dofile "concat.lua"
|
2001-07-29 05:51:36 +02:00
|
|
|
dofile "url.lua"
|
2001-08-07 21:50:04 +02:00
|
|
|
dofile "code.lua"
|
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
|
2001-09-12 20:16:09 +02:00
|
|
|
-- ...: pattern to receive
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Returns
|
2001-09-12 20:16:09 +02:00
|
|
|
-- ...: received pattern
|
2000-12-29 23:15:09 +01:00
|
|
|
-- err: error message if any
|
|
|
|
-----------------------------------------------------------------------------
|
2001-07-29 05:51:36 +02:00
|
|
|
function Private.try_receive(...)
|
2001-01-25 23:01:37 +01:00
|
|
|
local sock = arg[1]
|
2001-07-29 05:51:36 +02:00
|
|
|
local data, err = call(sock.receive, arg)
|
2001-01-25 23:01:37 +01:00
|
|
|
if err then
|
|
|
|
sock:close()
|
|
|
|
return nil, err
|
|
|
|
end
|
|
|
|
return data
|
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
|
2001-01-25 23:01:37 +01:00
|
|
|
-- data: 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
|
|
|
-----------------------------------------------------------------------------
|
2001-07-29 05:51:36 +02:00
|
|
|
function Private.try_send(sock, data)
|
|
|
|
local err = sock:send(data)
|
2001-01-25 23:01:37 +01:00
|
|
|
if err then sock:close() end
|
|
|
|
return err
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2001-08-07 21:50:04 +02:00
|
|
|
-- Computes status code from HTTP status line
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Input
|
2001-08-07 21:50:04 +02:00
|
|
|
-- line: HTTP status line
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Returns
|
2001-08-07 21:50:04 +02:00
|
|
|
-- code: integer with status code, or nil if malformed line
|
2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2001-07-29 05:51:36 +02:00
|
|
|
function Private.get_statuscode(line)
|
|
|
|
local code, _
|
|
|
|
_, _, code = strfind(line, "HTTP/%d*%.%d* (%d%d%d)")
|
2001-01-25 23:01:37 +01:00
|
|
|
return tonumber(code)
|
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
|
|
|
|
-----------------------------------------------------------------------------
|
2001-07-29 05:51:36 +02:00
|
|
|
function Private.receive_status(sock)
|
2001-01-25 23:01:37 +01:00
|
|
|
local line, err
|
2001-07-29 05:51:36 +02:00
|
|
|
line, err = %Private.try_receive(sock)
|
|
|
|
if not err then return %Private.get_statuscode(line), 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
|
|
|
|
-----------------------------------------------------------------------------
|
2001-07-29 05:51:36 +02:00
|
|
|
function Private.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, _
|
2001-01-25 23:01:37 +01:00
|
|
|
-- get first line
|
2001-07-29 05:51:36 +02:00
|
|
|
line, err = %Private.try_receive(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
|
2001-08-07 21:50:04 +02:00
|
|
|
_,_, name, value = strfind(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
|
2000-12-29 23:15:09 +01:00
|
|
|
name = strlower(name)
|
|
|
|
-- get next line (value might be folded)
|
2001-07-29 05:51:36 +02:00
|
|
|
line, err = %Private.try_receive(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
|
2001-01-25 23:01:37 +01:00
|
|
|
while not err and strfind(line, "^%s") do
|
2000-12-29 23:15:09 +01:00
|
|
|
value = value .. line
|
2001-07-29 05:51:36 +02:00
|
|
|
line, err = %Private.try_receive(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
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- 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
|
2001-06-06 22:55:45 +02:00
|
|
|
-- receive_cb: function to receive chunks
|
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
|
|
|
-----------------------------------------------------------------------------
|
2001-07-29 05:51:36 +02:00
|
|
|
function Private.receivebody_bychunks(sock, headers, receive_cb)
|
2001-06-06 22:55:45 +02:00
|
|
|
local chunk, size, line, err, go, uerr, _
|
2001-07-29 05:51:36 +02:00
|
|
|
while 1 do
|
2001-01-25 23:01:37 +01:00
|
|
|
-- get chunk size, skip extention
|
2001-07-29 05:51:36 +02:00
|
|
|
line, err = %Private.try_receive(sock)
|
2001-05-21 20:12:20 +02:00
|
|
|
if err then
|
2001-08-07 21:50:04 +02:00
|
|
|
local go, uerr = receive_cb(nil, err)
|
2001-06-06 22:55:45 +02:00
|
|
|
return uerr or err
|
|
|
|
end
|
2001-05-21 20:12:20 +02:00
|
|
|
size = tonumber(gsub(line, ";.*", ""), 16)
|
|
|
|
if not size then
|
2001-06-06 22:55:45 +02:00
|
|
|
err = "invalid chunk size"
|
2001-01-25 23:01:37 +01:00
|
|
|
sock:close()
|
2001-08-07 21:50:04 +02:00
|
|
|
go, uerr = receive_cb(nil, err)
|
2001-06-06 22:55:45 +02:00
|
|
|
return uerr or err
|
2001-01-25 23:01:37 +01:00
|
|
|
end
|
2001-07-29 05:51:36 +02:00
|
|
|
-- was it the last chunk?
|
|
|
|
if size <= 0 then break end
|
2001-01-25 23:01:37 +01:00
|
|
|
-- get chunk
|
2001-07-29 05:51:36 +02:00
|
|
|
chunk, err = %Private.try_receive(sock, size)
|
2001-05-21 20:12:20 +02:00
|
|
|
if err then
|
2001-08-07 21:50:04 +02:00
|
|
|
go, uerr = receive_cb(nil, err)
|
2001-06-06 22:55:45 +02:00
|
|
|
return uerr or err
|
|
|
|
end
|
|
|
|
-- pass chunk to callback
|
|
|
|
go, uerr = receive_cb(chunk)
|
|
|
|
if not go then
|
|
|
|
sock:close()
|
|
|
|
return uerr or "aborted by callback"
|
|
|
|
end
|
2001-07-29 05:51:36 +02:00
|
|
|
-- skip CRLF on end of chunk
|
|
|
|
_, err = %Private.try_receive(sock)
|
2001-05-21 20:12:20 +02:00
|
|
|
if err then
|
2001-08-07 21:50:04 +02:00
|
|
|
go, uerr = receive_cb(nil, err)
|
2001-06-06 22:55:45 +02:00
|
|
|
return uerr or err
|
|
|
|
end
|
2001-07-29 05:51:36 +02:00
|
|
|
end
|
|
|
|
-- the server should not send trailer headers because we didn't send a
|
|
|
|
-- header informing it we know how to deal with them. we do not risk
|
|
|
|
-- being caught unprepaired.
|
|
|
|
headers, err = %Private.receive_headers(sock, headers)
|
|
|
|
if err then
|
2001-08-07 21:50:04 +02:00
|
|
|
go, uerr = receive_cb(nil, err)
|
2001-07-29 05:51:36 +02:00
|
|
|
return uerr or err
|
|
|
|
end
|
2001-06-06 22:55:45 +02:00
|
|
|
-- let callback know we are done
|
2001-08-07 21:50:04 +02:00
|
|
|
go, uerr = receive_cb("")
|
2001-06-06 22:55:45 +02:00
|
|
|
return uerr
|
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
|
2001-06-06 22:55:45 +02:00
|
|
|
-- receive_cb: function to receive chunks
|
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
|
|
|
|
-----------------------------------------------------------------------------
|
2001-07-29 05:51:36 +02:00
|
|
|
function Private.receivebody_bylength(sock, length, receive_cb)
|
2001-06-06 22:55:45 +02:00
|
|
|
local uerr, go
|
|
|
|
while length > 0 do
|
2001-08-07 21:50:04 +02:00
|
|
|
local size = min(%Public.BLOCKSIZE, length)
|
2001-06-06 22:55:45 +02:00
|
|
|
local chunk, err = sock:receive(size)
|
|
|
|
if err then
|
|
|
|
go, uerr = receive_cb(nil, err)
|
|
|
|
return uerr or err
|
|
|
|
end
|
|
|
|
go, uerr = receive_cb(chunk)
|
|
|
|
if not go then
|
|
|
|
sock:close()
|
|
|
|
return uerr or "aborted by callback"
|
|
|
|
end
|
|
|
|
length = length - size
|
|
|
|
end
|
|
|
|
go, uerr = receive_cb("")
|
|
|
|
return uerr
|
2001-05-21 20:12:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Receives a message body by content-length
|
|
|
|
-- Input
|
|
|
|
-- sock: socket connected to the server
|
2001-06-06 22:55:45 +02:00
|
|
|
-- receive_cb: function to receive chunks
|
2001-05-21 20:12:20 +02:00
|
|
|
-- Returns
|
|
|
|
-- nil if successfull or an error message in case of error
|
|
|
|
-----------------------------------------------------------------------------
|
2001-07-29 05:51:36 +02:00
|
|
|
function Private.receivebody_untilclosed(sock, receive_cb)
|
2001-06-06 22:55:45 +02:00
|
|
|
local err, go, uerr
|
|
|
|
while 1 do
|
2001-08-07 21:50:04 +02:00
|
|
|
local chunk, err = sock:receive(%Public.BLOCKSIZE)
|
2001-06-06 22:55:45 +02:00
|
|
|
if err == "closed" or not err then
|
|
|
|
go, uerr = receive_cb(chunk)
|
|
|
|
if not go then
|
|
|
|
sock:close()
|
|
|
|
return uerr or "aborted by callback"
|
|
|
|
end
|
2001-08-07 21:50:04 +02:00
|
|
|
if err == "closed" then break end
|
2001-06-06 22:55:45 +02:00
|
|
|
else
|
|
|
|
go, uerr = callback(nil, err)
|
|
|
|
return uerr or err
|
|
|
|
end
|
|
|
|
end
|
|
|
|
go, uerr = receive_cb("")
|
|
|
|
return uerr
|
2001-05-21 20:12:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2001-08-07 21:50:04 +02:00
|
|
|
-- Receives 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
|
2001-06-06 22:55:45 +02:00
|
|
|
-- receive_cb: function to receive chunks
|
2001-05-21 20:12:20 +02:00
|
|
|
-- Returns
|
|
|
|
-- nil if successfull or an error message in case of error
|
|
|
|
-----------------------------------------------------------------------------
|
2001-07-29 05:51:36 +02:00
|
|
|
function Private.receive_body(sock, headers, receive_cb)
|
|
|
|
local te = headers["transfer-encoding"]
|
|
|
|
if te and te ~= "identity" then
|
2001-05-21 20:12:20 +02:00
|
|
|
-- get by chunked transfer-coding of message body
|
2001-07-29 05:51:36 +02:00
|
|
|
return %Private.receivebody_bychunks(sock, headers, receive_cb)
|
|
|
|
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"])
|
|
|
|
return %Private.receivebody_bylength(sock, length, receive_cb)
|
2000-12-29 23:15:09 +01:00
|
|
|
else
|
2001-05-21 20:12:20 +02:00
|
|
|
-- get it all until connection closes
|
2001-07-29 05:51:36 +02:00
|
|
|
return %Private.receivebody_untilclosed(sock, receive_cb)
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2001-08-07 21:50:04 +02:00
|
|
|
-- Drop HTTP response body
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Input
|
2001-07-29 05:51:36 +02:00
|
|
|
-- sock: socket connected to the server
|
|
|
|
-- headers: response header fields
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Returns
|
2001-07-29 05:51:36 +02:00
|
|
|
-- nil if successfull or an error message in case of error
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Private.drop_body(sock, headers)
|
|
|
|
return %Private.receive_body(sock, headers, function (c, e) return 1 end)
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
2001-06-06 22:55:45 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Sends data comming from a callback
|
|
|
|
-- Input
|
|
|
|
-- data: data connection
|
|
|
|
-- send_cb: callback to produce file contents
|
2001-08-07 21:50:04 +02:00
|
|
|
-- chunk, size: first callback return values
|
2001-06-06 22:55:45 +02:00
|
|
|
-- Returns
|
|
|
|
-- nil if successfull, or an error message in case of error
|
|
|
|
-----------------------------------------------------------------------------
|
2001-07-29 05:51:36 +02:00
|
|
|
function Private.send_indirect(data, send_cb, chunk, size)
|
2001-06-06 22:55:45 +02:00
|
|
|
local sent, err
|
|
|
|
sent = 0
|
|
|
|
while 1 do
|
|
|
|
if type(chunk) ~= "string" or type(size) ~= "number" then
|
2001-07-29 05:51:36 +02:00
|
|
|
data:close()
|
2001-06-06 22:55:45 +02:00
|
|
|
if not chunk and type(size) == "string" then return size
|
|
|
|
else return "invalid callback return" end
|
|
|
|
end
|
|
|
|
err = data:send(chunk)
|
|
|
|
if err then
|
2001-07-29 05:51:36 +02:00
|
|
|
data:close()
|
|
|
|
return err
|
|
|
|
end
|
2001-06-06 22:55:45 +02:00
|
|
|
sent = sent + strlen(chunk)
|
|
|
|
if sent >= size then break end
|
|
|
|
chunk, size = send_cb()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2001-09-12 20:16:09 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Sends mime headers
|
|
|
|
-- Input
|
|
|
|
-- sock: server socket
|
|
|
|
-- headers: table with mime headers to be sent
|
|
|
|
-- Returns
|
|
|
|
-- err: error message if any
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Private.send_headers(sock, headers)
|
|
|
|
local err
|
|
|
|
headers = headers or {}
|
|
|
|
-- send request headers
|
|
|
|
for i, v in headers do
|
|
|
|
err = %Private.try_send(sock, i .. ": " .. v .. "\r\n")
|
|
|
|
if err then return err end
|
|
|
|
end
|
|
|
|
-- mark end of request headers
|
|
|
|
return %Private.try_send(sock, "\r\n")
|
|
|
|
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
|
|
|
|
-- body_cb: callback to send request message body
|
2001-01-25 23:01:37 +01:00
|
|
|
-- Returns
|
|
|
|
-- err: nil in case of success, error message otherwise
|
|
|
|
-----------------------------------------------------------------------------
|
2001-08-07 21:50:04 +02:00
|
|
|
function Private.send_request(sock, method, uri, headers, body_cb)
|
2001-06-06 22:55:45 +02:00
|
|
|
local chunk, size, done, err
|
|
|
|
-- send request line
|
2001-08-07 21:50:04 +02:00
|
|
|
err = %Private.try_send(sock, method .. " " .. uri .. " HTTP/1.1\r\n")
|
2001-01-25 23:01:37 +01:00
|
|
|
if err then return err end
|
2001-06-06 22:55:45 +02:00
|
|
|
-- if there is a request message body, add content-length header
|
2001-07-29 05:51:36 +02:00
|
|
|
if body_cb then
|
|
|
|
chunk, size = body_cb()
|
2001-06-06 22:55:45 +02:00
|
|
|
if type(chunk) == "string" and type(size) == "number" then
|
2001-07-29 05:51:36 +02:00
|
|
|
headers["content-length"] = tostring(size)
|
2001-06-06 22:55:45 +02:00
|
|
|
else
|
|
|
|
sock:close()
|
2001-07-29 05:51:36 +02:00
|
|
|
if not chunk and type(size) == "string" then return size
|
|
|
|
else return "invalid callback return" end
|
2001-06-06 22:55:45 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
-- send request headers
|
2001-09-12 20:16:09 +02:00
|
|
|
err = %Private.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
|
2001-07-29 05:51:36 +02:00
|
|
|
if body_cb then
|
|
|
|
return %Private.send_indirect(sock, body_cb, chunk, size)
|
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
|
2001-07-29 05:51:36 +02:00
|
|
|
-- request: a table with the original request information
|
|
|
|
-- response: 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
|
|
|
|
-----------------------------------------------------------------------------
|
2001-07-29 05:51:36 +02:00
|
|
|
function Private.has_body(request, response)
|
|
|
|
if request.method == "HEAD" then return nil end
|
|
|
|
if response.code == 204 or response.code == 304 then return nil end
|
|
|
|
if response.code >= 100 and response.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
|
|
|
|
-----------------------------------------------------------------------------
|
2001-07-29 05:51:36 +02:00
|
|
|
function Private.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
|
2001-08-07 21:50:04 +02:00
|
|
|
lower["user-agent"] = %Public.USERAGENT
|
2001-07-29 05:51:36 +02:00
|
|
|
lower["host"] = parsed.host
|
|
|
|
-- override with user values
|
|
|
|
for i,v in headers do
|
2001-01-25 23:01:37 +01:00
|
|
|
lower[strlower(i)] = v
|
|
|
|
end
|
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
|
2001-07-29 05:51:36 +02:00
|
|
|
-- request: 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
|
|
|
-- response: 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
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Private.should_authorize(request, parsed, response)
|
|
|
|
-- if there has been an authorization attempt, it must have failed
|
|
|
|
if request.headers["authorization"] then return nil end
|
|
|
|
-- 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
|
|
|
|
-- request: 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
|
|
|
-- response: a table with the server response information
|
|
|
|
-- Returns
|
|
|
|
-- response: result of target redirection
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Private.authorize(request, parsed, response)
|
|
|
|
request.headers["authorization"] = "Basic " ..
|
2001-08-07 21:50:04 +02:00
|
|
|
Code.base64(parsed.user .. ":" .. parsed.password)
|
2001-07-29 05:51:36 +02:00
|
|
|
local authorize = {
|
|
|
|
redirects = request.redirects,
|
|
|
|
method = request.method,
|
|
|
|
url = request.url,
|
|
|
|
body_cb = request.body_cb,
|
|
|
|
headers = request.headers
|
|
|
|
}
|
2001-09-12 20:16:09 +02:00
|
|
|
return %Public.request_cb(authorize, response)
|
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
|
2001-07-29 05:51:36 +02:00
|
|
|
-- request: a table with the original request information
|
|
|
|
-- response: 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
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Private.should_redirect(request, response)
|
|
|
|
local follow = not request.stay
|
|
|
|
follow = follow and (response.code == 301 or response.code == 302)
|
|
|
|
follow = follow and (request.method == "GET" or request.method == "HEAD")
|
|
|
|
follow = follow and not (request.redirects and request.redirects >= 5)
|
|
|
|
return follow
|
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
|
2001-07-29 05:51:36 +02:00
|
|
|
-- request: a table with the original request information
|
|
|
|
-- response: a table with the following fields:
|
|
|
|
-- body_cb: response method body receive-callback
|
2001-01-25 23:01:37 +01:00
|
|
|
-- Returns
|
2001-07-29 05:51:36 +02:00
|
|
|
-- response: result of target redirection
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Private.redirect(request, response)
|
|
|
|
local redirects = request.redirects or 0
|
|
|
|
redirects = redirects + 1
|
|
|
|
local redirect = {
|
|
|
|
redirects = redirects,
|
|
|
|
method = request.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
|
2001-08-07 21:50:04 +02:00
|
|
|
url = URL.absolute_url(request.url, response.headers["location"]),
|
2001-07-29 05:51:36 +02:00
|
|
|
body_cb = request.body_cb,
|
|
|
|
headers = request.headers
|
|
|
|
}
|
2001-09-12 20:16:09 +02:00
|
|
|
return %Public.request_cb(redirect, response)
|
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
|
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
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Private.request_uri(parsed)
|
|
|
|
local uri = ""
|
|
|
|
if parsed.path then uri = uri .. parsed.path end
|
|
|
|
if parsed.params then uri = uri .. ";" .. parsed.params end
|
|
|
|
if parsed.query then uri = uri .. "?" .. parsed.query end
|
|
|
|
if parsed.fragment then uri = uri .. "#" .. parsed.fragment end
|
|
|
|
return uri
|
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
|
|
|
|
-- request: request table
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Private.build_request(data)
|
|
|
|
local request = {}
|
|
|
|
if type(data) == "table" then for i, v in data do request[i] = v end
|
|
|
|
else request.url = data end
|
|
|
|
return request
|
|
|
|
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
|
2001-07-29 05:51:36 +02:00
|
|
|
-- request: a table with the following fields
|
|
|
|
-- 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
|
|
|
|
-- body_cb: request message body send-callback, or nil if none
|
|
|
|
-- stay: should we refrain from following a server redirect message?
|
|
|
|
-- response: a table with the following fields:
|
|
|
|
-- body_cb: response method body receive-callback
|
2001-01-25 23:01:37 +01:00
|
|
|
-- Returns
|
2001-07-29 05:51:36 +02:00
|
|
|
-- response: a table with the following fields:
|
|
|
|
-- 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
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:16:09 +02:00
|
|
|
function Public.request_cb(request, response)
|
|
|
|
local parsed = URL.parse_url(request.url, {
|
|
|
|
host = "",
|
|
|
|
port = %Public.PORT,
|
|
|
|
path ="/"
|
|
|
|
})
|
2001-08-07 21:50:04 +02:00
|
|
|
-- explicit authentication info overrides that given by the URL
|
2001-07-29 05:51:36 +02:00
|
|
|
parsed.user = request.user or parsed.user
|
|
|
|
parsed.password = request.password or parsed.password
|
|
|
|
-- default method
|
|
|
|
request.method = request.method or "GET"
|
|
|
|
-- fill default headers
|
|
|
|
request.headers = %Private.fill_headers(request.headers, parsed)
|
|
|
|
-- try to connect to server
|
|
|
|
local sock
|
|
|
|
sock, response.error = connect(parsed.host, parsed.port)
|
|
|
|
if not sock then return response end
|
|
|
|
-- set connection timeout so that we do not hang forever
|
2001-08-07 21:50:04 +02:00
|
|
|
sock:timeout(%Public.TIMEOUT)
|
2001-07-29 05:51:36 +02:00
|
|
|
-- send request message
|
|
|
|
response.error = %Private.send_request(sock, request.method,
|
|
|
|
%Private.request_uri(parsed), request.headers, request.body_cb)
|
|
|
|
if response.error then return response end
|
|
|
|
-- get server response message
|
|
|
|
response.code, response.status, response.error =
|
|
|
|
%Private.receive_status(sock)
|
|
|
|
if response.error then return response end
|
|
|
|
-- receive all headers
|
|
|
|
response.headers, response.error = %Private.receive_headers(sock, {})
|
|
|
|
if response.error then return response end
|
|
|
|
-- decide what to do based on request and response parameters
|
|
|
|
if %Private.should_redirect(request, response) then
|
|
|
|
%Private.drop_body(sock, response.headers)
|
|
|
|
sock:close()
|
|
|
|
return %Private.redirect(request, response)
|
|
|
|
elseif %Private.should_authorize(request, parsed, response) then
|
|
|
|
%Private.drop_body(sock, response.headers)
|
|
|
|
sock:close()
|
|
|
|
return %Private.authorize(request, parsed, response)
|
|
|
|
elseif %Private.has_body(request, response) then
|
|
|
|
response.error = %Private.receive_body(sock, response.headers,
|
|
|
|
response.body_cb)
|
|
|
|
if response.error then return response end
|
|
|
|
sock:close()
|
|
|
|
return response
|
|
|
|
end
|
|
|
|
sock:close()
|
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
|
2001-07-29 05:51:36 +02:00
|
|
|
-- request: a table with the following fields
|
|
|
|
-- 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
|
|
|
|
-- stay: should we refrain from following a server redirect message?
|
2001-06-06 22:55:45 +02:00
|
|
|
-- Returns
|
2001-07-29 05:51:36 +02:00
|
|
|
-- response: a table with the following fields:
|
|
|
|
-- 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
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Public.request(request)
|
|
|
|
local response = {}
|
|
|
|
if request.body then
|
|
|
|
request.body_cb = function()
|
2001-08-07 21:50:04 +02:00
|
|
|
return %request.body, strlen(%request.body)
|
2001-07-29 05:51:36 +02:00
|
|
|
end
|
|
|
|
end
|
2001-08-07 21:50:04 +02:00
|
|
|
local cat = Concat.create()
|
2001-07-29 05:51:36 +02:00
|
|
|
response.body_cb = function(chunk, err)
|
2001-09-12 20:16:09 +02:00
|
|
|
if chunk then %cat:addstring(chunk) end
|
2001-07-29 05:51:36 +02:00
|
|
|
return 1
|
|
|
|
end
|
2001-09-12 20:16:09 +02:00
|
|
|
response = %Public.request_cb(request, response)
|
2001-08-07 21:50:04 +02:00
|
|
|
response.body = cat:getresult()
|
2001-07-29 05:51:36 +02:00
|
|
|
response.body_cb = nil
|
|
|
|
return response
|
|
|
|
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
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:16:09 +02:00
|
|
|
function Public.get(url_or_request)
|
|
|
|
local request = %Private.build_request(url_or_request)
|
|
|
|
request.method = "GET"
|
|
|
|
local response = %Public.request(request)
|
2001-07-29 05:51:36 +02:00
|
|
|
return response.body, response.headers,
|
2001-09-18 20:41:01 +02:00
|
|
|
response.code, response.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
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:16:09 +02:00
|
|
|
function Public.post(url_or_request, body)
|
|
|
|
local request = %Private.build_request(url_or_request)
|
|
|
|
request.method = "POST"
|
|
|
|
request.body = request.body or body
|
|
|
|
local response = %Public.request(request)
|
2001-07-29 05:51:36 +02:00
|
|
|
return response.body, response.headers,
|
2001-09-18 20:41:01 +02:00
|
|
|
response.code, response.error
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|