Skip to content

Commit

Permalink
modified: ../src/net/HttpServer.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
William Adams committed Jul 25, 2013
1 parent 9d3d9c1 commit a298486
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 18 deletions.
Binary file modified src/bin/tinn.exe
Binary file not shown.
11 changes: 11 additions & 0 deletions src/core/FileStream.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ local stream = require "stream"


local FileStream = {}
setmetatable(FileStream, {
__call = function(self, ...)
return FileStream.new(...);
end,
});

local FileStream_mt = {
__index = FileStream,
}
Expand Down Expand Up @@ -148,4 +154,9 @@ function FileStream:WriteLine(line)
return status, err
end

FileStream.writeString = FileStream.WriteString;
FileStream.writeLine = FileStream.WriteLine;
FileStream.writeByte = FileStream.WriteByte;
FileStream.writeBytes = FileStream.WriteBytes;

return FileStream;
13 changes: 8 additions & 5 deletions src/net/HttpServer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ end
--[[
Instance Methods
--]]
HttpServer.HandleRequestFinished = function(self, request)
-- Once we're done with this single request
-- send it back aroud again in case there is
-- more to be processed.
local sock = request.DataStream.Socket:getNativeSocket();
self:OnAccept(sock);
end

HttpServer.HandlePreamblePending = function(self, sock)
local socket = IOCPSocket:init(sock);
local stream, err = IOCPNetStream:init(socket);
Expand All @@ -49,11 +57,6 @@ HttpServer.HandlePreamblePending = function(self, sock)
request.Url = URL.parse(request.Resource);
local response = WebResponse:OpenResponse(request.DataStream)
self.OnRequest(self.OnRequestParam, request, response);

-- Once we're done with this single request
-- send it back aroud again in case there is
-- more to be processed.
self:OnAccept(sock);
else
print("HandleSingleRequest, Dump stream: ", err)
socket:closeDown();
Expand Down
26 changes: 13 additions & 13 deletions src/win32/apiset/ws2_32.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1332,34 +1332,34 @@ WSAResetEvent = Lib.WSAResetEvent,
WSASend = Lib.WSASend,

--[[
WSASendDisconnect
WSASendMsg
WSASendTo
WSASetBlockingHook
WSASendDisconnect = Lib.
WSASendMsg = Lib.
WSASendTo = Lib.
WSASetBlockingHook = Lib.
--]]

WSASetEvent = Lib.WSASetEvent,

--[[
WSASetLastError
WSASetServiceA
WSASetServiceW
WSASetLastError = Lib.WSASetLastError,
WSASetServiceA = Lib.WSASetServiceA,
WSASetServiceW = Lib.WSASetServiceW,
--]]

WSASocketA = Lib.WSASocketA,

--[[
WSASocketW
WSASocketW = Lib.WSASocketW,
--]]

WSAStartup = Lib.WSAStartup,

--[[
WSAStringToAddressA
WSAStringToAddressW
WSAUnadvertiseProvider
WSAUnhookBlockingHook
WSAWaitForMultipleEvents
WSAStringToAddressA = Lib.
WSAStringToAddressW = Lib.
WSAUnadvertiseProvider = Lib.
WSAUnhookBlockingHook = Lib.
WSAWaitForMultipleEvents = Lib.
--]]

--[[
Expand Down
82 changes: 82 additions & 0 deletions tests/comp_rest.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@

local restops = require("restops");

local Collections = require ("Collections");

local FileStream = require ("FileStream");
local NetStream = require ("NetStream");
local SocketPool = require("SocketPool");

local URL = require ("url");
local HttpRequest = require ("HttpRequest");
local HttpResponse = require("HttpResponse");
local chunkiter = require ("HttpChunkIterator");

local sout = FileStream.new(io.stdout)


local GET = function(resource, showheaders, onfinish)
--print("http_get: ", resource, showheaders, onfinish)
if not resource then
return onfinish(nil, "no resource specified");
end

local urlparts = URL.parse(resource, {port="80", path="/", scheme="http"});
local hostname = urlparts.host
local path = urlparts.path
local port = urlparts.port

-- Open up a stream to get the real content
local resourcestream, err = NetStream.Open(hostname, port);

if not resourcestream then
return onfinish(nil, err)
end

-- Send a proper http request to the service
if port and port ~= "80" then
hostname = hostname..':'..port
end

local headers = {
["Host"] = hostname,
["Connection"] = "close",
}
if urlparts.query then
path = path..'?'..urlparts.query
end

local request = HttpRequest.new("GET", path, headers, nil);
local success, err = request:Send(resourcestream);

if not success then
return onfinish(false, err)
end

local response, err = HttpResponse.Parse(resourcestream);

if response then
-- successful read of preamble
-- so print it out, and keep out of queue
if showheaders then
response:WritePreamble(sout);
end

for chunk, err in chunkiter.ReadChunks(response) do
-- do nothing
-- but read the chunks
io.write(chunk);
end
result = "OK";
else
result = err;
end

return onfinish(result);
end

OnMessage = function(msg)
if msg.Message == restops.GET then

end
end

0 comments on commit a298486

Please sign in to comment.