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.
19 (chicken process-context)
20 (chicken process-context posix)
21 matchable srfi-13 srfi-1
22 uri-common tcp6 openssl)
25 root-dir port certfile keyfile uid gid)
28 '(("gmi" "text/gemini" "charset=utf-8")
29 ("txt" "text/plain" "charset=utf-8")
30 ("csv" "text/csv" "charset=utf-8")
31 ("html" "text/html" "charset=utf-8")
32 ("xml" "text/xml" "charset=utf-8")
33 ("pdf" "application/pdf")
34 ("zip" "application/zip")
38 ("mp3" "audio/mpeg")))
40 (define (process-request config request-line)
41 (let ((uri (uri-normalize-path-segments (absolute-uri request-line))))
43 ((not (eq? (uri-scheme uri) 'gemini))
44 (fail-permanent "Unsupported scheme."))
46 (fail-permanent "URL lacks host name."))
47 ((not (valid-hostname (uri-host uri)))
48 (fail-permanent "Invalid host name."))
49 ((not (existing-host config uri))
50 (fail-permanent "Proxy requests forbidden."))
51 ((uri-path-relative? uri)
52 (fail-permanent "Path must be absolute."))
53 ((not (document-available? config uri))
54 (fail-permanent "Document not found."))
55 ((and (document-path-directory? config uri)
56 (uri-lacks-trailing-slash? uri))
57 (redirect-permanent (uri-with-trailing-slash uri)))
58 ((document-script? config uri)
59 (serve-script config uri))
61 (serve-document config uri)))))
63 (define (fail-permanent reason)
64 (print "50 " reason "\r"))
66 (define (redirect-permanent new-uri)
67 (print "30 " (uri->string new-uri) "\r"))
69 (define (serve-query prompt)
70 (print "10 " prompt "\r"))
72 (define (uri-lacks-trailing-slash? uri)
73 (not (string-null? (last (uri-path uri)))))
75 (define (uri-with-trailing-slash uri)
76 (update-uri uri path: (append (uri-path uri) '(""))))
78 (define (valid-hostname name)
80 ((host-label-part '(+ (or alphanumeric #\- #\_)))
81 (domain-part `(: #\. ,host-label-part))
82 (hostname-regex `(: ,host-label-part (+ ,domain-part))))
83 (irregex-match? hostname-regex name)))
85 (define (existing-host config uri)
86 (directory-exists? (make-pathname (config-root-dir config) (uri-host uri))))
88 (define (document-available? config uri)
89 (file-exists? (document-path config uri)))
91 (define (document-script? config uri)
92 (let ((path (document-path config uri)))
93 (and (file-exists? path)
94 (file-executable? path)
95 (equal? (pathname-extension path) "scm"))))
97 (define (document-path-directory? config uri)
98 (directory-exists? (document-path-raw config uri)))
100 (define (document-path-raw config uri)
101 (let* ((crumbs (reverse (cons (config-root-dir config)
102 (cons (uri-host uri) (cdr (uri-path uri)))))))
103 (make-pathname (reverse (cdr crumbs)) (car crumbs))))
105 (define (document-path config uri)
106 (let* ((path (document-path-raw config uri)))
107 (if (directory-exists? path)
108 (make-pathname path "index.gmi")
111 (define (ext->mime ext)
112 (let* ((mime-detected (assoc ext file-types)))
113 (cdr (if mime-detected
115 (assoc "txt" file-types)))))
117 (define (serve-document-header mime)
118 (print "20 " (string-intersperse mime ";") "\r"))
120 (define (serve-document config uri)
121 (let* ((path (document-path config uri))
122 (ext (pathname-extension path))
123 (mime (ext->mime ext)))
124 (serve-document-header mime)
126 ((file-executable? path)
127 (serve-text-dynamic path)) ; Binary-files can also be generated here, but the source is dynamic text
128 ((string-prefix? "text/" (car mime))
129 (serve-text-plain path))
130 (else (serve-binary path)))))
132 (define (serve-text-plain path)
133 (with-input-from-file path
135 (let loop ((str (read-string)))
136 (unless (eof-object? str)
138 (loop (read-string)))))))
140 (define (serve-text-dynamic path)
141 (with-input-from-file path
143 (let loop ((c (peek-char)))
150 (serve-dynamic-element (read) (pathname-directory path))
153 (loop (peek-char))))))))
155 (define (serve-dynamic-element element working-directory)
158 (with-current-working-directory
163 (with-current-working-directory
166 (let-values (((in-port out-port id) (process command)))
167 (let ((string (read-string #f in-port)))
168 (unless (eof-object? string)
170 (close-input-port in-port)
171 (close-output-port out-port))))))
172 (else (error "Unknown element type."))))
174 (define (serve-script config uri)
175 ;; Scripts are responsible for the entire response, including header
176 (let* ((path (document-path config uri))
177 (proc (eval (with-input-from-file path read))))
178 (with-current-working-directory
179 (pathname-directory (document-path config uri))
181 (apply proc (list uri))))))
183 (define (with-current-working-directory directory thunk)
184 (let ((old-wd (current-directory))
188 (change-directory directory)
189 (set! result (thunk))
190 (change-directory old-wd)
193 (change-directory old-wd)
196 (define (run-server config)
197 (set-buffering-mode! (current-output-port) #:line)
198 (define listener (ssl-listen* port: (config-port config)
199 certificate: (config-certfile config)
200 private-key: (config-keyfile config)
204 "Port: '" (config-port config) "'\n"
205 "Root directory: '" (config-root-dir config) "'\n"
206 "Cert file: '" (config-certfile config) "'\n"
207 "Key file: '" (config-keyfile config) "'\n"
209 "Gemini server listening ...")
212 (server-loop listener config))
214 (define (drop-privs config)
215 (let ((uid (config-uid config))
216 (gid (config-gid config)))
217 (if gid ; Group first, since only root can switch groups.
218 (set! (current-group-id) gid))
220 (set! (current-user-id) uid))))
223 (define (server-loop listener config)
224 (let-values (((in-port out-port) (ssl-accept listener)))
225 (let-values (((local-ip remote-ip) (tcp-addresses (ssl-port->tcp-port in-port))))
226 (print "Accepted connection from " remote-ip
227 " on " (seconds->string))
229 (let ((request-line (read-line in-port)))
230 (print* "Serving request '" request-line "' ... ")
231 (with-output-to-port out-port
233 (process-request config request-line)))
236 (print-error-message o))))
237 (close-input-port in-port)
238 (close-output-port out-port))
239 (server-loop listener config))
242 (define (print-usage progname)
243 (let ((indent-str (make-string (string-length progname) #\space)))
245 progname " [-h/--help]\n"
246 progname " [-p/--port PORT] [-u/--user UID] [-g/--group GID]\n"
247 indent-str " server-root-dir certfile keyfile")))
250 (let* ((progname (pathname-file (car (argv))))
251 (config (make-config #f 1965 #f #f #f #f)))
252 (if (null? (command-line-arguments))
253 (print-usage progname)
254 (let loop ((args (command-line-arguments)))
255 (let ((this-arg (car args))
256 (rest-args (cdr args)))
257 (if (string-prefix? "-" this-arg)
259 ((or (equal? this-arg "-h")
260 (equal? this-arg "--help"))
261 (print-usage progname))
262 ((or (equal? this-arg "-p")
263 (equal? this-arg "--port"))
264 (config-port-set! config (string->number (car rest-args)))
265 (loop (cdr rest-args)))
266 ((or (equal? this-arg "-u")
267 (equal? this-arg "--user"))
268 (config-uid-set! config (string->number (car rest-args)))
269 (loop (cdr rest-args)))
270 ((or (equal? this-arg "-g")
271 (equal? this-arg "--group"))
272 (config-gid-set! config (string->number (car rest-args)))
273 (loop (cdr rest-args)))
275 (print-usage progname)))
277 ((root-dir certfile keyfile)
278 (config-root-dir-set! config root-dir)
279 (config-certfile-set! config certfile)
280 (config-keyfile-set! config keyfile)
283 (print "One or more invalid arguments.")
284 (print-usage progname)))))))))