Re: http server push
Jan Wielemaker <[email protected]> Sun, 13 Apr 2014 10:26:44 +0200
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <[email protected]> |
Igor,
On 11-04-14 15:11, Igor Wojnicki wrote:
> Jan,
>
> Thanks for the reply.
>
> Your example, with the transfer encoding works fine as long as the
> client requests HTTP/1.1. The chunked encoding doesn't work with
> HTTP/1.0 though. I'd rather go without chunked encoding, esp. that it
> has to be supported by the client.
>
> It seems that using flush_output/0, while replying to a HTTP/1.0 request
> doesn't flush the buffer, the client does not receive data. Is it a bug
> or a feature?
>
> Of course it can be assumed that nobody uses HTTP/1.0 anymore but I'd
> appreciate if I could flush the output buffer and have this partial
> transfer working no matter HTTP version or transfer mode. Could you
> point me what to change in the http* modules to make it work this way?
That won't be really easy. Basically, the output of the handler is
attached to a `cgi-stream', defined in cgi_stream.c of the http package.
The stream is handled in http_wrapper.pl, notably cgi_hook/2. One option
is to change some of the logic there that disables collecting the data
and emits the header without a Content-length. The other option would
be to use the same mechanism as http_reply_file/2. This throws an
exception to get rid of the cgi-stream and talk directly to the
underlying sockets. In both cases, it is an open question how to
initiate this special mode. Probably this requires an additional
predicate. Another option is to define a special header. For example:
Content-length: close
and pick this up in cgi_hook/2 to do the magic.
Anyway, if you find a fairly clean solution, I'm happy to apply the
patches.
Hope this helps
--- Jan
>
> By the way I use swi 6.2.6.
>
> Thanks again,
> Igor.
>
> On 04/11/2014 12:22 PM, Jan Wielemaker wrote:
>> It works like this:
>>
>> :- use_module(library(http/http_dispatch)).
>> :- use_module(library(http/thread_httpd)).
>> :- http_handler(root(longpoll), longpoll, [time_limit(5)]).
>>
>> server :-
>> http_server(http_dispatch, [port(4000)]).
>>
>> longpoll(_Request) :-
>> format('Transfer-Encoding: chunked~n'),
>> format('Content-type: text/plain~n~n'),
>> format('testing~n'),
>> flush_output,
>> sleep(2),
>> format('testing~n').
>>
>> I.e., you need chunked transfer encoding and you need to flush.
>>
>> Cheers --- Jan
>