1/* Part of SWI-Prolog 2 3 Author: Jan Wielemaker 4 E-mail: J.Wielemaker@vu.nl 5 WWW: http://www.swi-prolog.org 6 Copyright (c) 2011-2020, VU University Amsterdam 7 All rights reserved. 8 9 Redistribution and use in source and binary forms, with or without 10 modification, are permitted provided that the following conditions 11 are met: 12 13 1. Redistributions of source code must retain the above copyright 14 notice, this list of conditions and the following disclaimer. 15 16 2. Redistributions in binary form must reproduce the above copyright 17 notice, this list of conditions and the following disclaimer in 18 the documentation and/or other materials provided with the 19 distribution. 20 21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 POSSIBILITY OF SUCH DAMAGE. 33*/ 34 35:- module(http_files, 36 [ http_reply_from_files/3 % +Dir, +Options, +Request 37 ]). 38:- use_module(library(http/http_dispatch)). 39:- use_module(library(http/http_dirindex)). 40:- use_module(library(filesex)). 41:- use_module(library(lists)). 42:- use_module(library(option)). 43 44:- predicate_options(http_reply_from_files/3, 2, 45 [ indexes(list(atom)), 46 pass_to(http_dispatch:http_reply_file/3, 2), 47 pass_to(http_dirindex:http_reply_dirindex/3, 2) 48 ]).
indexes
to
locate an index file (see below) or uses http_reply_dirindex/3
to create a listing of the directory.
Options:
['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])
.
113http_reply_from_files(Dir, Options, Request) :- 114 ( memberchk(path_info(PathInfo), Request) 115 -> true 116 ; PathInfo = '' 117 ), 118 http_safe_file(PathInfo, []), 119 locate_file(Dir, PathInfo, Result, ResultType, Options), 120 !, 121 reply(ResultType, Result, Options, Request). 122 123reply(file, Path, Options, Request) :- 124 http_reply_file(Path, [unsafe(true)|Options], Request). 125reply(index, Path, Options, Request) :- 126 http_reply_dirindex(Path, [unsafe(true)|Options], Request). 127reply(redirect, _, _, Request) :- 128 memberchk(path(Path), Request), 129 atom_concat(Path, /, NewLocation), 130 http_redirect(moved_temporary, NewLocation, Request). 131 132locate_file(Dir, PathInfo, Result, ResultType, Options) :- 133 absolute_file_name(Dir, DirPath, 134 [ file_type(directory), 135 access(read), 136 solutions(all) 137 ]), 138 directory_file_path(DirPath, PathInfo, Path), 139 ( exists_file(Path) 140 -> ResultType = file, 141 Result = Path 142 ; exists_directory(Path), 143 ( sub_atom(Path, _, _, 0, /) 144 -> ( option(indexes(Indexes), Options, ['index.html']), 145 member(Index, Indexes), 146 directory_file_path(Path, Index, IndexFile), 147 exists_file(IndexFile) 148 -> Result = IndexFile, 149 ResultType = file 150 ; Result = Path, 151 ResultType = index 152 ) 153 ; ResultType = redirect 154 ) 155 )
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.