Jump to content


Photo

Http Post - Gml Script


  • Please log in to reply
8 replies to this topic

#1 smart_monkey

smart_monkey

    GMC Member

  • GMC Member
  • 91 posts

Posted 13 March 2009 - 08:56 AM

HTTP Post - Version 1.3

Both the HTTP Post and HTTP Upload File run on the same principle and both dont have full error handling and HTTP Upload doesnt recieve response back from the server.

HTTP Post recieves pure code from the server skipping the header information. If your server does respond, make it send pure GML code as the script now returns the code and then you can make it execute it.

Here is a neat little script which can post information to a webserver. Give credit to me and to 39ster if used. If you happen to improve this script or have some ideas, PM me. If you make something with it, send PM with the link to the gmc topic.

ie. For a logon script (if the server is programmed right)

I did test it and I didnt notice any errors but if anyone does tell me. I will slightly modify this script so you can upload files to a webserver instead of sending information.

/*
argument0 = website (eg. http://www.yourpage.com/files/index.php)
argument1 = the argument data you wish to supply

eg.
argument0 = http://myweb.com/processes/index.php
argument1 = user="i%20like%20eggs"; password="eggs"

Useful to supply username and password information to a webpage
*/

server = "";
file = "/"
port = 80;
i = 0;
error = 200;

if(string_pos("http://", argument0) == 1)argument0 = string_delete(argument0, 1,7)
//get file part of url
i = string_pos("/", argument0);
if(i)
{
	file = string_copy(argument0, i, string_length(argument0)-i+1);
	argument0 = string_delete(argument0, i, string_length(file));
}
//get port part
i = string_pos(":", argument0);
if(i)
{
	port = real(string_copy(argument0, 1, i-1));
	argument0 = string_delete(argument0, 1, i);
}
//get server part
server = argument0;


tcp = tcpconnect(server, port, 0);
if(!tcp)return false;
setformat(tcp, 1, chr(13) + chr(10));
clearbuffer();
writechars("POST " + file + " HTTP/1.1" + chr(13) + chr(10));
writechars("Connection: close" + chr(13) + chr(10));
writechars("Host: " + server + chr(13) + chr(10));
writechars("User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.1; .NET CLR 2.0.50727; .NET CLR 1.1.4322)"+chr(13) + chr(10));
writechars("Content-Length: " + string(string_length("Content-Disposition: form-data; "+argument1)) + chr(13) + chr(10));
writechars("Content-Type: multipart/form-data;" + chr(13) + chr(10));
writechars("Content-Disposition: form-data; "+ string(argument1) + chr(13) + chr(10));
sendmessage(tcp);
//interpret header for any errors.
endloop = false;
while(!endloop)
{
	receivemessage(tcp); //receive one line
	i = readsep(" "); //read first word
	switch(i)
	{
//check http error code
		case "HTTP/1.1":
		case "HTTP/1.0":
			error = real(readsep(" "));
			if(error != 200 && error != 301)
			{
				closesocket(tcp);
				return error;
			}
		break;
//if blank line (end of header) then exit loop
		case "":
			endloop = true
		break;
	}
}
repeat (7)
{
receivemessage(tcp);
}
gmcode = readsep("?");
closesocket(tcp);
return (gmcode)

A quick PHP page that could recieve this and write it to the screen
<?PHP
echo $_POST["user"]."<br>";
echo $_POST["password"];
?>

You would need to use this inconjunction with sessions for you to be able to see the text. I may make better documentation on how to use it but for now, what you see is what you get.

HTTP Upload Script
This is a script similar in nearly all ways to the one above except you upload files rather then sending form data.

