-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
William Adams
committed
Jul 25, 2013
1 parent
9d3d9c1
commit a298486
Showing
5 changed files
with
114 additions
and
18 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |