Author Topic: TCPObject::get(%this) Resource  (Read 614 times)

I was bored so I made a simple TCPObject.get(); function
Usage
TCPObject.host = "blockland.us:80"; //place:port
TCPObject.file = "/index.html"; //file
TCPObject.get(); // Must be connected


Code

function TCPObject::get(%this)
{
   //Example of TCPObject.file: /index.html
   //Example of TCPObject.host: blockland.us:80
   %this.send("GET " @ %this.file @ " HTTP/1.1\r\n");
   %this.send("Host: " @ %this.host @ "\r\n");
   %this.send("Content-Location: " @ %this.file @ "\r\n\r\n");
}


Example Function

function TCPObject::get(%this)
{
   %this.send("GET " @ %this.file @ " HTTP/1.1\r\n");
   %this.send("Host: " @ %this.host @ "\r\n");
   %this.send("Content-Location: " @ %this.file @ "\r\n\r\n");
}

function TempTCP::onLine(%this,%line)
{
   if(%this.debug)
      echo(%line);
}

function TempTCP::autoConnect(%this)
{
   if(strPos(%this.host,"/") > 1)
   {
      %this.file = getSubStr(%this.host,strPos(%this.host,"/"),strLen(%this.host));
      %this.host = getSubStr(%this.host,0,strPos(%this.host,"/"));
      %this.connect(%this.host @ ":80");
   }
   else
   {
      %this.file = "/";
      %this.connect(%this.host @ ":80");
   }
}
function TempTCP::onDisconnected(%this)
{
   %this.delete();
}
function TempTCP::onConnected(%this)
{
   %this.debug = 1;
   %this.get();
}
function readPage(%webURL)
{
   new TCPObject(TempTCP) { host = %webURL; };
   TempTCP.autoConnect();
}


If you want to add anything to it feel free.
Also feel free to point out any errors I made.

Is the port of the site always the same in this usage or is it different for some websites?

Is the port of the site always the same in this usage or is it different for some websites?
HTTP is always port 80
Though sometimes people use other things.

 I don't quite see the point in an entire resource and thread just for this. Writing HTTP requests is already so laughably easy.


I don't quite see the point in an entire resource and thread just for this. Writing HTTP requests is already so laughably easy.
Sorry port, we're not all quite as skilled as you.
I'll definitely learn from this.

As Kalphiter stated, HTTP Objects are just as useful for GET requests, and even simpler to use that TCP Objects:


function getWebPage(%host, %port, %file) // For example, if we wanted to get the page "example.com:80/file.ext", then we would use getWebPage("example.com", 80, "/file.ext")
{
    new HTTPObject(MyHTTPObject).get(%host @ ":" @ %port, %file); // Creating the HTTPObject and sending a request to example.com:80/file.ext
}

// Now we need to process the response
// Unlike TCPObjects, HTTPObjects do not return any headers
function MyHTTPObject::onLine(%this, %line)
{
    %page = %page NL %line; // For each new line we add the data onto a single variable
    if(%line $= "END") // You can have a line on the page, such as 'END' that tells the client to do something, in this case, run a function with the gathered data
    {
        doStuffWith(%page);
        %this.disconnect();
        %this.delete();
    }
}


Alternatively, you can use the HTTPObject::onDisconnect method to let the script know the data is complete.