forked from SWI-Prolog/packages-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_files.pl
155 lines (137 loc) · 6.13 KB
/
http_files.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2011-2020, VU University Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
:- module(http_files,
[ http_reply_from_files/3 % +Dir, +Options, +Request
]).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_dirindex)).
:- use_module(library(filesex)).
:- use_module(library(lists)).
:- use_module(library(option)).
:- predicate_options(http_reply_from_files/3, 2,
[ indexes(list(atom)),
pass_to(http_dispatch:http_reply_file/3, 2),
pass_to(http_dirindex:http_reply_dirindex/3, 2)
]).
/** <module> Serve plain files from a hierarchy
Although the SWI-Prolog Web Server is intended to serve documents that
are computed dynamically, serving plain files is sometimes necessary.
This small module combines the functionality of http_reply_file/3 and
http_reply_dirindex/3 to act as a simple web-server. Such a server can
be created using the following code sample, which starts a server at
port 8080 that serves files from the current directory ('.'). Note that
the handler needs a `prefix` option to specify that it must handle all
paths that begin with the registed location of the handler.
```
:- use_module(library(http/http_server)).
:- use_module(library(http/http_files)).
:- http_handler(root(.), http_reply_from_files('.', []), [prefix]).
:- initialization(http_server([port(8080)]), main).
```
@see pwp_handler/2 provides similar facilities, where .pwp files
can be used to add dynamic behaviour.
*/
%! http_reply_from_files(+Dir, +Options, +Request)
%
% HTTP handler that serves files from the directory Dir. This
% handler uses http_reply_file/3 to reply plain files. If the
% request resolves to a directory, it uses the option =indexes= to
% locate an index file (see below) or uses http_reply_dirindex/3
% to create a listing of the directory.
%
% Options:
%
% * indexes(+List)
% List of files tried to find an index for a directory. The
% default is `['index.html']`.
%
% Note that this handler must be tagged as a =prefix= handler (see
% http_handler/3 and module introduction). This also implies that
% it is possible to override more specific locations in the
% hierarchy using http_handler/3 with a longer path-specifier.
%
% When using http_handler/3 to bind this predicate to an HTTP
% location, make sure it is bound to a location that ends in a `/`.
% When using http:location/3 to define symbolic names to HTTP
% locations this is written as
%
% :- http_handler(aliasname(.),
% http_reply_from_files(srcdir, []),
% [prefix]).
%
% @param Dir is either a directory or an path-specification as
% used by absolute_file_name/3. This option provides
% great flexibility in (re-)locating the physical files
% and allows merging the files of multiple physical
% locations into one web-hierarchy by using multiple
% user:file_search_path/2 clauses that define the same
% alias.
% @see The hookable predicate file_mime_type/2 is used to
% determine the ``Content-type`` from the file name.
http_reply_from_files(Dir, Options, Request) :-
( memberchk(path_info(PathInfo), Request)
-> true
; PathInfo = ''
),
http_safe_file(PathInfo, []),
locate_file(Dir, PathInfo, Result, ResultType, Options),
!,
reply(ResultType, Result, Options, Request).
reply(file, Path, Options, Request) :-
http_reply_file(Path, [unsafe(true)|Options], Request).
reply(index, Path, Options, Request) :-
http_reply_dirindex(Path, [unsafe(true)|Options], Request).
reply(redirect, _, _, Request) :-
memberchk(path(Path), Request),
atom_concat(Path, /, NewLocation),
http_redirect(moved_temporary, NewLocation, Request).
locate_file(Dir, PathInfo, Result, ResultType, Options) :-
absolute_file_name(Dir, DirPath,
[ file_type(directory),
access(read),
solutions(all)
]),
directory_file_path(DirPath, PathInfo, Path),
( exists_file(Path)
-> ResultType = file,
Result = Path
; exists_directory(Path),
( sub_atom(Path, _, _, 0, /)
-> ( option(indexes(Indexes), Options, ['index.html']),
member(Index, Indexes),
directory_file_path(Path, Index, IndexFile),
exists_file(IndexFile)
-> Result = IndexFile,
ResultType = file
; Result = Path,
ResultType = index
)
; ResultType = redirect
)
).