0

I'm trying to do a simple GET request using the winhttp library using luajit and seem to be stuck. I am creating the request, but when I try to receive the response I get nothing. The code I have can be seen below

local ffi = require("ffi")
local log = require("log")
local winhttp = ffi.load("winhttp")

ffi.cdef[[
 typedef void * HINTERNET;
 typedef unsigned int INTERNET_PORT;

 typedef const wchar_t * LPCWSTR;
 typedef unsigned long DWORD;
 typedef unsigned long DWORD_PTR;
 typedef DWORD * LPDWORD;
 typedef void * LPVOID;
 typedef int BOOL;

 static const int INTERNET_DEFAULT_PORT          = 0;
 static const int INTERNET_DEFAULT_HTTP_PORT     = 80;
 static const int INTERNET_DEFAULT_HTTPS_PORT    = 443;

 static const int WINHTTP_FLAG_SECURE            = 0x00800000;

 HINTERNET WinHttpOpen(LPCWSTR pwszUserAgent, DWORD dwAccessType, LPCWSTR pwszProxyName, LPCWSTR pwszProxyBypass, DWORD dwFlags);
 HINTERNET WinHttpConnect(HINTERNET hSession, LPCWSTR pswzServerName, INTERNET_PORT nServerPort, DWORD dwReserved);
 HINTERNET WinHttpOpenRequest(HINTERNET hConnect, LPCWSTR pwszVerb, LPCWSTR pwszObjectName, LPCWSTR pwszVersion, LPCWSTR pwszReferrer, LPCWSTR *ppwszAcceptTypes, DWORD dwFlags);
 BOOL WinHttpSendRequest(HINTERNET hRequest, LPCWSTR pwszHeaders, DWORD dwHeadersLength, LPVOID lpOptional, DWORD dwOptionalLength, DWORD dwTotalLength, DWORD_PTR dwContext);
 BOOL WinHttpReceiveResponse(HINTERNET hRequest, LPVOID lpReserved);
 BOOL WinHttpQueryDataAvailable(HINTERNET hRequest, LPDWORD lpdwNumberOfBytesAvailable);
 DWORD GetLastError(void);
]]

return {
 get = function(url)
  local session = winhttp.WinHttpOpen(L("Example/1.0"), 1, nil, nil, 0)
  log.debug(session)
  if not session then
    log.error("Could not create session ", ffi.C.GetLastError())
    return
  end
  --www.google.com
  local connect = winhttp.WinHttpConnect(session, L("www.google.com"), winhttp.INTERNET_DEFAULT_PORT, 0)
  log.debug(connect)
  if not connect then
    log.error("Could not connect ", ffi.C.GetLastError())
    return
  end
  local request = winhttp.WinHttpOpenRequest(connect, L("GET"), nil, nil, nil, nil, winhttp.WINHTTP_FLAG_SECURE)
  log.debug(request)
  if not request then
    log.error("Could not create request ", ffi.C.GetLastError())
    return
  end
  local response = winhttp.WinHttpReceiveResponse(request, nil)
  log.debug(response)
  if not tonumber(response) then
    log.error("Could not receive response ", ffi.C.GetLastError())
    return
  end
  local size = ffi.new("LPDWORD")
  winhttp.WinHttpQueryDataAvailable(request, size)
  log.debug(size)
  if not tonumber(size) or not tonumber(size[0]) then
    log.error("No data available ", ffi.C.GetLastError())
    return
  end
 end
}

A sample log output I get when running is

[DEBUG Fri Jun  2 09:10:42 2017] lua\libs\http.lua:34: cdata: 0x005d16c8
[DEBUG Fri Jun  2 09:10:42 2017] lua\libs\http.lua:41: cdata: 0x0077ec60
[DEBUG Fri Jun  2 09:10:42 2017] lua\libs\http.lua:47: cdata: 0x005a2328
[DEBUG Fri Jun  2 09:10:42 2017] lua\libs\http.lua:53: -0
[DEBUG Fri Jun  2 09:10:42 2017] lua\libs\http.lua:60: cdata: NULL
[ERROR Fri Jun  2 09:10:42 2017] lua\libs\http.lua:62: No data available  183
Danny
  • 7,368
  • 8
  • 46
  • 70
  • google uses redirect. May be need turn on it some where – moteus Jun 02 '17 at 13:31
  • tried a bunch of urls (reddit, stackoverflow, bing) all with the same results. Tho, I just logged `GetLastError()` after `WinHttpReceiveResponse` and got `ERROR_WINHTTP_INCORRECT_HANDLE_STATE`. So I guess that atleast is giving me something to go off of now. – Danny Jun 02 '17 at 13:37

1 Answers1

0

I overlooked one of the most important functions. I wasn't calling WinHttpSendRequest

Danny
  • 7,368
  • 8
  • 46
  • 70