Valhalla Legends Archive

Programming => General Programming => Topic started by: iago on September 29, 2004, 08:22 AM

Title: Sreaming Http Content
Post by: iago on September 29, 2004, 08:22 AM
I'm trying to write something similar to Skywing's ISAPI for streaming Battle.net channels, but I'm having a problem with getting it to work on all browsers.

Here is what I'm sending now:

    out.write(protocol + " 200 OK\n");
    out.write("Connection: close\n");
    out.write("Date: " + new Date() + "\n");
    out.write("Server: TestServer\n");
    out.write("Pragma: no-cache\n");
    out.write("Cache-Control: no-cache\n");
    out.write("Content-type: text/html\n");
    out.write("\n");
    out.flush();

Followed by the streaming HTML as it comes.

I think this is the same as Skywing's, but since it's not working 100%, I guess it's not.  Is there some special way to tell the browser there's going to be a stream coming or something?

Thanks!
Title: Re: Sreaming Http Content
Post by: Soul Taker on September 29, 2004, 10:38 AM
I think the trick is telling the browser that the HTML you're sending is humongous, that way it will keep  waiting for more data.  Then you just send the new stuff as it comes.
Title: Re: Sreaming Http Content
Post by: TangoFour on September 29, 2004, 10:45 AM
Instead of \n, shouldn't you use \r\n ? Carriage Return, followed by Line Feed?

I think I read something about it in the HTTP RFC
Title: Re: Sreaming Http Content
Post by: Skywing on September 29, 2004, 01:03 PM
Quote from: Soul Taker on September 29, 2004, 10:38 AM
I think the trick is telling the browser that the HTML you're sending is humongous, that way it will keep  waiting for more data.  Then you just send the new stuff as it comes.
No, then the browser will tend to not show the page until all the HTML is loaded (at leastt Netscape works like this, IIRC -- IE eventually starts showing stuff, but it takes time).  Regardless, not a desireable solution.

Quote from: TangoFour on September 29, 2004, 10:45 AM
Instead of \n, shouldn't you use \r\n ? Carriage Return, followed by Line Feed?

I think I read something about it in the HTTP RFC

Yes, it should be \r\n over the network (though perhaps iago's CGI interface is automagically translating \n into \r\n -- he needs to check on this, though).

The way I do it is like this:

if(!Streaming)
wsprintf(ContentLength+strlen(ContentLength), "Content-Length: %u\r\n\r\n", Bytes);
else
strcat(ContentLength, "\r\n");
Title: Re: Sreaming Http Content
Post by: iago on September 29, 2004, 05:51 PM
No, I wasn't sending \r\n.  I'll try doing that instead and see if it improves the performance in some browsers.
Title: Re: Sreaming Http Content
Post by: MyndFyre on September 29, 2004, 05:52 PM
I agree with Tango and Skywing: generally, you need to use \r\n.  

The way to keep the browser streaming is to completely emit the


Content-Length:


from the HTTP header.

That should keep the socket alive on both ends.

Although -- from Skywing's code it looks like he uses Content-Length: \r\n\r\n and just doesn't specify a length.  From having packet-sniffed your headers, though, I don't recall seeing Content-Length at all.
Title: Re: Sreaming Http Content
Post by: Banana fanna fo fanna on September 29, 2004, 07:02 PM
This seems like a web development topic to me :)
Title: Re: Sreaming Http Content
Post by: K on September 29, 2004, 07:02 PM
Quote
Although -- from Skywing's code it looks like he uses Content-Length: \r\n\r\n and just doesn't specify a length.  From having packet-sniffed your headers, though, I don't recall seeing Content-Length at all.

Look at what is written to the variable ContentLength in the first case vs. the second case.
Title: Re: Sreaming Http Content
Post by: MyndFyre on September 29, 2004, 07:58 PM
Quote from: K on September 29, 2004, 07:02 PM
Quote
Although -- from Skywing's code it looks like he uses Content-Length: \r\n\r\n and just doesn't specify a length.  From having packet-sniffed your headers, though, I don't recall seeing Content-Length at all.

Look at what is written to the variable ContentLength in the first case vs. the second case.

Ahhh my mistake, he's not actually writing "Content-Length", he's writing *to* ContentLength.  ;)
Title: Re: Sreaming Http Content
Post by: iago on September 29, 2004, 09:05 PM
Quote from: $t0rm on September 29, 2004, 07:02 PM
This seems like a web development topic to me :)

I'd say this suits Bot Development, because this is part of my bot :P

But I don't read the web development board, and I know that a lot of people don't, so posting it here it was more likely that I'd get a response, and also more likely that I'd remember its existance.

It helps, too, that I moderate this board :)

The problem I was having is that some browsers (Safari, IE, and some others) weren't displaying the page until the socket was closed (by me exiting).  Hopefully that'll change when I do \r\n.
Title: Re: Sreaming Http Content
Post by: Skywing on September 30, 2004, 02:26 PM
You're required to use \r\n by the HTTP spec, so it wouldn't surprise me if failing to do so is causing browsers to act strangely.
Title: Re: Sreaming Http Content
Post by: iago on September 30, 2004, 06:19 PM
Hmm, I made it \r\n and I still have the problem.  The weird thing is, my headers are the same as Skywing's, but his works with the browser and mine doesn't :(

I'm going to have to research this problem more, I guess :/
Title: Re: Sreaming Http Content
Post by: Banana fanna fo fanna on September 30, 2004, 07:40 PM
What language/framework/library are you running on?
Title: Re: Sreaming Http Content
Post by: MyndFyre on September 30, 2004, 08:53 PM
Quote from: iago on September 30, 2004, 06:19 PM
Hmm, I made it \r\n and I still have the problem.  The weird thing is, my headers are the same as Skywing's, but his works with the browser and mine doesn't :(

I'm going to have to research this problem more, I guess :/


Are you using a pure socket connection (Java), and is your socket staying connected when streaming?
Title: Re: Sreaming Http Content
Post by: iago on October 01, 2004, 07:29 AM
Storm -- it doesn't matter.  I'm using Java with a plain BufferedOutputStream.

Mynd - yes.  The problem is that at least one browser (Safari) doesn't recognize the streaming content until the socket is closed.  Once I close it, it displays the entire thing.

I would assume it's a problem with the browser, but he said he can see Webbot streams fine.
Title: Re: Sreaming Http Content
Post by: Banana fanna fo fanna on October 01, 2004, 09:50 PM
Are you sure you're calling flush()?
Title: Re: Sreaming Http Content
Post by: iago on October 02, 2004, 01:17 AM
Yes.  If i wasn't, it wouldn't work on other browsers.  The information is defintaely getting there.