--[[
---------------------------------------------------------------------------
Remote Train Control Program for Windows

© Copyright 2019 by Mark DiVecchio

	This file is part of Remote Train Control.

	Remote Train Control is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Remote Train Control is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Remote Train Control.  If not, see <http://www.gnu.org/licenses/>.

 Home Page : http://www.silogic.com/trains/RTC_Running.html
---------------------------------------------------------------------------]]
-- Semicolons are not required in Lua code
--
title = "Internet Test"
--
-- this script requires luasocket 2.0.2 or 3.0-rc1 as
-- modified for RTC by Mark DiVecchio
-- 26 May 2019
--
-- two folders should be present in the folder
-- that contains Remote_Control.exe - 'socket' and 'mime'
--
--
--[[
---------------------------------------------------------------------------
-- Prints the following
HTTP/1.1 200 OK
Server: openresty/1.19.9.1
Date: Fri, 09 Sep 2022 03:10:16 GMT
Content-Type: text/plain
Content-Length: 773
Connection: close
Last-Modified: Mon, 27 May 2019 15:43:59 GMT
ETag: "305-589e06aadadc0"
X-Webcom-Cache-Status: BYPASS
Accept-Ranges: bytes

 Remote Train Control Program for Windows

(c) Copyright 2019 by Mark DiVecchio

    Remote Train Control is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Remote Train Control is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Remote Train Control.  If not, see <http://
www.gnu.org/licenses/>.

Done
---------------------------------------------------------------------------
]]

local socket = require ("socket")
print("socket loaded");

function setup()
if (Debug() > 0) then
	print(package.path)
	print(package.cpath)
	end;
-- https://luapower.com/socket/download
-- https://github.com/luapower/socket
print("socket version : " .. socket._VERSION);
host = "www.silogic.com"
file = "/trains/RTC/copyright.txt"
--
-- should download and print out the copyright.txt file contents
--
c = assert(socket.connect(host,80));
-- gets 400 Bad Request when invoking HTTP/1.0 so use HTTP/1.1
--local request = string.format("GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", file, host);
--
-- works:
-- see: https://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=11756
local request = "GET "..file.." HTTP/1.1\r\nUser-agent: "..socket._VERSION.."\r\nHost: "..host.."\r\nConnection: close\r\n\r\n";
print("Sending request : " .. request);
c:send(request);

repeat
	local s, status, partial = c:receive(2^10)
	Yield();
	print(s or partial);
until status == "closed";

c:close();
print("Done");
return Stop();
end
