File: demo_files.pl

package info (click to toggle)
swi-prolog 9.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 82,408 kB
  • sloc: ansic: 387,503; perl: 359,326; cpp: 6,613; lisp: 6,247; java: 5,540; sh: 3,147; javascript: 2,668; python: 1,900; ruby: 1,594; yacc: 845; makefile: 428; xml: 317; sed: 12; sql: 6
file content (50 lines) | stat: -rw-r--r-- 1,877 bytes parent folder | download | duplicates (4)
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
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/html_write)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_dirindex)).

:- http_handler(root(.), serve_files, [prefix]).

%!  server(+Port, +DirSpec) is det.
%
%   Start  the  server  at  port  Port,  serving  directories  below
%   DirSpec. DirSpec may contain ~ and $var.
%
%   This simple example defines  a   complete  web-server for static
%   pages. Note that more specific  handlers   than  the  bave (i.e.
%   using a longer path) have  priority   over  this handler and can
%   thus be used to add dynamic parts to your server.

server(Port, DirSpec) :-
    expand_file_name(DirSpec, [Dir]),
    assert(user:file_search_path(document_root, Dir)),
    http_server(http_dispatch, [port(Port)]).

%!  serve_files(Request)
%
%   Server a file or directory  according   to  the path_info field,
%   which contains the path *after* the http-location matched by the
%   handler. If the  handler  is   matched  *exactly*,  path_info is
%   missing.
%
%   http_safe_file/1 checks the path for attempts to escape from the
%   hierarchy defined by the =document_root= alias. We find the path
%   before calling one of the two reply functions because we want to
%   know whether we are dealing with a   directory  or a file. After
%   that, the path is absolute  and   we  must  pass unsafe(true) to
%   avoid the path-checker in the reply-functions complaining.

serve_files(Request) :-
    (   memberchk(path_info(PathInfo), Request)
    ->  true
    ;   PathInfo = './'
    ),
    http_safe_file(document_root(PathInfo), []),
    absolute_file_name(document_root(PathInfo), Path,
                       [ access(read)] ),
    (   exists_directory(Path)
    ->  http_reply_dirindex(Path, [unsafe(true)], Request)
    ;   http_reply_file(Path, [unsafe(true)], Request)
    ).