Starting to use RCS in princeton again. Not behind a firewall anymore.
This commit is contained in:
parent
c51d4acf1c
commit
6789b83ff5
6 changed files with 58 additions and 60 deletions
19
src/ftp.lua
19
src/ftp.lua
|
@ -602,10 +602,12 @@ function Public.put_cb(request)
|
|||
local control, err = Private.open(parsed)
|
||||
if not control then return err end
|
||||
local segment = Private.parse_path(parsed)
|
||||
return Private.change_dir(control, segment) or
|
||||
err = Private.change_dir(control, segment) or
|
||||
Private.change_type(control, parsed.params) or
|
||||
Private.upload(control, request, segment) or
|
||||
Private.close(control)
|
||||
if err then return nil, err
|
||||
else return 1 end
|
||||
end
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
|
@ -616,15 +618,15 @@ end
|
|||
-- type: "i" for "image" mode, "a" for "ascii" mode or "d" for directory
|
||||
-- user: account user name
|
||||
-- password: account password)
|
||||
-- content: file contents
|
||||
-- content: file contents
|
||||
-- Returns
|
||||
-- err: error message if any
|
||||
-----------------------------------------------------------------------------
|
||||
function Public.put(url_or_request, content)
|
||||
local request = Private.build_request(url_or_request)
|
||||
request.content_cb = function()
|
||||
return content, string.len(content)
|
||||
end
|
||||
request.content = request.content or content
|
||||
request.content_cb = socket.callback.send_string(request.content)
|
||||
return Public.put_cb(request)
|
||||
end
|
||||
|
||||
|
@ -641,12 +643,9 @@ end
|
|||
-- err: error message in case of error, nil otherwise
|
||||
-----------------------------------------------------------------------------
|
||||
function Public.get(url_or_request)
|
||||
local cat = socket.concat.create()
|
||||
local concat = socket.concat.create()
|
||||
local request = Private.build_request(url_or_request)
|
||||
request.content_cb = function(chunk, err)
|
||||
if chunk then cat:addstring(chunk) end
|
||||
return 1
|
||||
end
|
||||
request.content_cb = socket.callback.receive_concat(concat)
|
||||
local err = Public.get_cb(request)
|
||||
return cat:getresult(), err
|
||||
return concat:getresult(), err
|
||||
end
|
||||
|
|
40
src/http.lua
40
src/http.lua
|
@ -338,16 +338,16 @@ function Private.send_request(sock, method, uri, headers, body_cb)
|
|||
err = Private.try_send(sock, method .. " " .. uri .. " HTTP/1.1\r\n")
|
||||
if err then return err end
|
||||
-- if there is a request message body, add content-length header
|
||||
if body_cb then
|
||||
chunk, size = body_cb()
|
||||
if type(chunk) == "string" and type(size) == "number" then
|
||||
headers["content-length"] = tostring(size)
|
||||
else
|
||||
sock:close()
|
||||
if not chunk and type(size) == "string" then return size
|
||||
else return "invalid callback return" end
|
||||
end
|
||||
end
|
||||
chunk, size = body_cb()
|
||||
if type(chunk) == "string" and type(size) == "number" then
|
||||
if size > 0 then
|
||||
headers["content-length"] = tostring(size)
|
||||
end
|
||||
else
|
||||
sock:close()
|
||||
if not chunk and type(size) == "string" then return size
|
||||
else return "invalid callback return" end
|
||||
end
|
||||
-- send request headers
|
||||
err = Private.send_headers(sock, headers)
|
||||
if err then return err end
|
||||
|
@ -505,7 +505,10 @@ end
|
|||
-----------------------------------------------------------------------------
|
||||
function Private.build_request(data)
|
||||
local request = {}
|
||||
if type(data) == "table" then for i, v in data do request[i] = v end
|
||||
if type(data) == "table" then
|
||||
for i, v in data
|
||||
do request[i] = v
|
||||
end
|
||||
else request.url = data end
|
||||
return request
|
||||
end
|
||||
|
@ -613,18 +616,11 @@ end
|
|||
-----------------------------------------------------------------------------
|
||||
function Public.request(request)
|
||||
local response = {}
|
||||
if request.body then
|
||||
request.body_cb = function()
|
||||
return request.body, string.len(request.body)
|
||||
end
|
||||
end
|
||||
local cat = socket.concat.create()
|
||||
response.body_cb = function(chunk, err)
|
||||
if chunk then cat:addstring(chunk) end
|
||||
return 1
|
||||
end
|
||||
request.body_cb = socket.callback.send_string(request.body)
|
||||
local concat = socket.concat.create()
|
||||
response.body_cb = socket.callback.receive_concat(concat)
|
||||
response = Public.request_cb(request, response)
|
||||
response.body = cat:getresult()
|
||||
response.body = concat:getresult()
|
||||
response.body_cb = nil
|
||||
return response
|
||||
end
|
||||
|
|
|
@ -56,6 +56,7 @@ LUASOCKET_API int luaopen_socket(lua_State *L)
|
|||
#include "concat.lch"
|
||||
#include "code.lch"
|
||||
#include "url.lch"
|
||||
#include "callback.lch"
|
||||
#include "smtp.lch"
|
||||
#include "ftp.lch"
|
||||
#include "http.lch"
|
||||
|
@ -64,6 +65,7 @@ LUASOCKET_API int luaopen_socket(lua_State *L)
|
|||
lua_dofile(L, "concat.lua");
|
||||
lua_dofile(L, "code.lua");
|
||||
lua_dofile(L, "url.lua");
|
||||
lua_dofile(L, "callback.lua");
|
||||
lua_dofile(L, "smtp.lua");
|
||||
lua_dofile(L, "ftp.lua");
|
||||
lua_dofile(L, "http.lua");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue