Daniel@0
|
1 /* Part of DML (Digital Music Laboratory)
|
Daniel@0
|
2 Copyright 2014-2015 Samer Abdallah, University of London
|
Daniel@0
|
3
|
Daniel@0
|
4 This program is free software; you can redistribute it and/or
|
Daniel@0
|
5 modify it under the terms of the GNU General Public License
|
Daniel@0
|
6 as published by the Free Software Foundation; either version 2
|
Daniel@0
|
7 of the License, or (at your option) any later version.
|
Daniel@0
|
8
|
Daniel@0
|
9 This program is distributed in the hope that it will be useful,
|
Daniel@0
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Daniel@0
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Daniel@0
|
12 GNU General Public License for more details.
|
Daniel@0
|
13
|
Daniel@0
|
14 You should have received a copy of the GNU General Public
|
Daniel@0
|
15 License along with this library; if not, write to the Free Software
|
Daniel@0
|
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
Daniel@0
|
17 */
|
Daniel@0
|
18
|
Daniel@0
|
19 :- module(httpfiles,
|
Daniel@0
|
20 [ reply_file/2
|
Daniel@0
|
21 , reply_file/3
|
Daniel@0
|
22 , reply_stream/2
|
Daniel@0
|
23 , reply_stream/3
|
Daniel@0
|
24 , reply_html_page/4
|
Daniel@0
|
25 , write_headers/1
|
Daniel@0
|
26 , mime_type/2
|
Daniel@0
|
27 ]).
|
Daniel@0
|
28
|
Daniel@0
|
29 :- use_module(library(http/html_write)).
|
Daniel@0
|
30
|
Daniel@0
|
31 :- meta_predicate reply_html_page(+,:,:,+).
|
Daniel@0
|
32
|
Daniel@0
|
33 write_headers(Headers) :- maplist(write_header,Headers), nl.
|
Daniel@0
|
34
|
Daniel@0
|
35 write_header(length(L)) :- write_header('Content-Length'-L).
|
Daniel@0
|
36 write_header(no_ranges) :- write_header('Accept-Ranges'-none).
|
Daniel@0
|
37 write_header(stable) :- write_header('Cache-Control'-'max-age=31536000, public').
|
Daniel@0
|
38 write_header(unstable) :- write_header('Cache-Control'-'max-age=0, public').
|
Daniel@0
|
39 write_header(error) :- write_header('Cache-Control'-'max-age=0, private, must-revalidate').
|
Daniel@0
|
40 write_header(html) :-
|
Daniel@0
|
41 html_current_option(content_type(ContentType)),
|
Daniel@0
|
42 write_header('Content-Type'-ContentType).
|
Daniel@0
|
43 write_header(type(Type)) :-
|
Daniel@0
|
44 mime_type(Type,MimeType),
|
Daniel@0
|
45 write_header('Content-Type'-MimeType).
|
Daniel@0
|
46 write_header(cookie(Name,Value,Expires)) :-
|
Daniel@0
|
47 format(atom(Cookie),'~w=~w; path=/; Expires=~w',[Name,Value,Expires]),
|
Daniel@0
|
48 write_header('Set-Cookie'-Cookie).
|
Daniel@0
|
49 write_header(no_cache) :-
|
Daniel@0
|
50 maplist( write_header,
|
Daniel@0
|
51 [ 'Cache-Control'-'no-cache, no-store, must-revalidate'
|
Daniel@0
|
52 , 'Pragma'-'no-cache'
|
Daniel@0
|
53 , 'Expires'-'0'
|
Daniel@0
|
54 ]).
|
Daniel@0
|
55
|
Daniel@0
|
56 write_header(Name-Value) :-
|
Daniel@0
|
57 format('~w: ~w~n',[Name,Value]).
|
Daniel@0
|
58
|
Daniel@0
|
59 reply_html_page(Style,Head,Body,Headers) :-
|
Daniel@0
|
60 phrase(page(Style,Head,Body),Tokens),
|
Daniel@0
|
61 write_headers([html|Headers]),
|
Daniel@0
|
62 print_html(Tokens).
|
Daniel@0
|
63
|
Daniel@0
|
64 reply_file(File,Type) :- reply_file(File,Type,[stable]).
|
Daniel@0
|
65 reply_file(File,Type,Headers) :-
|
Daniel@0
|
66 write_headers([type(Type)|Headers]),
|
Daniel@0
|
67 debug(httpfiles,"Sending ~w with type ~w...",[File,Type]),
|
Daniel@0
|
68 setup_call_cleanup(
|
Daniel@0
|
69 open(File, read, In, [type(binary)]),
|
Daniel@0
|
70 copy_stream_data(In, current_output),
|
Daniel@0
|
71 close(In)),
|
Daniel@0
|
72 debug(httpfiles,"Finished sending.",[]).
|
Daniel@0
|
73
|
Daniel@0
|
74 with_tmp_stream(Enc,File,Str,Goal) :-
|
Daniel@0
|
75 setup_call_cleanup(
|
Daniel@0
|
76 tmp_file_stream(Enc,File,Str), Goal,
|
Daniel@0
|
77 (close(Str,[force(true)]), delete_file(File))).
|
Daniel@0
|
78
|
Daniel@0
|
79 reply_stream(In,Type) :- reply_stream(In,Type,[stable]).
|
Daniel@0
|
80 reply_stream(In,Type,Headers) :-
|
Daniel@0
|
81 ( memberchk(length(Length),Headers), var(Length)
|
Daniel@0
|
82 -> debug(httpfiles,"Stream length unknown - buffering to file...",[]),
|
Daniel@0
|
83 with_tmp_stream( octet, TmpFile, ToTmp,
|
Daniel@0
|
84 ( copy_stream_data(In,ToTmp), close(ToTmp),
|
Daniel@0
|
85 size_file(TmpFile,Length),
|
Daniel@0
|
86 debug(httpfiles,"Stream length was ~d bytes",[Length]),
|
Daniel@0
|
87 reply_file(TmpFile,Type,Headers)
|
Daniel@0
|
88 ))
|
Daniel@0
|
89 ; write_headers([type(Type)|Headers]),
|
Daniel@0
|
90 copy_stream_data(In, current_output)
|
Daniel@0
|
91 ),
|
Daniel@0
|
92 debug(httpfiles,"Finished sending stream.",[]).
|
Daniel@0
|
93
|
Daniel@0
|
94 % debug_stream(S,Label,Prop) :- stream_property(S,Prop), debug(httpfiles,"Stream property (~w): ~w",[Label,Prop]).
|
Daniel@0
|
95
|
Daniel@0
|
96 % copy_stream_length(In,Out,Len) :-
|
Daniel@0
|
97 % copy_stream_data(In,Out).
|
Daniel@0
|
98 % copy_stream_length(In,Out,0,Len).
|
Daniel@0
|
99
|
Daniel@0
|
100 % copy_stream_length(In,_,Len,Len) :- at_end_of_stream(In), !.
|
Daniel@0
|
101 % copy_stream_length(In,Out,L1,L3) :-
|
Daniel@0
|
102 % debug(httpfiles,"Reading...",[]),
|
Daniel@0
|
103 % read_pending_input(In,Codes,[]),
|
Daniel@0
|
104 % length(Codes,N), L2 is L1+N,
|
Daniel@0
|
105 % debug(httpfiles,"Transferring ~d bytes...",[N]),
|
Daniel@0
|
106 % format(Out,'~s',[Codes]),
|
Daniel@0
|
107 % flush_output(Out),
|
Daniel@0
|
108 % copy_stream_length(In,Out,L2,L3).
|
Daniel@0
|
109
|
Daniel@0
|
110 mime_type(csv , "text/csv").
|
Daniel@0
|
111 mime_type(ogg , "audio/ogg").
|
Daniel@0
|
112 mime_type(mp3 , "audio/mp3").
|
Daniel@0
|
113 mime_type(aac , "audio/aac").
|
Daniel@0
|
114 mime_type(svg, "image/svg+xml; charset=UTF-8").
|
Daniel@0
|
115 mime_type(pdf, "application/pdf").
|
Daniel@0
|
116 mime_type(eps, "application/postscript").
|
Daniel@0
|
117 mime_type(ps, "application/postscript").
|
Daniel@0
|
118 mime_type(kern, "text/plain; charset=UTF-8").
|
Daniel@0
|
119 mime_type(abc, "text/plain; charset=UTF-8"). % vnd.abc
|
Daniel@0
|
120 mime_type(mxml, "text/xml; charset=UTF-8").
|
Daniel@0
|
121 mime_type(lily, "text/plain; charset=UTF-8").
|
Daniel@0
|
122 mime_type(midi, "audio/midi").
|
Daniel@0
|
123 mime_type(png, "image/png").
|
Daniel@0
|
124 mime_type(mp4, "video/mp4").
|
Daniel@0
|
125 mime_type(json, "application/json; charset=UTF-8").
|
Daniel@0
|
126 mime_type(jsonp,"application/javascript; charset=UTF-8").
|
Daniel@0
|
127
|
Daniel@0
|
128 % httpfiles:mime_type(mxml, "application/vnd.recordare.musicxml+xml; charset=UTF-8").
|
Daniel@0
|
129 % httpfiles:mime_type(lily, "text/x-lilypond; charset=UTF-8").
|