;; See the readme for details.
(import (chicken io)
+ (chicken irregex)
(chicken port)
(chicken file)
(chicken string)
uri-common tcp6 openssl)
(define-record config
- root-dir host port certfile keyfile uid gid)
+ root-dir port certfile keyfile uid gid)
(define file-types
'(("gmi" "text/gemini" "charset=utf-8")
(fail-permanent "Unsupported scheme."))
((not (uri-host uri))
(fail-permanent "URL lacks host name."))
- ((not (equal? (uri-host uri) (config-host config)))
+ ((not (valid-hostname (uri-host uri)))
+ (fail-permanent "Invalid host name."))
+ ((not (existing-host config uri))
(fail-permanent "Proxy requests forbidden."))
((uri-path-relative? uri)
(fail-permanent "Path must be absolute."))
(define (uri-with-trailing-slash uri)
(update-uri uri path: (append (uri-path uri) '(""))))
+(define (valid-hostname name)
+ (let*
+ ((host-label-part '(+ (or alphanumeric #\- #\_)))
+ (domain-part `(: #\. ,host-label-part))
+ (hostname-regex `(: ,host-label-part (+ ,domain-part))))
+ (irregex-match? hostname-regex name)))
+
+(define (existing-host config uri)
+ (directory-exists? (make-pathname (config-root-dir config) (uri-host uri))))
+
(define (document-available? config uri)
(file-exists? (document-path config uri)))
(directory-exists? (document-path-raw config uri)))
(define (document-path-raw config uri)
- (let* ((crumbs (reverse (cons (config-root-dir config) (cdr (uri-path uri))))))
+ (let* ((crumbs (reverse (cons (config-root-dir config)
+ (cons (uri-host uri) (cdr (uri-path uri)))))))
(make-pathname (reverse (cdr crumbs)) (car crumbs))))
(define (document-path config uri)
private-key: (config-keyfile config)
protocol: 'tlsv12))
- (print "Host: '" (config-host config) "'\n"
+ (print
"Port: '" (config-port config) "'\n"
"Root directory: '" (config-root-dir config) "'\n"
"Cert file: '" (config-certfile config) "'\n"
(print "Usage:\n"
progname " [-h/--help]\n"
progname " [-p/--port PORT] [-u/--user UID] [-g/--group GID]\n"
- indent-str " server-root-dir hostname certfile keyfile")))
+ indent-str " server-root-dir certfile keyfile")))
(define (main)
(let* ((progname (pathname-file (car (argv))))
- (config (make-config #f #f 1965 #f #f #f #f)))
+ (config (make-config #f 1965 #f #f #f #f)))
(if (null? (command-line-arguments))
(print-usage progname)
(let loop ((args (command-line-arguments)))
(else
(print-usage progname)))
(match args
- ((root-dir host certfile keyfile)
+ ((root-dir certfile keyfile)
(config-root-dir-set! config root-dir)
- (config-host-set! config host)
(config-certfile-set! config certfile)
(config-keyfile-set! config keyfile)
(run-server config))