feat: redirect response

This commit is contained in:
Arthur Khachaturov 2024-07-26 13:20:56 +03:00
parent be42b2ec17
commit e9dc49691a
No known key found for this signature in database
GPG key ID: CAC2B7EB6DF45D55
3 changed files with 12 additions and 3 deletions

View file

@ -65,7 +65,7 @@ http::get() { http::route "$1" "GET" "$2"; }
http::_static_file() {
local uri_path="${request_path#"${HTTP_REQUEST_GLOB%'**'}"}"
local filename="${HTTP_STATIC_FOLDER}/${uri_path}"
[[ -z "${uri_path}" || ! -f "${filename}" || "${filename}" == *".."* ]] && http::response 404 && return 1
[[ -z "${uri_path}" || ! -f "${filename}" || "${filename}" == *".."* ]] && http::response 400 && return 1
http::file "${filename}"
}

View file

@ -17,13 +17,12 @@ http::_parse_headers() {
http::_parse_body() { cat - >/dev/null; } # TODO
http::_route_request() {
[[ -z "${HTTP_SUPPORTED_METHODS[${request_method}]}" ]] && http::response 501 && return 1
[[ -z "${HTTP_SUPPORTED_METHODS[${request_method}]}" ]] && { http::response 501; return 1; }
local glob_endpoint handler path_found
for glob_endpoint in "${!http__all_routes[@]}"; do # FIXME O(n)
# shellcheck disable=SC2053
if [[ "${request_path}" == ${glob_endpoint} ]]; then
declare -rx HTTP_REQUEST_GLOB="${glob_endpoint}"
path_found=1
break
fi

View file

@ -68,6 +68,16 @@ http::file() {
}
# calle provides local -A response_headers
# $1 = url, $2 = status code (301/302, optional)
http::redirect() {
local status_code="${2:-302}"
[[ -z "$1" ]] && { http::response 500 && return 1; }
http::_response_base_headers "${status_code}"
response_headers["Location"]="$1"
http::_output_response_headers
}
# calle provides local -A response_headers
# $1 = filename
http::html() { response_headers["Content-Type"]="text/html" http::file "$@"; }