Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Lua ➜ LuaSocket downloading Https?

LuaSocket downloading Https?

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Rivius   (99 posts)  Bio
Date Tue 18 Dec 2018 01:35 AM (UTC)
Message
So I have a function that currently downloads .json files from a MUD's associated API. It's worked for years for me and looks like this


function download_json(host, file)
	local c = assert(socket.connect(host, 80))
	c:send("GET "..file.." HTTP/1.1\r\nUser-agent: "..socket._VERSION.."\r\nHost: "..host.."\r\nConnection: close\r\n\r\n")
	
	local js = {}
	while true do
		local s, stat, p = receive(c)
		if not s and p then
			if (#p > 0) then
				s = p
			end
		end
		js[#js + 1] = s
		if (stat == "closed") then
			break
		end
	end
	c:close() -- close the connection
	return string.match(table.concat(js, "\n"), "(%{.+%})")
end


It seems like now, whenever I try to download the file using this function, I get the error:



1="HTTP/1.1 301 Moved Permanently"
2="Date: Tue, 18 Dec 2018 01:17:35 GMT"
3="Server: Apache/2.4.10 (Debian)"
4="Location: https://api.lusternia.com/characters/rivius.json"
5="Cache-Control: max-age=0"
6="Expires: Tue, 18 Dec 2018 01:17:35 GMT"
7="Content-Length: 339"
8="Connection: close"
9="Content-Type: text/html; charset=iso-8859-1"
10=""
11="<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">"
12="<html><head>"
13="<title>301 Moved Permanently</title>"
14="</head><body>"
15="<h1>Moved Permanently</h1>"
16="<p>The document has moved <a href="https://api.lusternia.com/characters/rivius.json">here</a>.</p>"
17="<hr>"
18="<address>Apache/2.4.10 (Debian) Server at api.lusternia.com Port 80</address>"
19="</body></html>"

I have poor knowledge of these things, but I suspect there was a silent change forcing https, which I'm guessing Luasocket doesn't support. Is there a relatively painless way of overcoming this to get the function working again?
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #1 on Tue 18 Dec 2018 06:46 AM (UTC)
Message
Does this help?

http://www.gammon.com.au/forum/?id=14093

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Rivius   (99 posts)  Bio
Date Reply #2 on Tue 18 Dec 2018 10:18 AM (UTC)

Amended on Tue 18 Dec 2018 11:49 AM (UTC) by Rivius

Message
Thank you. That does help a bit, though a lot more complicated than I thought. The issue is portability there though. It sounds like I'd need to install a number of files in the mushclient directory itself which makes it tricky to just distribute a 'system' that you simply have to unzip. I don't suppose there's an easier workaround to simply getting a .json file off the web?
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #3 on Tue 18 Dec 2018 07:48 PM (UTC)
Message
You can get a JSON file from the web, that isn't the problem. The problem is that they appear to be forcing you into HTTPS mode with a redirect.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Rivius   (99 posts)  Bio
Date Reply #4 on Tue 18 Dec 2018 09:39 PM (UTC)
Message
Hrm. What are your thoughts of including luasec or otherwise into the Mush installer for future versions? I imagine more sites will move to https in time.
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #5 on Tue 18 Dec 2018 11:11 PM (UTC)

Amended on Tue 18 Dec 2018 11:16 PM (UTC) by Fiendish

Message
Honestly, I'm not sure that MUSHclient should even include luasocket. That alone is already out of scope.

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Rivius   (99 posts)  Bio
Date Reply #6 on Wed 19 Dec 2018 02:23 AM (UTC)

Amended on Wed 19 Dec 2018 02:24 AM (UTC) by Rivius

Message
Fair enough.

I decided to go full frankenstein and did this instead.


	local web_address = "https://api.lusternia.com/"..webobj
	local time_start = GetInfo(304)
	local job_name = string.match(webobj, "([a-z]+)%.json")
	local file_name = "_"..job_name..".json"
	local bitsadmin = "/C bitsadmin /transfer "..job_name.." /download /priority normal "..web_address.." "..GetInfo(67)..file_name
	
	utils.shellexecute("cmd", bitsadmin, nil, "open", 0)
	while (GetInfo(304) - time_start < 10) do
		local local_file = io.open(file_name, "r")
		if local_file then
			local json_string = local_file:read("*all")
			local_file:close()
			os.remove(file_name)
			return string.match(json_string, "(%{.+%})")
		end
		coroutine.yield()
	end


A little slower, only works on windows, but it works.
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #7 on Wed 19 Dec 2018 11:12 PM (UTC)

Amended on Wed 19 Dec 2018 11:13 PM (UTC) by Fiendish

Message
Another alternative would be to include a copy of wget.exe and use that. It still means distributing a dependency, but at least then you wouldn't have a bunch of fragments scattered around like it takes to add https to luasocket.

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #8 on Sat 22 Dec 2018 04:48 AM (UTC)
Message
Fiendish said:

Honestly, I'm not sure that MUSHclient should even include luasocket. That alone is already out of scope.


Yes, I try to not make the client too bloated. A while ago someone wanted me to add in FTP client functionality to "make it easier to download files from the MUD", something which I declined to do. It's just another thing to support, keep updating, troubleshoot, etc. when it is not really part of the core functionality.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


21,617 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.