From a712c4d8116371747ed3fda07eddc8e204fb626e Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Wed, 4 Mar 2015 11:04:56 +0100 Subject: [PATCH] expose parseRequest() methods for ftp and http requests --- src/ftp.lua | 47 ++++++++++++++++++++++++++++------------------- src/http.lua | 21 ++++++++++++++------- 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/src/ftp.lua b/src/ftp.lua index 917cd89..6286f90 100644 --- a/src/ftp.lua +++ b/src/ftp.lua @@ -232,17 +232,6 @@ local function parse(u) return t end -local function sput(u, body) - local putt = parse(u) - putt.source = ltn12.source.string(body) - return tput(putt) -end - -_M.put = socket.protect(function(putt, body) - if base.type(putt) == "string" then return sput(putt, body) - else return tput(putt) end -end) - local function tget(gett) gett = override(gett) socket.try(gett.host, "missing hostname") @@ -256,12 +245,17 @@ local function tget(gett) return f:close() end -local function sget(u) - local gett = parse(u) - local t = {} - gett.sink = ltn12.sink.table(t) - tget(gett) - return table.concat(t) +-- parses a simple form into the advanced form +-- if `body` is provided, a PUT, otherwise a GET. +-- If GET, then a field `target` is added to store the results +_M.parseRequest = function(u, body) + local t = parse(u) + if body then + t.source = ltn12.source.string(body) + else + t.target = {} + t.sink = ltn12.sink.table(t.target) + end end _M.command = socket.protect(function(cmdt) @@ -277,9 +271,24 @@ _M.command = socket.protect(function(cmdt) return f:close() end) +_M.put = socket.protect(function(putt, body) + if base.type(putt) == "string" then + putt = _M.parseRequest(putt, body) + tput(putt) + return table.concat(putt.target) + else + return tput(putt) + end +end) + _M.get = socket.protect(function(gett) - if base.type(gett) == "string" then return sget(gett) - else return tget(gett) end + if base.type(gett) == "string" then + gett = _M.parseRequest(gett) + tget(gett) + return table.concat(gett.target) + else + return tget(gett) + end end) return _M \ No newline at end of file diff --git a/src/http.lua b/src/http.lua index d5457f6..550634c 100644 --- a/src/http.lua +++ b/src/http.lua @@ -76,7 +76,7 @@ socket.sourcet["http-chunked"] = function(sock, headers) -- was it the last chunk? if size > 0 then -- if not, get chunk and skip terminating CRLF - local chunk, err, part = sock:receive(size) + local chunk, err = sock:receive(size) if chunk then sock:receive() end return chunk, err else @@ -329,11 +329,14 @@ end return 1, code, headers, status end -local function srequest(u, b) +-- parses a shorthand form into the advanced table form. +-- adds field `target` to the table. This will hold the return values. +_M.parseRequest = function(u, b) local t = {} local reqt = { url = u, - sink = ltn12.sink.table(t) + sink = ltn12.sink.table(t), + target = t, } if b then reqt.source = ltn12.source.string(b) @@ -343,13 +346,17 @@ local function srequest(u, b) } reqt.method = "POST" end - local code, headers, status = socket.skip(1, trequest(reqt)) - return table.concat(t), code, headers, status + return reqt end _M.request = socket.protect(function(reqt, body) - if base.type(reqt) == "string" then return srequest(reqt, body) - else return trequest(reqt) end + if base.type(reqt) == "string" then + reqt = _M.parseRequest(reqt, body) + local t, code, headers, status = reqt.target, socket.skip(1, trequest(reqt)) + return table.concat(t), code, headers, status + else + return trequest(reqt) + end end) return _M