Pages: 1
  Print  
Author Topic: How the heck am I supposed to get a HTML page?  (Read 38115 times)
Offline (Unknown gender) AsuMagic
Posted on: February 14, 2014, 04:32:36 pm
Member
Joined: Nov 2013
Posts: 23

View Profile Email
Hey,
I just don't understand how to download a HTML page ( In my case api.kag2d.com ).
I turned on the network stuff in ENIGMA API.
Someone told me I'd need to do net_init(), apisock = net_connect_tcp("api.kag2d.com",80,true), msg = "GET / HTTP/1.1";  tcp_send_raw(apisock,msg,string_length(msg)); and the stuff to receive the data.
The problem is that apisock is equal to -4. ENIGMA wiki says negative values returns an error but it does not give a list of the errors.
I don't have any idea how to do it.
Can someone explain me how do it works and an example to get the page api.kag2d.com? Just telling I'm a pure noob at networking stuff and not a pro at ENIGMA, I'm much more habitued to GM.
Logged
Offline (Male) Goombert
Reply #1 Posted on: February 14, 2014, 04:56:37 pm

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 2991

View Profile
Hello Asu, that system was originally written by IsmAvatar. I just ported it to ENIGMA.

The error codes are in a text file in the system source.
https://github.com/enigma-dev/enigma-dev/blob/master/ENIGMAsystem/SHELL/Networking_Systems/BerkeleySockets/errors.txt

Try net_http
http://enigma-dev.org/docs/Wiki/Net_http
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) AsuMagic
Reply #2 Posted on: February 15, 2014, 03:44:09 am
Member
Joined: Nov 2013
Posts: 23

View Profile Email
Wow fast answer ^^
I tried net_http but it returned '302 moved' while I don't have any error via my browser ( Both with opera and ie )
Logged
Offline (Male) Goombert
Reply #3 Posted on: February 15, 2014, 11:03:58 am

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 2991

View Profile
I forget how it works, I haven't messed with that networking system in forever :P

Look at the code and see what Berkeley Sockets functions they call.
https://github.com/enigma-dev/enigma-dev/blob/master/ENIGMAsystem/SHELL/Networking_Systems/BerkeleySockets/BSbrowser.cpp#L126

Looks like the code is similar to what you originally tried.
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) AsuMagic
Reply #4 Posted on: February 15, 2014, 04:39:26 pm
Member
Joined: Nov 2013
Posts: 23

View Profile Email
net_http makes my game crash..
Logged
Offline (Male) Goombert
Reply #5 Posted on: February 15, 2014, 07:09:45 pm

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 2991

View Profile
I see why, it's not the function it's how our website is set up.

I just put the following into the create event of an Object.
Code: (EDL) [Select]
show_message(net_http("google.com",""));It worked and produced the following.
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) AsuMagic
Reply #6 Posted on: February 16, 2014, 08:40:41 am
Member
Joined: Nov 2013
Posts: 23

View Profile Email
Oh it's maybe because I did res = net_http("api.kag2d.com","/");
But with the good one ( res = net_http("api.kag2d.com",""); ) , it returns this :
Code: [Select]
<html>
<head><title>400 Bad·>˜:©m
( I've used clipboard_set_text(res); to copy the message, it's the same if I use show_message(res); )

I'd bet it's because of bad encryption ( ANSI instead of UTF or reverse for example ) so the request is bugged out. Is there a way to know what's doing / what did a function?
Logged
Offline (Unknown gender) TheExDeus
Reply #7 Posted on: February 16, 2014, 10:17:40 am

Developer
Joined: Apr 2008
Posts: 1860

