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)))
138 (error "Index file not found."))))
140 (define (serve-text-file selector config)
141 (let ((filename (make-pathname (config-root-dir config) selector)))
142 (if (legal-filename? filename config)
143 (with-input-from-file filename
149 (error "File not found." filename))))
151 (define (serve-binary-file selector config)
152 (let ((filename (make-pathname (config-root-dir config) selector)))
153 (if (legal-filename? filename config)
154 (with-input-from-file filename
156 (let loop ((b (read-byte)))
161 (loop (read-byte)))))))
162 (error "File not found." filename))))
164 (define (serve-query selector arguments config)
165 (let ((filename (make-pathname (config-root-dir config)
166 (string-translate* selector '(("%3f" . "?"))))))
167 (if (and (legal-filename? filename config)
168 (= (length arguments) 1))
169 (with-input-from-file filename
175 (apply (eval (read)) arguments))))))
176 (error "Invalid query." selector arguments))))
179 (define (serve-url selector config)
180 (let ((url (substring selector 4)))
182 "<html><head><title>Redirection</title>"
183 "<meta http-equiv=\"refresh\" content=\"10; URL='" url "'\" />"
185 "<p>If you are seeing this page, your gopher browser does not "
186 "properly support URL directory entries or cannot follow such "
188 "<p>If you are viewing this page using a web browser, you should "
189 "be redirected shortly. Otherwise, you can manually open the "
190 "the follwing url:\n"
192 "<a href=\"" url "\">" url "</a>\n"
198 (define (serve-info-records string)
202 (for-each (lambda (char)
203 (print* (if (eq? char #\tab)
207 (print "\tfake\tfake\t1\r"))
208 (string-split string "\n" #t)))
210 (define (serve-record record dir-selector config)
212 (('shell command) (serve-shell-command command dir-selector config))
213 (('eval expression) (serve-expression expression dir-selector config))
214 (('url display-string url)
215 (print #\h display-string "\tURL:" url
216 "\t" (config-host config)
217 "\t" (config-port config) "\r"))
218 ((type display-string selector host port)
219 (print type display-string "\t" selector "\t" host "\t" port "\r"))
220 ((type display-string selector host)
221 (serve-record (list type display-string selector host 70)
222 dir-selector config))
223 ((type display-string selector)
224 (serve-record (list type display-string
225 (make-pathname dir-selector selector)
226 (config-host config) (config-port config))
227 dir-selector config))
228 ((display-string selector)
229 (serve-record (list (infer-selector-type selector) display-string selector)
230 dir-selector config))
232 (serve-record (list (infer-selecto-type selector) selector)
233 dir-selector config))
234 (else (error "Unknown record type."))))
236 (define (serve-shell-command command dir-selector config)
240 (let-values (((in-port out-port id) (process command)))
241 (let ((string (read-string #f in-port)))
242 (if (and (not (eof-object? string))
243 (> (string-length string) 0))
244 (serve-info-records (string-chomp string "\n"))))))))
246 (define (serve-expression expression dir-selector config)
250 (serve-info-records (conc (eval expression))))))
255 (define (with-current-working-directory directory thunk)
256 (let ((old-wd (current-directory))
260 (change-directory directory)
261 (set! result (thunk))
262 (change-directory old-wd)
265 (change-directory old-wd)
268 (define (with-selector-dir selector config thunk)
269 (with-current-working-directory
270 (make-pathname (config-root-dir config)
271 (pathname-directory selector)) thunk))
275 (define (print-usage progname)
277 progname " -h/--help\n"
278 progname " [-n/--no-footer] gopher-root-dir server-hostname [server-port]\n"
280 "The -n option tells the server to not display a directory footer."))
283 (let* ((progname (car (argv)))
285 (config (make-config '() '() 70 #t)))
288 (equal? (car args) "-h")
289 (equal? (car args) "--help"))
290 (print-usage progname)
292 (if (or (equal? (car args) "-n")
293 (equal? (car args) "--no-footer"))
295 (config-display-footer-set! config #f)
296 (set! args (cdr args))))
297 (if (or (< (length args) 2)
299 (print-usage progname)
301 (config-root-dir-set! config (car args))
302 (config-host-set! config (cadr args))
303 (if (= (length args) 3)
304 (config-port-set! config (string->number (caddr args))))
305 (run-server config)))))))
310 ;; (run-server (make-config "gopher-root" "localhost" 70 #t)))