diff options
author | Arun Isaac | 2022-07-05 01:46:26 +0530 |
---|---|---|
committer | Arun Isaac | 2022-07-05 01:48:05 +0530 |
commit | 9398c1869801f120927ca4c27188d43e2c0e4db2 (patch) | |
tree | dbf89e986b9b13e0f339d940529b6edccc249792 | |
parent | 2cfd5ea44f179aa5c0667d904e2771e8feffb5b6 (diff) | |
download | tissue-9398c1869801f120927ca4c27188d43e2c0e4db2.tar.gz tissue-9398c1869801f120927ca4c27188d43e2c0e4db2.tar.lz tissue-9398c1869801f120927ca4c27188d43e2c0e4db2.zip |
web: server: Serve static files too.
* tissue/web/server.scm: Import (srfi srfi-171) and (ice-9
filesystem).
(%mime-types): New variable.
(handler): Serve static files too.
-rw-r--r-- | tissue/web/server.scm | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/tissue/web/server.scm b/tissue/web/server.scm index d07e629..b5338b0 100644 --- a/tissue/web/server.scm +++ b/tissue/web/server.scm @@ -22,6 +22,8 @@ #:use-module (rnrs io ports) #:use-module (srfi srfi-1) #:use-module (srfi srfi-26) + #:use-module (srfi srfi-171) + #:use-module (ice-9 filesystem) #:use-module (ice-9 match) #:use-module (htmlprag) #:use-module (sxml simple) @@ -168,6 +170,18 @@ operators " (string-split query #\&)) '())) +(define %mime-types + '(("gif" image/gif) + ("html" text/html) + ("jpeg" image/jpeg) + ("jpg" image/jpeg) + ("js" text/javascript) + ("json" application/json) + ("png" image/png) + ("pdf" application/pdf) + ("svg" image/svg+xml) + ("txt" text/plain))) + (define (handler request body hosts state-directory) "Handle web REQUEST with BODY and return two values---the response headers and body. @@ -189,6 +203,7 @@ STATE-DIRECTORY." (repository-open (string-append state-directory "/" hostname "/repository")))) (cond + ;; Search page ((member path (list "/" "/search")) (let ((search-query (or (assoc-ref parameters "query") ""))) @@ -212,6 +227,29 @@ STATE-DIRECTORY." search-query (MSet-get-matches-estimated mset) (assq-ref host-parameters 'css))))))))) + ;; Static files + ((let ((file-path (string-append state-directory "/" hostname "/website" path))) + (and (file-exists? file-path) + ;; Check that the file really is within the document + ;; root. + (string-prefix? (string-append state-directory "/" hostname "/website/") + (canonicalize-path file-path)) + (canonicalize-path file-path))) + => (lambda (file-path) + (values `((content-type . ,(or (assoc-ref %mime-types (string-remove-prefix + "." (file-name-extension file-path))) + '(application/octet-stream)))) + ;; Return a procedure so that the file can be + ;; read out a little at a time instead of having + ;; to load it whole into memory. + (lambda (out) + (call-with-input-file file-path + (lambda (in) + (port-transduce (tmap (cut put-bytevector out <>)) + (const #t) + get-bytevector-some + in))))))) + ;; Not found (else (values (build-response #:code 404) (string-append "Resource not found: " |