HTTPObject EOF Detection?
by RealDecoy Inc · in Torque 3D Professional · 12/11/2009 (11:33 pm) · 3 replies
Ok, so I have some code to talk to a webservice. I'm using XML (using the builtin tinyxml implimentation) and I have that pretty much working (tweaking xml schemas and so on). But that's with a dummy xml file being spit out by apache which is all on one line.
I modified my onLine function to match this resource: http://www.torquepowered.com/community/resources/view/4085
But it's detecting a custom character set with a trailing newline to mark end of file.
I need a "normal" way to detect EOF, so that I can do:
if EOF then process block, clear buffer and reset connection
else append line to buffer
But I can't seem to find this info anywhere... So how do I go about detecting EOF with HTTPObject?
I modified my onLine function to match this resource: http://www.torquepowered.com/community/resources/view/4085
But it's detecting a custom character set with a trailing newline to mark end of file.
I need a "normal" way to detect EOF, so that I can do:
if EOF then process block, clear buffer and reset connection
else append line to buffer
But I can't seem to find this info anywhere... So how do I go about detecting EOF with HTTPObject?
#2
This passes the returned content to an XML parser, but you could use this to return content to whatever you like. (this relies on modifying httpobject source to not urlencode the & symbol.)
12/12/2009 (12:33 am)
In case anyone is interested, here's a quick chunk from my http webservice code.This passes the returned content to an XML parser, but you could use this to return content to whatever you like. (this relies on modifying httpobject source to not urlencode the & symbol.)
$WebServer = "someurl.com:80";
$WebPath = "/";
$WebApp = "webservice.php";
$WebService = new HTTPObject(WebSvc);
$WebHeaderDone = 0;
function WebServiceCommand(%this,%xmlpacket)
{
$WebService.clientid = "Torque";
%query = "packet="@%xmlpacket;
%script = $WebPath @ $WebApp;
$WebService.get($WebServer,%script,%query);
}
function WebSvc::onLine(%this,%line)
{
if(getWord(%line,0) $= "Content-Length:"){
$WebContentLength = getWord(%line,1);
}else{
if($WebHeaderDone==false)
{
if($WebContentLength>0 && strlen(%line)<=1)
{
$WebHeaderDone = true;
}
}else{
$WebResp = $WebResp @ "n" @ %line;
if(strlen($WebResp)>=$WebContentLength)
{
XML_RespDecode(%this,$WebResp);
$WebResp = "";
$WebContentLength = 0;
$WebHeaderDone = false;
$WebService.disconnect();
}
}
}
}
#3
Note : It works with T3D beta2.
Content-Length: is not always returned by webservices (even if it should).
08/08/2010 (11:59 am)
It can also be done with a code like this : function ini_http()
{
$WebServer = "someurl.com:80";
$WebPath = "/";
$WebApp = "webservice.php";
$WebService = new HTTPObject(WebSvc);
}
function WebServiceCommand(%this,%xmlpacket)
{
$WebService.clientid = "Torque";
%query = "packet="@%xmlpacket;
%script = $WebPath @ $WebApp;
$WebService.get($WebServer,%script,%query);
}
function WebSvc::onLine(%this,%line)
{
$WebResp = $WebResp @ %line;
}
function WebSvc::onDisconnect(%this)
{
echo($WebResp);
// To check if we've lasts chars.
echo(getSubStr($WebResp,strlen($WebResp)-8,8));
// XML_RespDecode(%this,$WebResp);
}Note : It works with T3D beta2.
Content-Length: is not always returned by webservices (even if it should).
Torque 3D Owner RealDecoy Inc
For anyone else wondering this, you can read the content headers and grab the content length header, then start recording after the following blank line, and count content length of your buffer, once the buffer content length is equal or longer than the http content length you should be at EOF.
Once I get a good chunk of this code working, I'll post it as a resource perhaps.
Thanks!