From 84b7fd1fc74558cb5148b925dc7104df24f4e054 Mon Sep 17 00:00:00 2001 From: Alexander Artemenko Date: Sat, 13 Jan 2024 15:41:16 +0000 Subject: [PATCH] Allow to change status code using REBLOCKS/RESPONE:STATUS-CODE function. --- src/doc/changelog.lisp | 9 +++++++++ src/doc/response.lisp | 11 ++++++++--- src/response.lisp | 25 ++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/doc/changelog.lisp b/src/doc/changelog.lisp index 0998600e..3178547b 100644 --- a/src/doc/changelog.lisp +++ b/src/doc/changelog.lisp @@ -34,6 +34,15 @@ "REBLOCKS/SESSION:INIT") :external-links (("Ultralisp" . "https://ultralisp.org")) :external-docs ("https://40ants.com/log4cl-extras/")) + (0.58.0 2024-01-13 + """ +Added +===== + +A new function REBLOCKS/RESPONSE:STATUS-CODE was added as a replacement to REBLOCKS/RESPONSE:GET-CODE. +The new function is setfable. Now you can change HTTP status code of the currently rendered page. +This is especially helpful when rendering \"Not found\" or error pages. +""") (0.57.0 2024-01-08 """ Added diff --git a/src/doc/response.lisp b/src/doc/response.lisp index 35f59c05..d00cece1 100644 --- a/src/doc/response.lisp +++ b/src/doc/response.lisp @@ -16,6 +16,7 @@ (defsection @response (:title "Response" :ignore-words ("URL" + "SETF" "AJAX" "HTTP")) (@best-practice section) @@ -47,10 +48,10 @@ (defsection @api (:title "API") (reblocks/response:add-header function) (reblocks/response:add-retpath-to function) - (reblocks/response:get-code function) + + (reblocks/response:status-code function) (reblocks/response:get-content function) (reblocks/response:get-content-type function) - (reblocks/response:get-custom-headers function) (reblocks/response:get-headers function) (reblocks/response:set-cookie function) (reblocks/response:cookies-to-set function) @@ -60,4 +61,8 @@ (reblocks/response:immediate-response condition) (reblocks/response:make-response function) (reblocks/response:make-uri function) - (reblocks/response:send-script function)) + (reblocks/response:send-script function) + + "# Deprecated" + (reblocks/response:get-code function) + (reblocks/response:get-custom-headers function)) diff --git a/src/response.lisp b/src/response.lisp index 9e246f0a..37114158 100644 --- a/src/response.lisp +++ b/src/response.lisp @@ -42,7 +42,8 @@ #:get-content-type #:add-retpath-to #:set-cookie - #:cookies-to-set)) + #:cookies-to-set + #:status-code)) (in-package #:reblocks/response) @@ -62,15 +63,37 @@ (defun get-custom-headers (&optional (response *response*)) + "Function GET-CUSTOM-HEADERS is deprecated. Use GET-HEADERS instead." (check-type response lack.response:response) + ;; TODO: remove this function after "2023-05-01" (log:warn "Function GET-CUSTOM-HEADERS is deprecated. Use GET-HEADERS instead.") (lack.response:response-headers response)) (defun get-code (&optional (response *response*)) + "Function GET-CODE is deprecated. Use STATUS-CODE instead." + ;; TODO: remove this function after "2023-05-01" + (log:warn "Function GET-CODE is deprecated. Use STATUS-CODE instead.") (lack.response:response-status response)) +(defun status-code (&optional (response *response*)) + "Returns a status code to be returned in response to the current request. + + You can use SETF to change the status code: + + ```lisp + (setf (reblocks/response:status-code) + 404) + ```" + (lack.response:response-status response)) + + +(defun (setf status-code) (value &optional (response *response*)) + (setf (lack.response:response-status response) + value)) + + (defun get-content (&optional (response *response*)) (lack.response:response-body response))