13 (chicken process-context)
14 srfi-1 srfi-13 matchable)
17 ;; (use srfi-1 srfi-13 tcp posix matchable)
21 (define gopher-index-filename "index")
23 (define burrower-version "1.0.0")
25 (define burrower-footer
27 "--------------------------------------------------\n"
28 "This gopher hole was dug using Burrower v" burrower-version ".\n"
29 "Powered by Chicken Scheme!"))
33 ;; We don't yet use worker threads here to handle requests,
34 ;; the server just blocks until the first request is finished.
35 ;; While we should fix this, it's actually probably okay, as
36 ;; we genuinely don't expect a huge flood of gopher traffic. :-(
38 (define-record config root-dir host port display-footer)
40 (define (run-server config)
41 (set-buffering-mode! (current-output-port) #:line)
42 (print "Gopher server listening on port " (config-port config) " ...")
43 (let ((listener (tcp-listen (config-port config))))
45 (let-values (((in-port out-port) (tcp-accept listener)))
46 (let* ((line (read-line in-port))
47 (selector (string-trim-both line)))
48 (let-values (((local-ip remote-ip) (tcp-addresses in-port)))
49 (print "Accepted connection from " remote-ip
50 " on " (seconds->string))
53 (with-output-to-port out-port
55 (serve-selector (if (= (string-length selector) 0)
59 (print "... served selector '" selector "'. Closing connection."))
61 (print-error-message o out-port)
62 (print-error-message o)
63 (print "Error while attempting to serve selector " selector ".")))))
64 (close-input-port in-port)
65 (close-output-port out-port))
67 (tcp-close listener)))
70 ;;; Selector type inference
72 (define (true-for-one? predicate values)
75 (if (predicate (car values))
77 (true-for-one? predicate (cdr values)))))
79 (define (has-suffix? selector . suffixes)
80 (true-for-one? (lambda (suffix)
81 (string-suffix? suffix selector))
84 (define (has-prefix? selector . prefixes)
85 (true-for-one? (lambda (prefix)
86 (string-prefix? prefix selector))
89 (define (infer-selector-type selector)
90 (let ((l (string-downcase selector)))
92 ((or (= (string-length l) 0) (string-suffix? "/" l)) 1)
93 ((has-suffix? l ".txt" ".org" ".md") 0)
94 ((has-suffix? l ".png" ".jpg" ".gif" ".bmp" ".tif" ".tga") 'I)
95 ((has-suffix? l "?.scm" "%3f.scm") 7)
96 ((has-prefix? l "url:" "/url:") 'h)
100 ;;; Selector retrieval
102 (define (serve-selector raw-selector config)
103 (let* ((selector-list (string-split raw-selector "\t"))
104 (selector (car selector-list))
105 (arguments (cdr selector-list)))
106 (case (infer-selector-type selector)
107 ((1) (serve-directory selector config))
108 ((0) (serve-text-file selector config))
109 ((7) (serve-query selector arguments config))
110 ((h) (serve-url selector config))
111 (else (serve-binary-file selector config)))))
113 (define (legal-filename? filename config)
114 (and (string-prefix? (config-root-dir config)
115 (normalize-pathname filename))
116 (regular-file? filename)))
118 (define (serve-directory selector config)
119 (let ((filename (make-pathname (list (config-root-dir config) selector)
120 gopher-index-filename)))
121 (if (legal-filename? filename config)
123 (with-input-from-file filename
125 (let loop ((c (peek-char)))
132 (serve-record (read) selector config)
134 (serve-info-records (read-line)))
135 (loop (peek-char)))))))
136 (if (config-display-footer config)
137 (serve-info-records burrower-footer))
139 (error "Index file not found."))))
141 (define (serve-text-file selector config)
142 (let ((filename (make-pathname (config-root-dir config) selector)))
143 (if (legal-filename? filename config)
145 (with-input-from-file filename
152 (error "File not found." filename))))
154 (define (serve-binary-file selector config)
155 (let ((filename (make-pathname (config-root-dir config) selector)))
156 (if (legal-filename? filename config)
157 (with-input-from-file filename
159 (let loop ((b (read-byte)))
164 (loop (read-byte)))))))
165 (error "File not found." filename))))
167 (define (serve-query selector arguments config)
168 (let ((filename (make-pathname (config-root-dir config)
169 (string-translate* selector '(("%3f" . "?"))))))
170 (if (and (legal-filename? filename config)
171 (= (length arguments) 1))
172 (with-input-from-file filename
178 (apply (eval (read)) arguments))))))
179 (error "Invalid query." selector arguments))))
182 (define (serve-url selector config)
183 (let ((url (substring selector 4)))
185 "<html><head><title>Redirection</title>"
186 "<meta http-equiv=\"refresh\" content=\"10; URL='" url "'\" />"
188 "<p>If you are seeing this page, your gopher browser does not "
189 "properly support URL directory entries or cannot follow such "
191 "<p>If you are viewing this page using a web browser, you should "
192 "be redirected shortly. Otherwise, you can manually open the "
193 "the follwing url:\n"
195 "<a href=\"" url "\">" url "</a>\n"
201 (define (serve-info-records string)
205 (for-each (lambda (char)
206 (print* (if (eq? char #\tab)
210 (print "\tfake\tfake\t1\r"))
211 (string-split string "\n" #t)))
213 (define (serve-record record dir-selector config)
215 (('shell command) (serve-shell-command command dir-selector config))
216 (('eval expression) (serve-expression expression dir-selector config))
217 (('url display-string url)
218 (print #\h display-string "\tURL:" url
219 "\t" (config-host config)
220 "\t" (config-port config) "\r"))
221 ((type display-string selector host port)
222 (print type display-string "\t" selector "\t" host "\t" port "\r"))
223 ((type display-string selector host)
224 (serve-record (list type display-string selector host 70)
225 dir-selector config))
226 ((type display-string selector)
227 (serve-record (list type display-string
228 (make-pathname dir-selector selector)
229 (config-host config) (config-port config))
230 dir-selector config))
231 ((display-string selector)
232 (serve-record (list (infer-selector-type selector) display-string selector)
233 dir-selector config))
235 (serve-record (list (infer-selecto-type selector) selector)
236 dir-selector config))
237 (else (error "Unknown record type."))))
239 (define (serve-shell-command command dir-selector config)
243 (let-values (((in-port out-port id) (process command)))
244 (let ((string (read-string #f in-port)))
245 (if (and (not (eof-object? string))
246 (> (string-length string) 0))
247 (serve-info-records (string-chomp string "\n"))))))))
249 (define (serve-expression expression dir-selector config)
253 (serve-info-records (conc (eval expression))))))
258 (define (with-current-working-directory directory thunk)
259 (let ((old-wd (current-directory))
263 (change-directory directory)
264 (set! result (thunk))
265 (change-directory old-wd)
268 (change-directory old-wd)
271 (define (with-selector-dir selector config thunk)
272 (with-current-working-directory
273 (make-pathname (config-root-dir config)
274 (pathname-directory selector)) thunk))
278 (define (print-usage progname)
280 progname " -h/--help\n"
281 progname " [-n/--no-footer] gopher-root-dir server-hostname [server-port]\n"
283 "The -n option tells the server to not display a directory footer."))
286 (let* ((progname (car (argv)))
288 (config (make-config '() '() 70 #t)))
291 (equal? (car args) "-h")
292 (equal? (car args) "--help"))
293 (print-usage progname)
295 (if (or (equal? (car args) "-n")
296 (equal? (car args) "--no-footer"))
298 (config-display-footer-set! config #f)
299 (set! args (cdr args))))
300 (if (or (< (length args) 2)
302 (print-usage progname)
304 (config-root-dir-set! config (car args))
305 (config-host-set! config (cadr args))
306 (if (= (length args) 3)
307 (config-port-set! config (string->number (caddr args))))
308 (run-server config)))))))
313 (run-server (make-config "gopher-root" "localhost" 70 #t)))