[Lua] Winhttprequest Wrapper
It's an HTTP wrapper for the winhttprequest COM object in Windows. It's pretty basic and somewhat messy, but meh, wrote it a while ago as my introductory project in Lua.
Aside from being crudely written it's pretty convenient if you're looking for something native to Windows rather than Lua's standard HTTP implementation (luasocket) or another HTTP API like cURL.
Here it is.
PHP Code:
--------------------------------------------------------------
-- @Description: winhttprequest COM wrapper & accompanied supplemental functions
-- @Author: Forget Me Not
-- @Contact: xxprotozoid@live.com
-- @Version: v0.1
-- @Remarks:
--- [1] Primarily for Windows use only.
--- [2] Requires the luacom library; externally requires the winhttprequest.winhttprequest.5.1 COM
--- object to be present on the machine of use.
--- [3] camelCase convention is probably bad taste. But whatever. Deal with it.
-- @License: GNU General Public License - http://www.gnu.org/copyleft/gpl.html
--------------------------------------------------------------
require("luacom")
local WINHTTP = luacom.CreateObject("winhttp.winhttprequest.5.1")
local USER_AGENT = [[Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12)]]
local lastPage
function httpGet(httpObj, webPage, cookie, keepAlive)
if not httpObj then httpObj = WINHTTP end
if webPage == "" then return end
httpObj:Open("GET", webPage, 0)
httpObj:SetRequestHeader("User-Agent", USER_AGENT)
if keepAlive ~= nil then httpObj:SetRequestHeader("Connection", "keep-alive") end
lastPage = webPage
httpObj:SetRequestHeader("Referrer", lastPage)
if cookie ~= nil then httpObj:SetRequestHeader("Cookie", cookie) end
httpObj:Send()
return httpObj.ResponseText
end
function httpPost(httpObj, webPage, paramTable, cookie, keepAlive)
if not httpObj then httpObj = WINHTTP end
if webPage == nil then return end
if type(paramTable) ~= "table" then return end
httpObj:Open("POST", webPage, 0)
httpObj:SetRequestHeader("User-Agent", USER_AGENT)
if keepAlive ~= nil then httpObj:SetRequestHeader("Connection", "keep-alive") end
httpObj:SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
lastPage = webPage
httpObj:SetRequestHeader("Referrer", lastPage)
if cookie ~= nil then httpObj:SetRequestHeader("Cookie", cookie) end
local buffer = ""
for v in pairs(paramTable) do buffer = buffer .. paramTable[v] end
httpObj:Send(buffer)
return httpObj.ResponseText
end
function httpReferrer(httpObj, referrer)
if not httpObj then httpObj = WINHTTP end
if referrer ~= "" and referrer ~= nil then httpObj:SetRequestHeader("Referrer", referrer) end
end
function httpRawHeaders(header)
local function split(inputStr, sep)
if sep == nil then
sep = "%s"
end
t={} ; i=1
for str in string.gmatch(inputStr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
return t
end
local headerSplit = split(header, ": ")
WINHTTP:SetRequestHeader(headerSplit[1], headerSplit[2])
end
function httpSetUserAgent(usrAgent)
WINHTTP:SetRequestHeader("User-Agent", usrAgent)
end
function string.between(s,leader, trailer)
local end_trailer = string.find(s, leader) + #leader
local start_trailer = string.find(s, trailer, end_leader)
return string.sub(s, end_trailer, start_trailer - 1)
end
Here it is on github: https://gist.github.com/3626029
If you have any questions/comments, feel free to post. And I know there isn't a lot of Lua programmers here, but I saw it laying around and thought I might as well put it out there for learning purposes.
Enjoy.
Edit: I may post a refactored version that's improved and cleaned up if and when I have the spare time.