diff options
author | Wojtek Kosior <koszko@koszko.org> | 2023-12-19 15:31:38 +0100 |
---|---|---|
committer | Wojtek Kosior <koszko@koszko.org> | 2023-12-19 15:31:38 +0100 |
commit | d8776e1ca00711b4153a827ed604eba55f7cd62b (patch) | |
tree | 304c9824bef2a958b620254675fd03cead5817b5 /src/guile | |
parent | aba77461ac0927285dfea4baf59b3bf3a9e6d2b8 (diff) | |
download | cantius-d8776e1ca00711b4153a827ed604eba55f7cd62b.tar.gz cantius-d8776e1ca00711b4153a827ed604eba55f7cd62b.zip |
Allow handlers to only return response body.
Diffstat (limited to 'src/guile')
-rw-r--r-- | src/guile/cantius.scm | 85 |
1 files changed, 48 insertions, 37 deletions
diff --git a/src/guile/cantius.scm b/src/guile/cantius.scm index 1f6ec34..fe8131a 100644 --- a/src/guile/cantius.scm +++ b/src/guile/cantius.scm @@ -317,45 +317,56 @@ (path (uri:split-and-decode-uri-path path-string)) (($* endpoint-ref-result path-tail endpoint parameters) (query-endset root-endset path (%catchall-ref-result)))) + (define (normalized-path-string) + (format #f "/~a" (uri:encode-and-join-uri-path path))) + + (define (redirect-path) + (if (%redirect/normalize-path?) + (normalized-path-string) + (uri:uri-path (request-uri request)))) + + (define (redirect-query) + (if (%redirect/remove-query?) + #f + (uri:uri-query uri))) + + (define (compute-response) + (if (or (and (%redirect/normalize-path?) + (regexp-exec %ugly-uri-path-regex path-string)) + (and (%redirect/remove-query?) + (uri:uri-query uri))) + (values (build-response* #:redirect-to (uri:build-relative-ref + #:path (redirect-path) + #:query (redirect-query))) + "Redirect...") + (let/ec escape + (with-exception-handler + (lambda (ex) + (define msg + (exception->msg ex)) + + (display msg (current-error-port)) + + (escape (build-response* #:code 500) + (string-append msg "\nBacktrace:\n\n" (bt-string)))) + (lambda () + (parameterize ((%current-path path) + (%current-path-string path-string)) + (start-stack 'cantius-request + (apply (endpoint-handler endpoint) request body + (or (and=> path-tail list) '()))))))))) + (with-parameters* (map car parameters) (map cdr parameters) (lambda () - (define (normalized-path-string) - (format #f "/~a" (uri:encode-and-join-uri-path path))) - - (define (redirect-path) - (if (%redirect/normalize-path?) - (normalized-path-string) - (uri:uri-path (request-uri request)))) - - (define (redirect-query) - (if (%redirect/remove-query?) - #f - (uri:uri-query uri))) - - (if (or (and (%redirect/normalize-path?) - (regexp-exec %ugly-uri-path-regex path-string)) - (and (%redirect/remove-query?) - (uri:uri-query uri))) - (values (build-response* #:redirect-to (uri:build-relative-ref - #:path (redirect-path) - #:query (redirect-query))) - "Redirect...") - (let/ec escape - (with-exception-handler - (lambda (ex) - (define msg - (exception->msg ex)) - - (display msg (current-error-port)) - - (escape (build-response* #:code 500) - (string-append msg "\nBacktrace:\n\n" (bt-string)))) - (lambda () - (parameterize ((%current-path path) - (%current-path-string path-string)) - (start-stack 'cantius-request - (apply (endpoint-handler endpoint) request body - (or (and=> path-tail list) '())))))))))))) + (match ((compose list compute-response)) + ((response-body) + (values (build-response*) response-body)) + ((response response-body) + (values response response-body)) + (something-else + (values (build-response* #:code 500) + (format #f "Invalid return values from handler:~%~a" + something-else)))))))) (define-public (run-cantius endset . server-args) (apply run-server (cut handler <> <> endset) server-args)) |