Author Topic: Squirrel functions for encoding GET/POST data  (Read 1461 times)

Rhytz

  • Newbie
  • *
  • Posts: 30
  • Karma: +13/-7
    • View Profile
    • Rhytz's Website
Squirrel functions for encoding GET/POST data
« on: November 16, 2016, 12:09:19 am »
My PHP/Web SDK allows you do query your server from your website. But querying your website from your LU server in real time was not yet possible. Until now!

I've written some new functions that allow you to encode Squirrel strings and tables into "GET/POST" data which your webserver can understand.

postdata_encode( table ) - Converts a Squirrel table or array to a POST-able string
urlencode( string ) - Percent-encodes reserved URI characters in given string, and returns encoded string

Sample code:

Code: [Select]
function onScriptLoad(){



dofile( "Scripts/Rhytz/phpsdk/encode_functions.nut" );

SendSocket <- NewSocket( "ReceiveData" );

SendSocket.Connect("192.168.2.243", 2037 );

//SetLostConnFunc doesn't seem to work properly now, if SetNewConnFunc is set as well. Switching them around makes only SetLostConnFunc work and vice versa.
//SendSocket.SetLostConnFunc( "Failure" );
SendSocket.SetNewConnFunc( "Connected" );

}

function Connected( socket ){
local ServerData = {
GamemodeName = GetGamemodeName(),
MapName = GetMapName(),
ServerName = GetServerName(),
MaxPlayers = GetMaxPlayers(),
Players = GetPlayers(),
Password = GetPassword(),
Port = GetPort(),
AnArray = ["test1", "test2", "test3", ["arraywithinarray1","arraywithinarray2","arraywithinarray3"]],
MTUSize = GetMTUSize(),
falsevalue = false,
nullvalue = null
}
local data = postdata_encode(ServerData);
local path = "/rhytz/lu2.class.php";

    SendSocket.Send("POST " + path + " HTTP/1.0\r\n");
    SendSocket.Send("Content-Length: " + data.len() + "\r\n");
SendSocket.Send("Content-Type: application/x-www-form-urlencoded\r\n");
SendSocket.Send("\r\n");
SendSocket.Send(data);
}

function ReceiveData( socket, data ){
print(data); //Handle the returned webserver data here
}

The scripting functions:

Code: [Select]
/*
PHP SDK server for Liberty Unleashed, by Rhytz
encode_functions.nut - Contains custom URL encoding functions
(c) 2016
*/

/*
postdata_encode( table )

Converts a Squirrel table or array to POST-able data
*/
function postdata_encode( table ) {
if(typeof(table) == "table" || typeof(table)  == "array"){
postdata <-  "";
keyname <- "";
postdata_recurse( table );

return postdata;
}else{
return false;
}
}

/*
postdata_recurse( table )

Recursive function that loops through the table/array and encodes it.
Complementary function to postdata_encode().
*/
function postdata_recurse( table, depth = 0 ) {
local len = table.len();
local i = 1;
local typ = typeof(table);
//Loop through all keys in the supplied table
foreach (key, value in table) {
//Switch datatype of 'value' for appropriate handling
switch (typeof(value)){
//If the datatype is another array or table, run the function again
case "array":
case "table":
depth == 0 ? keyname  = key : keyname += "[" + key + "]";
postdata_recurse( value , depth + 1);
break;

//Handling of other datatypes
case "bool":
local boolvalue = value ? "true" : "false";
depth == 0 ? postdata += key + "=" + boolvalue : postdata += keyname + "[" + key + "]=" + boolvalue;
break;

case "integer":
case "float":
depth == 0 ? postdata += key + "=" + value : postdata += keyname + "[" + key + "]=" + value;
break;

case "null":
depth == 0 ? postdata += key + "=0"  : postdata += keyname + "[" + key + "]=0";
break;

case "string":
depth == 0 ? postdata += key + "=" + urlencode(value) : postdata += keyname + "[" + key + "]=" + urlencode(value);
break;

//Return false if the datatype is unsupported
default:
depth == 0 ? postdata += key + "=false" : postdata += keyname + "[" + key + "]=false";
break;
}

//Append a ampersand at the end if this isnt the last item in the table
if(i < len){
postdata += "&";
}

i++;
}
}


/*
urlencode( string )

Percent-encodes reserved URI characters in given string, and returns encoded string
*/
function urlencode( string ) {
local out = "";
for(local i=0; i<string.len(); i++){
local c = string.slice(i, i+1);
switch(c){
case " ":
out += "%20";
break;
case "!":
out += "%21";
break;
case "#":
out += "%23";
break;
case "$":
out += "%24";
break;
case "&":
out += "%26";
break;
case "'":
out += "%27";
break;
case "(":
out += "%28";
break;
case ")":
out += "%29";
break;
case "*":
out += "%2A";
break;
case "+":
out += "%2B";
break;
case ",":
out += "%2C";
break;
case "/":
out += "%2F";
break;
case ":":
out += "%3A";
break;
case ";":
out += "%3B";
break;
case "=":
out += "%3D";
break;
case "?":
out += "%3F";
break;
case "@":
out += "%40";
break;
case "[":
out += "%5B";
break;
case "]":
out += "%5D";
break;
default:
out += c;
break;
}
}
return out;
}

You can now access the $_POST parameter in your PHP script and handle the data.
« Last Edit: November 16, 2016, 12:11:34 am by Rhytz »
Meet everyone on the Liberty Unleashed Discord || Check out my website/portal!

Rhytz

  • Newbie
  • *
  • Posts: 30
  • Karma: +13/-7
    • View Profile
    • Rhytz's Website
Re: Squirrel functions for encoding GET/POST data
« Reply #1 on: November 16, 2016, 12:35:16 am »
To separate the HTTP header from the data returned by your webserver, you can do something like this:

Code: [Select]

function ReceiveData( socket, data ){
local newline = data.find("\r\n\r\n");
local header = data.slice(0, newline);
local content = data.slice(newline + 4, data.len());
print(content);
}
Meet everyone on the Liberty Unleashed Discord || Check out my website/portal!

Vortrex

  • Full Member
  • ***
  • Posts: 267
  • Karma: +54/-73
    • View Profile
Re: Squirrel functions for encoding GET/POST data
« Reply #2 on: November 16, 2016, 08:45:22 pm »
I haven't tested this yet, but it looks awesome.

Well done.

 

© Liberty Unleashed Team.