It seems like the server can respond in 2 different ways, depeding on the file size
For a small image (mostly one color, of empty server):
-It tells me the content length
-I can save the image successfully
For a big image (lots of colors, big build)
-It doesn't tell me the content length
-It puts a wierd "2000" on the line after the empty line
-The image saved is incomplete
I added a bunch of debug echo in the code:
function zgui_serverlist::loadserverimage(%this, %id)
{
if(%id < %this.getcount())
{
filedelete("config/zgui_serverdetail.jpg");
%ip = %this.getobject(%id).ip;
%port = %this.getobject(%id).port;
%data = "q=" @ strreplace(%ip, "." , "-") @ "_" @ %port;
%len = strlen(%data);
%request = "GET /detail.php?" @ %data @ " HTTP/1.1\r\nHost:image.blockland.us\r\nUser-Agent: Torque/1.0\r\nContent-Type: text/html\r\nContent-Length: " @ %len @"\r\n\r\n" @ %data;
%tcp = new TCPObject(zgui_dlserverimagetcp)
{
request = %request;
};
zgui_container.add(%tcp);
%this.dlimagetcp = %tcp;
%tcp.connect("image.blockland.us:80");
}
}
function zgui_dlserverimagetcp::onConnected(%this)
{
echo("CONNECTED");
echo("SEND " @ %this.request);
%this.send(%this.request);
}
function zgui_dlserverimagetcp::onLine(%this, %line)
{
echo("RECEIVE" SPC %line);
if(strstr(%line, "Content-Length:") == 0)
{
%this.haslen = 1;
%this.len = getword(%line, 1);
echo("We know length, save when chunk is bigger than len");
}
else if(%line $= "" && !%this.haslen)
{
%this.getnext = 1;
echo("Server didn't tell us the length wtf wtf");
}
else if(%line $= "" && %this.haslen)
{
%this.setbinary(1);
}
else if(%this.getnext)
{
echo("Ok what is up with this 2000 after the empty line?");
%this.setbinary(1);
}
}
function zgui_dlserverimagetcp::onBinChunk(%this, %chunk)
{
echo("CHUNK" SPC %chunk);
if(%this.haslen)
{
if(%chunk >= %this.len)
{
echo("save because chunk is bigger than len");
%this.savebuffertofile("config/client/zgui_serverdetail.jpg");
%this.schedule(10,delete);
zgui_serverdetail_image.setbitmap("config/client/zgui_serverdetail.jpg");
}
}
else
{
cancel(%this.schedule);
%this.schedule = %this.schedule(1000,endload);
}
}
function zgui_dlserverimagetcp::endload(%this)
{
echo("server didn't tell us the length, saving by timeout");
%this.savebuffertofile("config/client/zgui_serverdetail.jpg");
%this.schedule(10,delete);
zgui_serverdetail_image.setbitmap("config/client/zgui_serverdetail.jpg");
}
Here is what it looks like for the 2 ways the server responds:
CONNECTED
SEND GET /detail.php?q=69-64-43-11_32500 HTTP/1.1
Host:image.blockland.us
User-Agent: Torque/1.0
Content-Type: text/html
Content-Length: 19
q=69-64-43-11_32500
RECEIVE HTTP/1.1 200 OK
RECEIVE Date: Wed, 27 Feb 2013 16:40:59 GMT
RECEIVE Server: Apache/2.2.3 (Red Hat)
RECEIVE X-Powered-By: PHP/5.1.6
RECEIVE Vary: Accept-Encoding
RECEIVE Content-Length: 6864
We know length, save when chunk is bigger than len
RECEIVE Content-Type: image/jpeg
RECEIVE
CHUNK 1316
CHUNK 2816
CHUNK 4316
CHUNK 5816
CHUNK 6864
save because chunk is bigger than len
CONNECTED
SEND GET /detail.php?q=68-168-212-106_30200 HTTP/1.1
Host:image.blockland.us
User-Agent: Torque/1.0
Content-Type: text/html
Content-Length: 22
q=68-168-212-106_30200
RECEIVE HTTP/1.1 200 OK
RECEIVE Date: Wed, 27 Feb 2013 16:41:19 GMT
RECEIVE Server: Apache/2.2.3 (Red Hat)
RECEIVE X-Powered-By: PHP/5.1.6
RECEIVE Vary: Accept-Encoding
RECEIVE Transfer-Encoding: chunked
RECEIVE Content-Type: image/jpeg
RECEIVE
Server didn't tell us the length wtf wtf
RECEIVE 2000
Ok what is up with this 2000 after the empty line?
CHUNK 1304
CHUNK 2804
-snip-
CHUNK 66494
CHUNK 67994
CHUNK 69478
server didn't tell us the length, saving by timeout
Can someone tell me how to handle saving the file if the server responds in the second way, probably something to do with this line:
Transfer-Encoding: chunked