View Profile
The function is the one Robert linked too. The code is:
Code: [Select]
string net_http(string host, string loc) {
 char *packet;

 int s = net_connect_tcp(host,"http",0);
 if (s < 0) die("Connect",0);

 char *cmd = "\
GET %s HTTP/1.1\r\n\
Host: %s\r\n\
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0\r\n\
Connection: close\r\n\r\n";
 int len = prepare(&packet,cmd,2,loc.c_str(),host.c_str());
 int r = net_send_raw(s,packet,len);
 free(packet);
 if (r < 0) die("Send",1,s);

 do {
  if ((packet = (char*)net_receive(s).c_str()) == NULL)
   die("Receive",1,s);

  //HTTP 1.1 requires handling of 100 Continue.
  while (strstr(packet,"HTTP/1.1 100 Continue\r\n") != NULL)
   packet = strstr(packet,"\r\n\r\n") + 4;
  //skip the header
  packet = strstr(packet,"\r\n\r\n");
 } while (packet == NULL); //packet not filled? Go back for more.
 packet += 4;

 closesocket(s);
 return packet;
}
Logged
Offline (Male) Goombert
Reply #8 Posted on: February 16, 2014, 12:37:58 pm

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 2991

View Profile
Quote
Is there a way to know what's doing / what did a function?
Rephrase that because I don't know what you mean.
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) AsuMagic
Reply #9 Posted on: February 16, 2014, 02:42:05 pm
Member
Joined: Nov 2013
Posts: 23

View Profile Email
What deus answered, what does this function precisely do
Logged
Offline (Male) Goombert
Reply #10 Posted on: February 16, 2014, 02:46:03 pm

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 2991

View Profile
Ah, it's like in Firefox when you right click and view the source code of a web page. It retrieves the code of the webpage.
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) AsuMagic
Reply #11 Posted on: February 17, 2014, 06:32:04 am
Member
Joined: Nov 2013
Posts: 23

View Profile Email
sooo how to fix this 400 error?
Logged
Offline (Unknown gender) TheExDeus
Reply #12 Posted on: February 17, 2014, 08:12:50 am

Developer
Joined: Apr 2008
Posts: 1860

View Profile
Quote
I'd bet it's because of bad encryption ( ANSI instead of UTF or reverse for example ) so the request is bugged out.
I think it because of encryption (https) in general (not coding like your ANSI/UTF thinking). The site https://api.kag2d.com/ is encrypted with TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 256 bit keys. That means the request also needs to be encrypted and the response decrypted. The function does nothing like that. Here is the request sent by the function:
Quote
GET  HTTP/1.1\r\n\
Host: https://api.kag2d.com/\r\n\
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0\r\n\
Connection: close\r\n\r\n
There are many things you could try to send it, but encryption is not supported by that function (it would require handshaking, certificates and a lot of other things to work). So try disabling encryption. Later either we or yourself maybe can figure out how to make it work with encryption.
Logged
Offline (Unknown gender) AsuMagic
Reply #13 Posted on: February 17, 2014, 12:44:01 pm
Member
Joined: Nov 2013
Posts: 23

View Profile Email
Okaaay so as I'm really a shit at encryption stuff, I guess I'll need to find a DLL?
How to load .gml files with ENIGMA?
I've searched but didn't find so I don't have any idea how to use half of DLLs.
I've also tried a networking DLL but it said it couldn't load DLL etc. and an GEX one but I just didn't find how to use them with ENIGMA.
Logged
Offline (Unknown gender) TheExDeus
Reply #14 Posted on: February 17, 2014, 04:40:26 pm

Developer
Joined: Apr 2008
Posts: 1860

View Profile
Well I thought you just might try getting rid of encryption for site. No real reason for a site like that to have encryption. I personally made an update system once and all I did was GET a .txt file from a webserver. It's basically what you are trying, but you try to get .html via https.
I could technically make libcurl as an extension to ENIGMA. It would allow you to easily also get files from FTP and so on. The problem is that libcurl isn't that small and I am not sure about their licenses. So adding it to ENIGMA's git would probably be a mistake.
We should make a separate section to download ENIGMA extensions that are not directly inside ENIGMA.
Logged
Pages: 1
  Print