:- ensure_loaded(common).
cvs_id('$Id: static_reply.pl,v 1.2 2005/04/14 00:01:29 sandro Exp $').
/*


  
*/
%%
%%  Write an HTTP Header and send the file, if it exists
%%  as WWW/<FileName>.{jpg,png,html,css,rdf}
%%
%%  ... or if   WWW/<FileName>.header exists, then send it
%%  first as the HTTP response header, and then send along
%%  WWW/<FileName> as the content.   The header file should
%%  at least include a Content-Type header, and may also
%%  include (eg) Cache-Control.  (That's what we want it for:
%%  cache testing.)

serve(FileName) :-
	www_file(FileName, '.headers', HeaderFileName),
	exists_file(HeaderFileName),
	!,
	www_file(FileName, '', FullFileName),
	telling(Telling), 
        open(HeaderFileName, read, Stream1, [type(binary)]),
        copy_stream_data(Stream1, Telling),
	close(Stream1),
	format('~n', []),
        open(FullFileName, read, Stream2, [type(binary)]),
        copy_stream_data(Stream2, Telling),
	close(Stream2).
serve(FileName) :-
	% suffix not given
	mime_type(Suffix, ContentType),
	www_file(FileName, Suffix, FullFileName),
	send_file(FullFileName, ContentType).
serve(FileName) :-
	% suffix given
	mime_type(GivenSuffix, ContentType),
	atom_concat(Prefix, GivenSuffix, FileName),
	mime_type(FoundSuffix, ContentType),
	www_file(Prefix, FoundSuffix, FullFileName),
	send_file(FullFileName, ContentType).
serve(FileName) :-
	www_file(FileName, '.*', FullFileName),
	print_message(informational, format('Nothing like ~q', [FullFileName])),
	send_404(FileName).

% should perhaps come from /etc/mime.types...
mime_type('.jpg', 'image/jpeg').
mime_type('.gif', 'image/gif').
mime_type('.png', 'image/png').
mime_type('.html', 'text/html').
mime_type('.css', 'text/css').
mime_type('.rdf', 'application/rdf+xml').



%%
%%  ... do the work of serve/1; prolog-fail if no such file
%%  so that serve/1 will backtrack and try another.
%%
send_file(FullFileName, ContentType) :-
	exists_file(FullFileName),
	!,  % we're committed; don't try to backtrack to another media type!
        open(FullFileName, read, Stream, [type(binary)]),
	get_time(Now),   % want it in GMT!
	ExpiresTime is Now + 186400, % go long for now -- no timezone support!
	http_header:rfc_date(ExpiresTime, Codes, ""),
	atom_codes(Expires, Codes),
	time_file(FullFileName, LastModTime),
	http_header:rfc_date(LastModTime, LMCodes, ""),
	atom_codes(LM, LMCodes),
        format('Content-Type: ~a~nExpires: ~a GMT~nLast-Modified: ~a GMT~n~n', [ContentType, Expires, LM]),
        %format('Content-Type: ~a~nCache-Control: max-age=3600~nExpires: ~a GMT~nLast-Modified: ~a GMT~n~n', [ContentType, Expires, LM]), %format('Content-Type: ~a~nExpires: Wed, 22 Jul 2004 20:16:01 GMT~nLast-Modified: Tue, 21 Jul 2004 20:30:44~n~n', [ContentType, Expires]),
        telling(Telling),
        copy_stream_data(Stream, Telling),
	close(Stream).

send_404(Filename) :-
	% to really return a 404, we'll need http_wrapper to let us
	% get this through to http_reply.  Sigh.   Temp Hack (error 500,
	% probably, not 404.)
	%    throw(http_reply(server_error(foo2))).
	throw(http_reply(not_found(Filename))).
	
