Author Topic: TCP Object retrieving corrupt image?  (Read 5244 times)

I've been working with TCP lately, and I'm trying to get images from a website just for practice.
Here's the code that I'm currently using:

new TCPObject(imageTCP)
{
    host = "blockland.us";
    port = "80";
    fileBase = "config/client/image.jpg";
};

function imageTCP::requestFile(%this, %file)
{
    %this.downloadFile = %file;
    %this.connect(%this.host @ ":" @ %this.port);
}

function imageTCP::onLine(%this, %line)
{
    if(strStr(%line, "Content-Length") > 0)
        %this.len = firstWord(%line);
    if(%line $= "" || %line $= " ")
        %this.setBinarySize(%this.len);
}

function imageTCP::onBinChunk(%this, %c)
{
    if(!%c < %this.len)
        %this.saveBufferToFile(%this.fileBase);
}

function imageTCP::onConnected(%this)
{
    %this.send("GET" SPC %this.downloadFile SPC "HTTP/1.0\nHost:" SPC %this.host @ "\r\n\r\n");
}


And using imageTCP.requestFile("/frontpage/frontpage-shaders-00.jpg); , this is the result:


halp
« Last Edit: July 16, 2014, 09:55:05 PM by Cruxeis »

You can't pull images from Blockland.us, you'll get a bunch of chunked images. It's from my understanding that you'll need an external site for this.

Edit: To be specific:
It will send the preview images chunked, regardless of whether you tell it you only support identity encoding or http/1.0 or want to use connection:close. You can't filter out the chunk borders using torkscript as you can't parse the binary data, so you'll need an external webserver to fix that or ask badspot to add a way to download the images without chunked transfer.

-snip-
Are you sure? It looks like he's talking about just the server previews, not the entire site. Unless I'm blind lol

Oh that part is because I was trying to set server previews to the images on the site. I'm not too sure about the frontpage images. I'd attempt to use a different site though, to see if it's the code or the site itself.

The site isn't chunked, it's the preview images that are chunked. It's definitely the site. Also, I remember it sending out non-chunked image when you request with http/1.0. Maybe Badspot changed it, but it definitely used to work that way.

I remember that "new gui" system that used tcp objects to get an images of the server

The site isn't chunked, it's the preview images that are chunked. It's definitely the site. Also, I remember it sending out non-chunked image when you request with http/1.0. Maybe Badspot changed it, but it definitely used to work that way.

Nope. Just tried it:

Got it working with Support_TCPClient. Give me a little while to polish it up.

Finished! http://greek2me.us/code/Support_TCPClient.cs
However, it's still subject to change.

Here's the relevant onBinChunk method that handles it:
Code: [Select]
//Called when a binary chunk is received. To use with your mod, replace "TCPClient" with your class name.
//Only called when in binary mode.
//@param int chunk The number of bytes received.
function TCPClient::onBinChunk(%this, %chunk)
{
//Damn inheritance is broken in the engine.
if(isFunction(%this.className, "onBinChunk"))
eval(%this.className @ "::onBinChunk(" @ %this @ ",\"" @ %chunk @ "\");");

%contentLength = %this.headerField["Content-Length"];
%contentLengthSet = (strLen(%contentLength) > 0);
if($TCPClient::Debug)
echo(" > " @ %chunk @ "/" @ %contentLength SPC "bytes");

if(%contentLengthSet)
%this.setProgressBar(%chunk / %contentLength);
if(%chunk >= %contentLength && %contentLengthSet)
{
%save = true;
%done = true;
}
else
{
//this is a chunked/streaming transfer
if(!%contentLengthSet)
{
%save = true;
%done = false;
}
cancel(%this.timeoutSchedule);
%this.timeoutSchedule = %this.schedule($TCPClient::timeout, "onDone", $TCPClient::Error::connectionTimedOut);
}

if(%save)
{
if(strLen(%this.savePath) && isWriteableFileName(%this.savePath))
{
%this.saveBufferToFile(%this.savePath);
if(%done)
{
%this.onDone($TCPClient::Error::none);
}
}
else
{
%this.onDone($TCPClient::Error::invalidDownloadLocation);
}
}
}

Note that an HTTP 1.0 request is required.
« Last Edit: July 17, 2014, 02:16:29 AM by Greek2me »

I thought you said the images would be chunked even with a http/1.0 request. If they're still chunked, how could that code work?

I remember that "new gui" system that used tcp objects to get an images of the server

That used an external site, it must be just the server preview images that are chunked.

I thought you said the images would be chunked even with a http/1.0 request. If they're still chunked, how could that code work?

Not chunked, wrong word I guess. The difference from a normal file though is that the server doesn't tell you what size the file is.