1 ;; The Right-Awful Gemini Server
3 ;; This is a gemini server in the spirit of the
4 ;; scratchy gopher server. Just as for that server,
5 ;; rags uses runtime evaluation of embedded scheme
6 ;; expressions to provide dynamically generated content.
8 ;; See the readme for details.
18 (chicken process-context)
19 (chicken process-context posix)
20 matchable srfi-13 srfi-1
21 uri-common tcp6 openssl)
24 root-dir host port certfile keyfile uid gid)
27 '(("gmi" "text/gemini" "charset=utf-8")
28 ("txt" "text/plain" "charset=utf-8")
29 ("csv" "text/csv" "charset=utf-8")
30 ("html" "text/html" "charset=utf-8")
31 ("xml" "text/xml" "charset=utf-8")
32 ("pdf" "application/pdf")
33 ("zip" "application/zip")
37 ("mp3" "audio/mpeg")))
39 (define (process-request config request-line)
40 (let ((uri (uri-normalize-path-segments (absolute-uri request-line))))
42 ((not (eq? (uri-scheme uri) 'gemini))
43 (fail-permanent "Unsupported scheme."))
45 (fail-permanent "URL lacks host name."))
46 ((not (equal? (uri-host uri) (config-host config)))
47 (fail-permanent "Proxy requests forbidden."))
48 ((uri-path-relative? uri)
49 (fail-permanent "Path must be absolute."))
50 ((not (document-available? config uri))
51 (fail-permanent "Document not found."))
52 ((and (document-path-directory? config uri)
53 (uri-lacks-trailing-slash? uri))
54 (redirect-permanent (uri-with-trailing-slash uri)))
55 ((document-script? config uri)
56 (serve-script config uri))
58 (serve-document config uri)))))
60 (define (fail-permanent reason)
61 (print "50 " reason "\r"))
63 (define (redirect-permanent new-uri)
64 (print "30 " (uri->string new-uri) "\r"))
66 (define (serve-query prompt)
67 (print "10 " prompt "\r"))
69 (define (uri-lacks-trailing-slash? uri)
70 (not (string-null? (last (uri-path uri)))))
72 (define (uri-with-trailing-slash uri)
73 (update-uri uri path: (append (uri-path uri) '(""))))
75 (define (document-available? config uri)
76 (file-exists? (document-path config uri)))
78 (define (document-script? config uri)
79 (let ((path (document-path config uri)))
80 (and (file-exists? path)
81 (file-executable? path)
82 (equal? (pathname-extension path) "scm"))))
84 (define (document-path-directory? config uri)
85 (directory-exists? (document-path-raw config uri)))
87 (define (document-path-raw config uri)
88 (let* ((crumbs (reverse (cons (config-root-dir config) (cdr (uri-path uri))))))
89 (make-pathname (reverse (cdr crumbs)) (car crumbs))))
91 (define (document-path config uri)
92 (let* ((path (document-path-raw config uri)))
93 (if (directory-exists? path)
94 (make-pathname path "index.gmi")
97 (define (ext->mime ext)
98 (let* ((mime-detected (assoc ext file-types)))
99 (cdr (if mime-detected
101 (assoc "txt" file-types)))))
103 (define (serve-document-header mime)
104 (print "20 " (string-intersperse mime ";") "\r"))
106 (define (serve-document config uri)
107 (let* ((path (document-path config uri))
108 (ext (pathname-extension path))
109 (mime (ext->mime ext)))
110 (serve-document-header mime)
112 ((file-executable? path)
113 (serve-text-dynamic path)) ; Binary-files can also be generated here, but the source is dynamic text
114 ((string-prefix? "text/" (car mime))
115 (serve-text-plain path))
116 (else (serve-binary path)))))
118 (define (serve-text-plain path)
119 (with-input-from-file path
121 (let loop ((str (read-string)))
122 (unless (eof-object? str)
124 (loop (read-string)))))))
126 (define (serve-text-dynamic path)
127 (with-input-from-file path
129 (let loop ((c (peek-char)))
136 (serve-dynamic-element (read) (pathname-directory path))
139 (loop (peek-char))))))))
141 (define (serve-dynamic-element element working-directory)
144 (with-current-working-directory
149 (with-current-working-directory
152 (let-values (((in-port out-port id) (process command)))
153 (let ((string (read-string #f in-port)))
154 (unless (eof-object? string)
156 (close-input-port in-port)
157 (close-output-port out-port))))))
158 (else (error "Unknown element type."))))
160 (define (serve-script config uri)
161 ;; Scripts are responsible for the entire response, including header
162 (let ((path (document-path config uri)))
163 (apply (eval (with-input-from-file path read))
166 (define (with-current-working-directory directory thunk)
167 (let ((old-wd (current-directory))
171 (change-directory directory)
172 (set! result (thunk))
173 (change-directory old-wd)
176 (change-directory old-wd)
179 (define (run-server config)
180 (set-buffering-mode! (current-output-port) #:line)
181 (define listener (ssl-listen* port: (config-port config)
182 certificate: (config-certfile config)
183 private-key: (config-keyfile config)
186 (print "Host: '" (config-host config) "'\n"
187 "Port: '" (config-port config) "'\n"
188 "Root directory: '" (config-root-dir config) "'\n"
189 "Cert file: '" (config-certfile config) "'\n"
190 "Key file: '" (config-keyfile config) "'\n"
192 "Gemini server listening ...")
195 (server-loop listener config))
197 (define (drop-privs config)
198 (let ((uid (config-uid config))
199 (gid (config-gid config)))
200 (if gid ; Group first, since only root can switch groups.
201 (set! (current-group-id) gid))
203 (set! (current-user-id) uid))))
206 (define (server-loop listener config)
207 (let-values (((in-port out-port) (ssl-accept listener)))
208 (let-values (((local-ip remote-ip) (tcp-addresses (ssl-port->tcp-port in-port))))
209 (print "Accepted connection from " remote-ip
210 " on " (seconds->string))
212 (let ((request-line (read-line in-port)))
213 (print* "Serving request '" request-line "' ... ")
214 (with-output-to-port out-port
216 (process-request config request-line)))
219 (print-error-message o))))
220 (close-input-port in-port)
221 (close-output-port out-port))
222 (server-loop listener config))
225 (define (print-usage progname)
226 (let ((indent-str (make-string (string-length progname) #\space)))
228 progname " [-h/--help]\n"
229 progname " [-p/--port PORT] [-u/--user UID] [-g/--group GID]\n"
230 indent-str " server-root-dir hostname certfile keyfile")))
233 (let* ((progname (pathname-file (car (argv))))
234 (config (make-config #f #f 1965 #f #f #f #f)))
235 (if (null? (cdr (argv)))
236 (print-usage progname)
237 (let loop ((args (cdr (argv))))
238 (let ((this-arg (car args))
239 (rest-args (cdr args)))
240 (if (string-prefix? "-" this-arg)
242 ((or (equal? this-arg "-h")
243 (equal? this-arg "--help"))
244 (print-usage progname))
245 ((or (equal? this-arg "-p")
246 (equal? this-arg "--port"))
247 (config-port-set! config (string->number (car rest-args)))
248 (loop (cdr rest-args)))
249 ((or (equal? this-arg "-u")
250 (equal? this-arg "--user"))
251 (config-uid-set! config (string->number (car rest-args)))
252 (loop (cdr rest-args)))
253 ((or (equal? this-arg "-g")
254 (equal? this-arg "--group"))
255 (config-gid-set! config (string->number (car rest-args)))
256 (loop (cdr rest-args)))
258 (print-usage progname)))
260 ((root-dir host certfile keyfile)
261 (config-root-dir-set! config root-dir)
262 (config-host-set! config host)
263 (config-certfile-set! config certfile)
264 (config-keyfile-set! config keyfile)
267 (print "One or more invalid arguments.")
268 (print-usage progname)))))))))