(chicken process)
(chicken process-context)
(chicken process-context posix)
- matchable srfi-13
+ matchable srfi-13 srfi-1
uri-common tcp6 openssl)
(define-record config
(fail-permanent "Path must be absolute."))
((not (document-available? config uri))
(fail-permanent "Document not found."))
+ ((and (document-path-directory? config uri)
+ (uri-lacks-trailing-slash? uri))
+ (redirect-permanent (uri-with-trailing-slash uri)))
(else
(serve-document config uri)))))
(define (fail-permanent reason)
(print "50 " reason "\r"))
+(define (redirect-permanent new-uri)
+ (print "30 " (uri->string new-uri) "\r"))
+
+(define (uri-lacks-trailing-slash? uri)
+ (not (string-null? (last (uri-path uri)))))
+
+(define (uri-with-trailing-slash uri)
+ (update-uri uri path: (append (uri-path uri) '(""))))
+
(define (document-available? config uri)
(file-exists? (document-path config uri)))
+(define (document-path-directory? 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))))))
+ (make-pathname (reverse (cdr crumbs)) (car crumbs))))
+
(define (document-path config uri)
- (let* ((crumbs (reverse (cons (config-root-dir config) (cdr (uri-path uri)))))
- (path (make-pathname (reverse (cdr crumbs)) (car crumbs))))
+ (let* ((path (document-path-raw config uri)))
(if (directory-exists? path)
(make-pathname path "index.gmi")
path)))