;; The Right-Awful Gemini Server
-;;
+;;
+;; This is a gemini server in the spirit of the
+;; scratchy gopher server. Just as for that server,
+;; rags uses runtime evaluation of embedded scheme
+;; expressions to provide dynamically generated content.
+;;
+;; See the readme for details.
(import (chicken io)
(chicken port)
(define file-types
'(("gmi" "text/gemini" "charset=utf-8")
("txt" "text/plain" "charset=utf-8")
- ("xml" "text/xml" "charset=utf-8")))
+ ("csv" "text/csv" "charset=utf-8")
+ ("html" "text/html" "charset=utf-8")
+ ("xml" "text/xml" "charset=utf-8")
+ ("pdf" "application/pdf")
+ ("zip" "application/zip")
+ ("jpg" "image/jpeg")
+ ("jpeg" "image/jpeg")
+ ("png" "image/png")
+ ("mp3" "audio/mpeg")))
(define (process-request config request-line)
(let ((uri (uri-normalize-path-segments (absolute-uri request-line))))
((and (document-path-directory? config uri)
(uri-lacks-trailing-slash? uri))
(redirect-permanent (uri-with-trailing-slash uri)))
+ ((document-script? config uri)
+ (serve-script config uri))
(else
(serve-document config uri)))))
(define (redirect-permanent new-uri)
(print "30 " (uri->string new-uri) "\r"))
+(define (serve-query prompt)
+ (print "10 " prompt "\r"))
+
(define (uri-lacks-trailing-slash? uri)
(not (string-null? (last (uri-path uri)))))
(define (document-available? config uri)
(file-exists? (document-path config uri)))
+(define (document-script? config uri)
+ (let ((path (document-path config uri)))
+ (and (file-exists? path)
+ (file-executable? path)
+ (equal? (pathname-extension path) "scm"))))
+
(define (document-path-directory? config uri)
(directory-exists? (document-path-raw config uri)))
(if (directory-exists? path)
(make-pathname path "index.gmi")
path)))
+
+(define (ext->mime ext)
+ (let* ((mime-detected (assoc ext file-types)))
+ (cdr (if mime-detected
+ mime-detected
+ (assoc "txt" file-types)))))
+
+(define (serve-document-header mime)
+ (print "20 " (string-intersperse mime ";") "\r"))
(define (serve-document config uri)
(let* ((path (document-path config uri))
(ext (pathname-extension path))
- (mime-detected (assoc ext file-types))
- (mime (if mime-detected mime-detected (assoc "txt" file-types)))
- (mime-type (cadr mime)))
- (print "20 " (string-intersperse (cdr mime) ";") "\r")
+ (mime (ext->mime ext)))
+ (serve-document-header mime)
(cond
((file-executable? path)
(serve-text-dynamic path)) ; Binary-files can also be generated here, but the source is dynamic text
- ((string-prefix? "text/" mime-type)
+ ((string-prefix? "text/" (car mime))
(serve-text-plain path))
(else (serve-binary path)))))
(close-output-port out-port))))))
(else (error "Unknown element type."))))
+(define (serve-script config uri)
+ ;; Scripts are responsible for the entire response, including header
+ (let* ((path (document-path config uri))
+ (proc (eval (with-input-from-file path read))))
+ (with-current-working-directory
+ (pathname-directory (document-path config uri))
+ (lambda ()
+ (apply proc (list uri))))))
+
(define (with-current-working-directory directory thunk)
(let ((old-wd (current-directory))
(result 'none))