(import (chicken io) (chicken file) (chicken pathname) (chicken condition) uri-common) (define SERVER-ROOT "public_gemini") (define SERVER-HOST "thelambdalab.xyz") (define file-types '(("gmi" "text/gemini; charset=utf8") ("txt" "text/plain; charset=utf8"))) (define (process-request request-line) (condition-case (let ((uri (uri-normalize-path-segments (absolute-uri request-line)))) (cond ((not (eq? (uri-scheme uri) 'gemini)) (fail-permanent "Unsupported scheme.")) ((not (uri-host uri)) (fail-permanent "URL lacks host name.")) ((not (equal? (uri-host uri) SERVER-HOST)) (fail-permanent "Proxy requests forbidden.")) ((uri-path-relative? uri) (fail-permanent "Path must be absolute.")) ((not (document-available? uri)) (fail-permanent "Document not found.")) (else (serve-document uri)))) (o (exn) (print o) (fail-permanent "Failed to parse URL.")))) (define (fail-permanent reason) (print "50 " reason "\r")) (define (document-available? uri) (print (document-path uri)) (file-exists? (document-path uri))) (define (document-path uri) (let* ((crumbs (reverse (cons SERVER-ROOT (cdr (uri-path uri))))) (path (make-pathname (reverse (cdr crumbs)) (car crumbs)))) (if (directory-exists? path) (make-pathname path "index.gmi") path))) (define (serve-document uri) (let ((path (document-path uri))) (print "20 Surprise!\r"))) (process-request "gemini://thelambdalab.xyz//")