/*
argument0 = website to upload script (eg. http://www.yourpage.com/files/index.php)
argument1 = your file name (address on computer and can be relative)

eg.
argument0 = http://myweb.com/processes/upload.php
argument1 = C:\myfiles\myfile.txt

You can upload any file with this tool. If the upload script your sending to does not accept your file,
it is likely that your file type is not accepted, to big or it needs to login.

WARNING: IF THE UPLOAD SCRIPT YOUR SENDING TO IS YOUR OWN SCRIPT, IT MAY HAVE SECURITY HOLES!
		 PLEASE FOR YOUR SAKE, DO NOT UPLOAD TO YOUR SITE UNLESS YOUR SURE YOUR CODE HAS NO
		 SECURITY FLAWS!
*/

server = "";
file = "/"
port = 80;
i = 0;
error = 200;

if(string_pos("http://", argument0) == 1)argument0 = string_delete(argument0, 1,7)
//get file part of url
i = string_pos("/", argument0);
if(i)
{
	file = string_copy(argument0, i, string_length(argument0)-i+1);
	argument0 = string_delete(argument0, i, string_length(file));
}
//get port part
i = string_pos(":", argument0);
if(i)
{
	port = real(string_copy(argument0, 1, i-1));
	argument0 = string_delete(argument0, 1, i);
}
//get server part
server = argument0;

//Opens and Reads file
compile = "";
if (file_exists(argument1))
{
ufile=file_text_open_read(argument1)
while not(file_text_eof(ufile))
	{
	compile=compile+file_text_read_string(ufile);
	}
file_text_close(ufile);
}

