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) 2009-2014, 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_server_files, 36 [ serve_files_in_directory/2 % +Alias, +HTTPRequest 37 ]). 38:- use_module(library(http/http_path)). 39:- use_module(library(http/http_dispatch)).
53:- multifile 54 http:location/3. % Alias, Expansion, Options 55:- dynamic 56 http:location/3. % Alias, Expansion, Options 57 58httplocation(icons, root(icons), [ priority(-100) ]). 59httplocation(css, root(css), [ priority(-100) ]). 60httplocation(js, root(js), [ priority(-100) ]). 61 62:- multifile 63 user:file_search_path/2. 64:- dynamic 65 user:file_search_path/2. 66 67user:file_search_path(icons, library('http/web/icons')). 68user:file_search_path(css, library('http/web/css')). 69user:file_search_path(js, library('http/web/js')). 70 71:- http_handler(icons(.), serve_files_in_directory(icons), 72 [prefix, priority(-100)]). 73:- http_handler(css(.), serve_files_in_directory(css), 74 [prefix, priority(-100)]). 75:- http_handler(js(.), serve_files_in_directory(js), 76 [prefix, priority(-100)]).
prefix
. Below is an example
serving images from http://<host>/img/... from the directory
http/web/icons
.
http:location(img, root(img), []). user:file_search_path(icons, library('http/web/icons')). :- http_handler(img(.), serve_files_in_directory(icons), [prefix]).
This predicate calls http_404/2 if the physical file cannot be located. If the requested path-name is unsafe (i.e., points outside the hierarchy defines by the file_search_path/2 declaration), this handlers returns a 403 Forbidden page.
103serve_files_in_directory(Alias, Request) :- 104 memberchk(path_info(PathInfo), Request), 105 !, 106 Term =.. [Alias,PathInfo], 107 ( catch(http_safe_file(Term, []), 108 error(permission_error(read, file, _), _), 109 fail) 110 -> ( absolute_file_name(Term, Path, 111 [ access(read), 112 file_errors(fail) 113 ]) 114 -> http_reply_file(Path, 115 [ unsafe(true), 116 static_gzip(true) 117 ], Request) 118 ; http_404([], Request) 119 ) 120 ; memberchk(path(Path), Request), 121 throw(http_reply(forbidden(Path))) 122 ). 123serve_files_in_directory(_Alias, Request) :- 124 memberchk(path(Path), Request), 125 throw(http_reply(forbidden(Path)))
Serve files needed by modules from the server
This module provides an infrastructure for serving resource-files such as icons, JavaScript, CSS files, etc. The default configuration serves the HTTP locations
icons
,css
andjs
(see http_absolute_location/3)The location for these services can be changed by adding rules for location/3. Directories providing additional or alternative resources can be achieved by adding rules for file_search_path/2. */