This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
TyDoesMC024's profile picture

Need some help with printing http.get()

Started by TyDoesMC024, 02 October 2016 - 12:48 AM
TyDoesMC024 #1
Posted 02 October 2016 - 02:48 AM
Hello. I am making an API that will have all my future programs required features. So I created a PHP website to do some encryption.
http://base64.netne.net/?b64encode=TEXT TO ENCODE
http://base64.netne.net/?b64decode=ENCODED TEXT TO DECODE

API code:

--[[ Ty's API ]]--


--[[ Functions ]]--

function log(name,type,msg)
print("["..name.."] ["..type.."]: "..msg.."")
end

function encrypt(msg)
local website = "http://base64.netne.net/?b64encode="
local webhandle = http.get(website.. msg)
if webhandle == false then
print("Error could not connect to webservices")
else
local results = webhandle.readAll()..
print(results)
webhandle.close()
end
end

I made another program to test my API it only ran os.loadAPI() and TysAPI.encrypt("Test")
It doesn't give me an error but prints nothing and forces a reboot or terminate.
Bomb Bloke #2
Posted 02 October 2016 - 05:52 AM
I'd guess that if you stuck some more print statements in there (to better track the flow of your script), you'd find the point where it halts is where you call http.get() - presumably it's waiting for a response and, for whatever reason, isn't getting one. This may or may not be related to the user agent.

Bear in mind that if http.get() decides to fail, it'll return nil, not false.

You could create a version of http.get() which only "waits" a certain amount of time like this:

Spoiler
function http.timedGet(url, headers, time)
	http.request(url, headers)
	
	if type(time) == "number" then time = os.startTimer(time) end
	
	while true do
		local event, par1, par2 = os.pullEvent()
		
		if event == "http_success" and par1 == url then
			return par2
		
		elseif (event == "http_failure" and par == url) or (event == "timer" and par == time) then
			return nil
			
		end
	end
end

Eg, http.timedGet("http://base64.netne.net/?b64encode=hello", nil, 5) will return within five seconds, for better or worse.

It may or may not help if you parse "msg" through textutils.urlEncode().

Personally I use Package for handling base64, though there are also other APIs that've been posted on these boards for similar purposes. The conversions can be handled easily within Lua, so relying on an external server to handle them seems like more trouble than it's worth?

Just in regards to your semantics, base64 encoding is not a form of encryption.
Edited on 02 October 2016 - 03:54 AM
TyDoesMC024 #3
Posted 03 October 2016 - 04:05 PM
Ok, thank you. I think it was Mimic (Online EMU)s problem. and as for Base64. I know its very easy to crack but I plan on making a SHA-512 hashing PHP script along side my Base64. The Base64 will encode the text and it will use the results (base64 encoded text) as a salt.
TyDoesMC024 #4
Posted 03 October 2016 - 04:31 PM
I have my API working. I was needing a little more help though. How would I be able to save the results to a variable in the api.

New code:
--[[ API ]]--
--[[ Variables ]]--
lastEncode = ""
lastDecode = ""
--[[ Functions ]]--

function log(name, type, text)
  print("["..name.."] ["..type.."]: "..text.."")
end
function encrypt(emsg)
  ewebsite = "http://base64.netne.net/?b64encode="
  ewebhandle = http.get(ewebsite..emsg)
  lastEncode = ewebhandle.readLine()
end
function decrypt(dmsg)
  dwebsite = "http://base64.netne.net/?b64decode="
  dwebhandle = http.get(dwebsite..dmsg) 
  print(dwebhandle.readLine())
  dwebhandle.close()
end
KingofGamesYami #5
Posted 03 October 2016 - 04:55 PM
I can confirm that Mimic has problems with HTTP.
Anavrins #6
Posted 03 October 2016 - 11:26 PM
Ok, thank you. I think it was Mimic (Online EMU)s problem. and as for Base64. I know its very easy to crack but I plan on making a SHA-512 hashing PHP script along side my Base64. The Base64 will encode the text and it will use the results (base64 encoded text) as a salt.
What do you mean it's easy to crack…, base64 isn't even an encryption function.
Base64 encodes raw bitstream into printable ascii character, used when you want to store binary data on a printable character-only medium.
A completely reversible encoding function that doesn't need any secret key.
Also, base64 is an extremely simple function to implement in Lua, I don't see why you would need to use http api for that…
If you want reference for the code, you can check out my implementation, http://pastebin.com/TEtna4tX

I can confirm that Mimic has problems with HTTP.
Mimic has a lot of problem, mainly being outdated and un-maintained, as well as a lot of accuracy issues.
Edited on 03 October 2016 - 09:34 PM
Bomb Bloke #7
Posted 04 October 2016 - 04:14 AM
It'd be nice if Mimic were updated. It's still pretty useful, but on a user-by-user basis that usefulness drops dramatically if one doesn't know which issues have to be accounted for.

Long story short, if you get any error you can't figure out when using any emulator, it's best to confirm you get the same problem in vanilla ComputerCraft before posting about it.

I have my API working. I was needing a little more help though. How would I be able to save the results to a variable in the api.

Generally, having an API store data for any longer than it takes to complete a given function call is discouraged, and it's not obvious why you'd want your API to save things here.

Instead, it sounds like you're looking for the "return" keyword. Read up on it here; basically, though, you might do something like this:

function decrypt(dmsg)
  local dwebhandle = http.get("http://base64.netne.net/?b64decode="..dmsg) 
  local results = dwebhandle.readLine()
  dwebhandle.close()
  return results
end

.
.
.

local myDecodedMessage = decrypt("someEncodedText")
TyDoesMC024 #8
Posted 04 October 2016 - 06:01 AM
So I have an issue with my other API I am working on,
I am trying to use shell.run() using a function arg function(arg)
I am trying to get pastebin but the address (fmNvnAzu) is the arg from the function. It gives me expected symbol ')'
when I use os.run it says expected a table but recieved a string.
Bomb Bloke #9
Posted 04 October 2016 - 06:58 AM
The code you've provided won't give an "expected symbol" error, and nor does it call os.run(). This makes it rather difficult to comment on what your problem might be.

When you get an error, the name of the script file involved will be included, along with a number pointing to the exact line where execution failed. I rather suspect your problem lies in whatever external file it is you're attempting to call.