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.
Here it is on github: https://gist.github.com/3626029PHP 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
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.
Results 1 to 3 of 3
Thread: [Lua] Winhttprequest wrapper
- 04 Sep. 2012 08:25pm #1
[Lua] Winhttprequest Wrapper
Last edited by The Unintelligible; 04 Sep. 2012 at 10:38pm.
- 05 Sep. 2012 09:14am #2
Never used Lua, but your post and get methods use a lot of the same code. That's bad design, you shouldn't repeat yourself. Tsk tsk.
Either way, good work, thanks for the release and all that.
- 05 Sep. 2012 01:29pm #3
Yeah, didn't put too much initial thought into it. All I know is afterwards I felt ashamed for spawning this kind of code.
Design tips for refactoring pl0x. I know it can be improved, a lot (i.e. get/post methods could be condensed in a single request method. Could use modularization to spoof simple HTTP wrapper/module instantation). But this will lessen the workload for me.
You know, I noticed I just pretty much said all there is to be said in terms of revision. And no problem.