diff options
Diffstat (limited to 'prototypes/c3b2/web/static.scm')
-rw-r--r-- | prototypes/c3b2/web/static.scm | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/prototypes/c3b2/web/static.scm b/prototypes/c3b2/web/static.scm new file mode 100644 index 0000000..49c618d --- /dev/null +++ b/prototypes/c3b2/web/static.scm | |||
@@ -0,0 +1,45 @@ | |||
1 | ;;; Copyright © 2014 David Thompson <davet@gnu.org> | ||
2 | ;;; Copyright © 2015-2017 Amirouche Boubekki <amirouche@hypermove.net> | ||
3 | ;; | ||
4 | ;; This program is free software: you can redistribute it and/or modify | ||
5 | ;; it under the terms of the GNU General Public License as published by | ||
6 | ;; the Free Software Foundation, either version 3 of the License, or | ||
7 | ;; (at your option) any later version. | ||
8 | |||
9 | ;; This program is distributed in the hope that it will be useful, | ||
10 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | ;; GNU General Public License for more details. | ||
13 | |||
14 | ;; You should have received a copy of the GNU General Public License | ||
15 | ;; along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
16 | (define-module (web static)) | ||
17 | |||
18 | ;; stdlib | ||
19 | (use-modules (ice-9 binary-ports)) | ||
20 | |||
21 | ;; local | ||
22 | (use-modules (web mime-types)) | ||
23 | (use-modules (web helpers)) | ||
24 | |||
25 | ;;; | ||
26 | ;;; static assets rendering | ||
27 | ;;; | ||
28 | |||
29 | (define (valid? path) | ||
30 | (null? (filter (lambda (component) (equal? component "..")) path))) | ||
31 | |||
32 | (define (directory? filename) | ||
33 | (string=? filename (dirname filename))) | ||
34 | |||
35 | (define-public (render-static-asset path) | ||
36 | (if (valid? path) | ||
37 | ;; XXX: the requested file is translated into a filpath related to where | ||
38 | ;; the server is executed. | ||
39 | (let ((filename (string-join (cons* (getcwd) "static" path) | ||
40 | "/"))) | ||
41 | (if (and (file-exists? filename) (not (directory? filename))) | ||
42 | (values `((content-type ,(mime-type filename))) | ||
43 | (call-with-input-file filename get-bytevector-all)) | ||
44 | (not-found (string-join (cons "static" path) "/" 'prefix)))) | ||
45 | (forbidden))) | ||