//Gets the file name
i = string_pos("\", argument1);
if (i)
{
while (string_pos("\", argument1)!=0)
	{
	argument1=string_delete(argument1,0,string_pos("\", argument1));
	}
}
file_name=argument1;


tcp = tcpconnect(server, port, 0);
if(!tcp)return false;
setformat(tcp, 1, chr(13) + chr(10));
clearbuffer();
writechars("POST " + file + " HTTP/1.1" + chr(13) + chr(10));
writechars("Connection: close" + chr(13) + chr(10));
writechars("Host: " + server + chr(13) + chr(10));
writechars("User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.1; .NET CLR 2.0.50727; .NET CLR 1.1.4322)"+chr(13) + chr(10));
writechars("Content-Length: " + string(98+string_length(file_name)+string_length(compile)) + chr(13) + chr(10));
writechars("Content-Type: multipart/form-data; boundary=xYzZY" + chr(13) + chr(10));
writechars("--xYzZY" + chr(13) + chr(10));
writechars('Content-Disposition: form-data; filename="'+string(file_name)+'"'+ chr(13) + chr(10));
writechars("Content-Type: text/plain"+ chr(13) + chr(10));
writechars(string(compile) + chr(13) + chr(10));
writechars("--xYzZY--" + chr(13) + chr(10));
sendmessage(tcp);
//interpret header for any errors.
endloop = false;
while(!endloop)
{
	receivemessage(tcp); //receive one line
	i = readsep(" "); //read first word
	switch(i)
	{
//check http error code
		case "HTTP/1.1":
		case "HTTP/1.0":
			error = real(readsep(" "));
			if(error != 200 && error != 301)
			{
				closesocket(tcp);
				return error;
			}
		break;
//if blank line (end of header) then exit loop
		case "":
			endloop = true
		break;
	}
}
closesocket(tcp);

A code that can be used for the php server upload script.
<?php
$blacklist = array(".php", ".phtml", ".php3", ".php4");
foreach ($blacklist as $item) {
if(preg_match("/$item\$/i", $_FILES['userfile']['name'])) {
echo "We do not allow uploading PHP files\n";
exit;
}
}
$uploaddir = 'uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "File uploading failed.\n";
}
?>

Edited by smart_monkey, 28 July 2009 - 08:59 AM.

  • 1

#2 Southman

Southman

    I simply am not here

  • GMC Member
  • 1662 posts
  • Version:GM:Studio

Posted 14 March 2009 - 01:31 AM

this is perfect, just what i needed, nice
  • 0

#3 smart_monkey

smart_monkey

    GMC Member

  • GMC Member
  • 91 posts

Posted 14 March 2009 - 07:43 AM

No problem. I did fix up my code a bit so it should work better now. Check the first post to see the new version
  • 0

#4 score_under

score_under

    Least kawaii

  • GMC Member
  • 1321 posts

Posted 14 March 2009 - 05:27 PM

So this sends a POST request yet doesn't receive the output?

The output being critical to error handling, to user/pass validation, and to giving details about the data (e.g. post character name and spit out level and stats).
  • 0

Anti-Decompiler for GM6.1 to GM8.1.91! :GM8_new: [Main skin by Sindarin]
Discontinued.

decimal2.png
^ Signature image because it's been sorta empty since the old host died

If you need to contact me, I still get notification emails from PMs.


#5 smart_monkey

smart_monkey

    GMC Member

  • GMC Member
  • 91 posts

Posted 15 March 2009 - 06:55 AM

I am working on it to get the results from the server. My aim of being able to post was to write a script that can send files to servers which I have added below the other in the first post.

I understand what you mean though, it is kind of stupid for it to send information without knowing if it worked and what the reply would be.

Just one thing I did notice, for some reason, the game maker game running this script disappears like it was closed. This is not the case, it IS STILL RUNNING but hides itself. It reappears after it posts the information. A rough guess to how long the window disappears for is about 30seconds to 2minutes.
  • 0

#6 Krisando

Krisando

    GMC Member

  • New Member
  • 1351 posts

Posted 17 March 2009 - 08:02 PM

COol thing, the reason why I would refuse to place this on my site is because people would post god knows what.
  • 0

Posted Image


#7 halo shg

halo shg

    Programmer

  • GMC Member
  • 600 posts
  • Version:GM:Studio

Posted 17 March 2009 - 11:08 PM

Aww.. I was hoping it was 100% GML... Oh well, good script anyways.
  • 0

Dev: MacBook Pro Retina Display 15" w/ Win7 (Parallels) running GM Studio: Master and GM8 (GMS: ANDROID JAVA EXTENSIONS ARE HERE!)
Devices: Samsung Galaxy Note II / Samsung Galaxy S III / iPhone 4S / iPad 2 / iPod Touch 5th gen



Politics' is made up of two words, 'poli,' which is Greek for 'many,' and 'tics,' which are blood-sucking insects.


-Gore Vidal


#8 smart_monkey

smart_monkey

    GMC Member

  • GMC Member
  • 91 posts

Posted 18 March 2009 - 08:23 AM

Only if I could make 100% GML. Thanks for all your replies!
Krisando, if you right a good script on your site, then only files you want to go up will go up.

Also for all people who use the script, there is a bug where the window will completely disappear and wont come back. It is NOTHING to do with my script. It is to do with 39dll.dll. Dont ask me how or why, these are questions for 39ster. Again, its not what I wrote, its to do with 39dll or Game Maker itself.
  • 0

#9 stopb

stopb

    GMC Member

  • New Member
  • 24 posts

Posted 04 June 2009 - 08:34 AM

i created new script in game maker
named it uploadfile
Copyed the HTTP Upload Script into it

I logged on my webpage http://stopb.planet.ee/
in the htdocs folder created .php file
named it upload.php
Then i created 2 directorys: upload & uploads (just in case)
I made these directorys free for all users to write, read, and do whatever in them.

Then i made an object in Game Maker.
named it o_upload, witch create event contains next:

uploadfile('http://stopb.planet.ee/upload.php,C:\Upload.txt');

The file Upload.txt is on the C drive . and it contains 'Uploaded text'

Made a room.
At the room create event i placed code:

dllinit(0, true, true);

then places the o_upload in the room.

When i louch it, it shows me just a black screen and game Crushes/Laggs/Freezes.
Folders at the homepage are empty.

Please give me any support how can i make it work correct =)
  • 0