On 2013-05-15,
pic...@alice.it <
pic...@alice.it> wrote:
>>
>> So, added handling of Location (and Status) to the core HTTP server libs.
>>
>
> Thank you so much.
Comments make things better :-)
> A note on the implementation of the server. Why did you choose to send http reply via throw/1? I ask this because I had troubles with normal exception handling and I had to add a rule to intercept http_reply exception. This is by no means criticism, but what was the rationale?
Hmmm. The HTTP server libs have a long history, starting as a small
hack before there were even threads in SWI-Prolog and incrementally
extended to handle more and more demands. I think there are two reaons.
First of all, one needs the infrastructure to handle exceptions from
the handler anyway to avoid the server from dying and to produce a
sensible error reply.
Second, the default handler writes current output to a special
location. In old versions this was a temporary (memory) file. Now,
if you want tasks such as serving a large plain file, you do not want
the handler to write the content of the file to current_output into
the memory file, compute a header from it and send the memory file to
the client over the socket connection. It just means a lot of memory
usage and needless copying of data. So, instead, you want to serve
the file directly from disk. This implies you want to get out of the
current redirected output mode and do something else. That was
already implemented for dealing with exceptions and thus it seemed
sensible to reuse that code.
The alternative that I didn't see those days is to formulate the
alternative action as a CGI header. Something like
send_file(File) :-
mime_type(File, Type),
format('Content-type: ~w~n', [Type]),
format('Send-file: ~w~n~n', [File]).
This would be more elegant in the sense that I think that exceptions
are called such because they are not supposed to be used as part of
the normal control flow. On the other hand, http_reply_file/3 deals
with these details, and so does http_redirect/3 and http_404/2. These
predicates where introduced to shield the underlying exception
mechanism from the user. A nice bonus is that you can document
predicates and find them.
Cheers --- Jan
P.s. Updated
http_cgi.pl on the plweb git repo: documentation and
added a lot more to the CGI environment. Planning to move
this library to the core, so further comments, fixes and
additions are welcome.