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)). 40 41/** <module> Serve files needed by modules from the server 42 43This module provides an infrastructure for serving resource-files such 44as icons, JavaScript, CSS files, etc. The default configuration serves 45the HTTP locations =icons=, =css= and =js= (see 46http_absolute_location/3) 47 48The location for these services can be changed by adding rules for 49http:location/3. Directories providing additional or alternative 50resources can be achieved by adding rules for user:file_search_path/2. 51*/ 52 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)]). 77 78%! serve_files_in_directory(+Alias, +Request) 79% 80% Serve files from the directory Alias from the path-info from 81% Request. This predicate is used together with 82% file_search_path/2. Note that multiple clauses for the same 83% file_search_path alias can be used to merge files from different 84% physical locations onto the same HTTP directory. Note that the 85% handler must be declared as =prefix=. Below is an example 86% serving images from http://<host>/img/... from the directory 87% =http/web/icons=. 88% 89% == 90% http:location(img, root(img), []). 91% user:file_search_path(icons, library('http/web/icons')). 92% 93% :- http_handler(img(.), serve_files_in_directory(icons), [prefix]). 94% == 95% 96% This predicate calls http_404/2 if the physical file cannot be 97% located. If the requested path-name is unsafe (i.e., points 98% outside the hierarchy defines by the file_search_path/2 99% declaration), this handlers returns a _403 Forbidden_ page. 100% 101% @see http_reply_file/3 102 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)))