Finished adding sessions and updating examples
This commit is contained in:
parent
29d58016e0
commit
75b395ffa2
@ -12,7 +12,7 @@ Here is some leaked source code for my brand new Cat website. Do not steal!
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
source exprash.sh;
|
source exprash.sh;
|
||||||
|
|
||||||
redirectStdout 'log';
|
redirect_stdout 'log';
|
||||||
|
|
||||||
declare -A cats;
|
declare -A cats;
|
||||||
cats[calico]="Calico";
|
cats[calico]="Calico";
|
||||||
@ -50,11 +50,11 @@ get '/cats/*' && {
|
|||||||
printf '<h1>Error: Cannot find that cat</h1>\n' | send;
|
printf '<h1>Error: Cannot find that cat</h1>\n' | send;
|
||||||
}
|
}
|
||||||
|
|
||||||
getError '/cats/*' && {
|
get_error '/cats/*' && {
|
||||||
printf '<h1>%s</h1>' "$(errorMessage)" | send;
|
printf '<h1>%s</h1>' "$(get_error_message)" | send;
|
||||||
}
|
}
|
||||||
|
|
||||||
(use || useError) && {
|
(use || use_error) && {
|
||||||
printf '<h1>404</h1>' | send;
|
printf '<h1>404</h1>' | send;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
source exprash.sh;
|
source exprash.sh;
|
||||||
|
|
||||||
redirectStdout 'log';
|
redirect_stdout 'log';
|
||||||
|
|
||||||
declare -A cats;
|
declare -A cats;
|
||||||
cats[calico]="Calico";
|
cats[calico]="Calico";
|
||||||
@ -39,10 +39,10 @@ get '/cats/*' && {
|
|||||||
printf '<h1>Error: Cannot find that cat</h1>\n' | send;
|
printf '<h1>Error: Cannot find that cat</h1>\n' | send;
|
||||||
}
|
}
|
||||||
|
|
||||||
getError '/cats/*' && {
|
get_error '/cats/*' && {
|
||||||
printf '<h1>%s</h1>' "$(errorMessage)" | send;
|
printf '<h1>%s</h1>' "$(get_error_message)" | send;
|
||||||
}
|
}
|
||||||
|
|
||||||
(use || useError) && {
|
(use || use_error) && {
|
||||||
printf '<h1>404</h1>' | send;
|
printf '<h1>404</h1>' | send;
|
||||||
}
|
}
|
||||||
|
86
examples/login.sh
Normal file
86
examples/login.sh
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
source exprash.sh;
|
||||||
|
|
||||||
|
redirect_stdout 'log';
|
||||||
|
use_session
|
||||||
|
use_body
|
||||||
|
|
||||||
|
username='admin'
|
||||||
|
password='password'
|
||||||
|
|
||||||
|
# Authorization middleware
|
||||||
|
function is_authorized() {
|
||||||
|
if [ "$(session 'authorized')" != "1" ]; then
|
||||||
|
next 'unauthorized'
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
get '/' && {
|
||||||
|
if [ "$(session 'authorized')" == "1" ]; then
|
||||||
|
html="<h1>Welcome $username</h1>"
|
||||||
|
html+="<a href='admin'>Click Here For Secrets</a><br /><br />"
|
||||||
|
html+="<a href='logout'>Logout</a>"
|
||||||
|
else
|
||||||
|
html="<h1>Welcome</h1>"
|
||||||
|
html+="<p>You must login to learn secrets</p>"
|
||||||
|
html+="<a href='login'>Login</a>"
|
||||||
|
fi
|
||||||
|
printf '%s\n' "$html" | send
|
||||||
|
}
|
||||||
|
|
||||||
|
get '/admin' && is_authorized && {
|
||||||
|
html='<h1>Here are all of my secrets:</h1>'
|
||||||
|
html+="<ul><li>Rabbits are soft.</li></ul>"
|
||||||
|
html+="<a href='.'>Go Home</a>"
|
||||||
|
printf '%s' "$html" | send
|
||||||
|
}
|
||||||
|
|
||||||
|
get '/login' && {
|
||||||
|
html="<h1>Login:</h1>"
|
||||||
|
html+='<form method="POST" action="login">'
|
||||||
|
html+=' <input type="text" name="username" placeholder="username" />'
|
||||||
|
html+=' <input type="password" name="password" placeholder="password" />'
|
||||||
|
html+=' <input type="submit" value="login" />'
|
||||||
|
html+='</form>'
|
||||||
|
printf '%s\n' "$html" | send
|
||||||
|
}
|
||||||
|
|
||||||
|
get '/incorrect-password' && {
|
||||||
|
if [ "$(session 'authorized')" == "1" ]; then
|
||||||
|
redirect '.'
|
||||||
|
else
|
||||||
|
html="<h1>Incorrect Password</h1>"
|
||||||
|
html+="<p>Try again:</p>"
|
||||||
|
html+="<a href='login'>Login</a>"
|
||||||
|
printf '%s\n' "$html" | send
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
post '/login' && {
|
||||||
|
post_user=$(body 'username')
|
||||||
|
post_pass=$(body 'password')
|
||||||
|
if [ "$post_user" == "$username" ] && [ "$post_pass" == "$password" ]; then
|
||||||
|
session 'authorized' 1
|
||||||
|
redirect '.'
|
||||||
|
else
|
||||||
|
redirect 'incorrect-password'
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
get '/logout' && {
|
||||||
|
session 'authorized' 0
|
||||||
|
redirect '.'
|
||||||
|
}
|
||||||
|
|
||||||
|
(use || use_error) && {
|
||||||
|
if [ "$(get_error_message)" == "unauthorized" ]; then
|
||||||
|
html='<h1>Error: Access Denied</h1>'
|
||||||
|
html+='<a href='login'>Click here to login</a>'
|
||||||
|
printf '%s' "$html" | send
|
||||||
|
else
|
||||||
|
status '404'
|
||||||
|
printf '<h1>404</h1>' | send
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
334
src/exprash.sh
334
src/exprash.sh
@ -14,13 +14,13 @@ shopt -s lastpipe
|
|||||||
# ===============
|
# ===============
|
||||||
|
|
||||||
# $1: Log file for redirecting stdout to
|
# $1: Log file for redirecting stdout to
|
||||||
function redirectStdout() {
|
function redirect_stdout() {
|
||||||
local output="$1";
|
local output="$1";
|
||||||
[ -z "$output" ] && output="/dev/null";
|
[ -z "$output" ] && output="/dev/null";
|
||||||
|
|
||||||
exec 5<&1;
|
exec 5<&1;
|
||||||
_exprashRedirectStdout="$output";
|
_exprash_redirect_stdout="$output";
|
||||||
exec 1>>"$_exprashRedirectStdout";
|
exec 1>>"$_exprash_redirect_stdout";
|
||||||
printf '%s: %s\n' "${REQUEST_METHOD:-UNKNOWN}" "${PATH_INFO:-/}"
|
printf '%s: %s\n' "${REQUEST_METHOD:-UNKNOWN}" "${PATH_INFO:-/}"
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,23 +54,23 @@ function delete() {
|
|||||||
|
|
||||||
# $1: path
|
# $1: path
|
||||||
function all() {
|
function all() {
|
||||||
[ "$_exprashRouteHandled" -eq 1 ] && return 1;
|
[ "$_exprash_route_handled" -eq 1 ] && return 1;
|
||||||
[ -n "$_exprashErrorMessage" ] && return 1;
|
[ -n "$_exprash_error_message" ] && return 1;
|
||||||
|
|
||||||
# Reset params
|
# Reset params
|
||||||
_exprashParams=();
|
_exprash_params=();
|
||||||
|
|
||||||
_pathMatch "$1" _exprashParams || return 1;
|
_path_match "$1" _exprash_params || return 1;
|
||||||
_exprashRouteHandled=1;
|
_exprash_route_handled=1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
function use() {
|
function use() {
|
||||||
[ "$_exprashRouteHandled" -eq 1 ] && return 1;
|
[ "$_exprash_route_handled" -eq 1 ] && return 1;
|
||||||
[ -n "$_exprashErrorMessage" ] && return 1;
|
[ -n "$_exprash_error_message" ] && return 1;
|
||||||
|
|
||||||
_exprashRouteHandled=1;
|
_exprash_route_handled=1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,48 +79,48 @@ function use() {
|
|||||||
# =====================
|
# =====================
|
||||||
|
|
||||||
# $1: path
|
# $1: path
|
||||||
function getError() {
|
function get_error() {
|
||||||
[ "$REQUEST_METHOD" != "GET" ] && return 1;
|
[ "$REQUEST_METHOD" != "GET" ] && return 1;
|
||||||
allError "$1";
|
all_error "$1";
|
||||||
}
|
}
|
||||||
|
|
||||||
# $1: path
|
# $1: path
|
||||||
function postError() {
|
function post_error() {
|
||||||
[ "$REQUEST_METHOD" != "POST" ] && return 1;
|
[ "$REQUEST_METHOD" != "POST" ] && return 1;
|
||||||
allError "$1";
|
all_error "$1";
|
||||||
}
|
}
|
||||||
|
|
||||||
# $1: path
|
# $1: path
|
||||||
function putError() {
|
function put_error() {
|
||||||
[ "$REQUEST_METHOD" != "PUT" ] && return 1;
|
[ "$REQUEST_METHOD" != "PUT" ] && return 1;
|
||||||
allError "$1";
|
all_error "$1";
|
||||||
}
|
}
|
||||||
|
|
||||||
# $1: path
|
# $1: path
|
||||||
function deleteError() {
|
function delete_error() {
|
||||||
[ "$REQUEST_METHOD" != "DELETE" ] && return 1;
|
[ "$REQUEST_METHOD" != "DELETE" ] && return 1;
|
||||||
allError "$1";
|
all_error "$1";
|
||||||
}
|
}
|
||||||
|
|
||||||
# $1: path
|
# $1: path
|
||||||
function allError() {
|
function all_error() {
|
||||||
[ "$_exprashRouteHandled" -eq 1 ] && return 1;
|
[ "$_exprash_route_handled" -eq 1 ] && return 1;
|
||||||
[ -z "$_exprashErrorMessage" ] && return 1;
|
[ -z "$_exprash_error_message" ] && return 1;
|
||||||
|
|
||||||
# Reset params
|
# Reset params
|
||||||
_exprashParams=();
|
_exprash_params=();
|
||||||
|
|
||||||
_pathMatch "$1" _exprashParams || return 1;
|
_path_match "$1" _exprash_params || return 1;
|
||||||
_exprashRouteHandled=1;
|
_exprash_route_handled=1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
function useError() {
|
function use_error() {
|
||||||
[ "$_exprashRouteHandled" -eq 1 ] && return 1;
|
[ "$_exprash_route_handled" -eq 1 ] && return 1;
|
||||||
[ -z "$_exprashErrorMessage" ] && return 1;
|
[ -z "$_exprash_error_message" ] && return 1;
|
||||||
|
|
||||||
_exprashRouteHandled=1;
|
_exprash_route_handled=1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,20 +130,20 @@ function useError() {
|
|||||||
|
|
||||||
# $1 (optional): error message
|
# $1 (optional): error message
|
||||||
function next() {
|
function next() {
|
||||||
_exprashRouteHandled=0;
|
_exprash_route_handled=0;
|
||||||
[ -n "$1" ] && _exprashErrorMessage="$1";
|
[ -n "$1" ] && _exprash_error_message="$1";
|
||||||
}
|
}
|
||||||
|
|
||||||
# =============
|
# =============
|
||||||
# App Functions
|
# App Functions
|
||||||
# =============
|
# =============
|
||||||
|
|
||||||
function errorMessage() {
|
function get_error_message() {
|
||||||
printf '%s' "$_exprashErrorMessage";
|
printf '%s' "$_exprash_error_message";
|
||||||
}
|
}
|
||||||
|
|
||||||
function hasErrorMessage() {
|
function has_error_message() {
|
||||||
[ -n "$_exprashErrorMessage" ];
|
[ -n "$_exprash_error_message" ];
|
||||||
}
|
}
|
||||||
|
|
||||||
# =================
|
# =================
|
||||||
@ -152,59 +152,60 @@ function hasErrorMessage() {
|
|||||||
|
|
||||||
# $1: param name
|
# $1: param name
|
||||||
function param() {
|
function param() {
|
||||||
printf '%s\n' "${_exprashParams[$1]}";
|
printf '%s\n' "${_exprash_params[$1]}";
|
||||||
}
|
}
|
||||||
|
|
||||||
function hasParam() {
|
function has_param() {
|
||||||
[[ -v "_exprashParams[$1]" ]];
|
[[ -v "_exprash_params[$1]" ]];
|
||||||
}
|
}
|
||||||
|
|
||||||
# $1: key
|
# $1: key
|
||||||
# $2: (optional) index for accessing array parameters
|
# $2: (optional) index for accessing array parameters
|
||||||
function query() {
|
function query() {
|
||||||
_multiGet _exprashQuery "$1" "$2"
|
_multi_get _exprash_query "$1" "$2"
|
||||||
}
|
}
|
||||||
# $1: key
|
# $1: key
|
||||||
function hasQuery() {
|
function has_query() {
|
||||||
_multiHas _exprashQuery "$1"
|
_multi_has _exprash_query "$1"
|
||||||
}
|
}
|
||||||
# $1: key
|
# $1: key
|
||||||
function lenQuery() {
|
function len_query() {
|
||||||
_multiLen _exprashQuery "$1"
|
_multi_len _exprash_query "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Call this function to parse URLencoded request bodies
|
# Call this function to parse URLencoded request bodies
|
||||||
function useBody() {
|
function use_body() {
|
||||||
if [[ "${HTTP_CONTENT_TYPE,,}" == "application/x-www-form-urlencoded" ]]; then
|
if [[ "${HTTP_CONTENT_TYPE,,}" == "application/x-www-form-urlencoded" ]];
|
||||||
_parseUrlEncoded _exprashBody
|
then
|
||||||
|
_parse_url_encoded _exprash_body
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
# $1: key
|
# $1: key
|
||||||
# $2: (optional) index for accessing array parameters
|
# $2: (optional) index for accessing array parameters
|
||||||
function body() {
|
function body() {
|
||||||
_multiGet _exprashBody "$1" "$2"
|
_multi_get _exprash_body "$1" "$2"
|
||||||
}
|
}
|
||||||
# $1: key
|
# $1: key
|
||||||
function hasBody() {
|
function has_body() {
|
||||||
_multiHas _exprashBody "$1"
|
_multi_has _exprash_body "$1"
|
||||||
}
|
}
|
||||||
# $1: key
|
# $1: key
|
||||||
function lenBody() {
|
function len_body() {
|
||||||
_multiLen _exprashBody "$1"
|
_multi_len _exprash_body "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
# $1: key
|
# $1: key
|
||||||
function cookie() {
|
function cookie() {
|
||||||
printf '%s' "${_exprashCookies[$1]}"
|
printf '%s' "${_exprash_cookies[$1]}"
|
||||||
}
|
}
|
||||||
# $1: key
|
# $1: key
|
||||||
function hasCookie() {
|
function has_cookie() {
|
||||||
[[ -v "_exprashCookies[$1]" ]]
|
[[ -v "_exprash_cookies[$1]" ]]
|
||||||
}
|
}
|
||||||
# $1: key
|
# $1: key
|
||||||
# $2: value
|
# $2: value
|
||||||
function setCookie() {
|
function set_cookie() {
|
||||||
_exprashSetCookies["$1"]="$2"
|
_exprash_set_cookies["$1"]="$2"
|
||||||
}
|
}
|
||||||
|
|
||||||
# ==================
|
# ==================
|
||||||
@ -212,16 +213,16 @@ function setCookie() {
|
|||||||
# ==================
|
# ==================
|
||||||
|
|
||||||
function send() {
|
function send() {
|
||||||
if [ "$_exprashHeadersSent" -eq 0 ]; then
|
if [ "$_exprash_headers_sent" -eq 0 ]; then
|
||||||
sendHeaders
|
send_headers
|
||||||
_exprashHeadersSent=1
|
_exprash_headers_sent=1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
_sendRaw
|
_send_raw
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendJson() {
|
function send_json() {
|
||||||
setHeader 'Content-Type' 'application/json'
|
set_header 'Content-Type' 'application/json'
|
||||||
send
|
send
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -233,31 +234,34 @@ function redirect() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
status '302'
|
status '302'
|
||||||
setHeader 'Location' "$path"
|
set_header 'Location' "$path"
|
||||||
printf 'Redirecting to: %s' "$path" | send
|
printf 'Redirecting to: %s' "$path" | send
|
||||||
}
|
}
|
||||||
|
|
||||||
# $1: satus code
|
# $1: satus code
|
||||||
function status() {
|
function status() {
|
||||||
setHeader "Status" "$1"
|
set_header "Status" "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
# $1: Header Name
|
# $1: Header Name
|
||||||
# $2: Header Value
|
# $2: Header Value
|
||||||
function setHeader() {
|
function set_header() {
|
||||||
_exprashHeaders["$1"]="$2"
|
_exprash_headers["$1"]="$2"
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendHeaders() {
|
function send_headers() {
|
||||||
printf '%s\n' "Content-Type: ${_exprashHeaders['Content-Type']}" | _sendRaw
|
printf '%s\n' "Content-Type: ${_exprash_headers['Content-Type']}" | \
|
||||||
for key in "${!_exprashHeaders[@]}"; do
|
_send_raw
|
||||||
|
for key in "${!_exprash_headers[@]}"; do
|
||||||
[ "$key" == 'Content-Type' ] && continue
|
[ "$key" == 'Content-Type' ] && continue
|
||||||
printf '%s\n' "${key}: ${_exprashHeaders[$key]}" | _sendRaw
|
printf '%s\n' "${key}: ${_exprash_headers[$key]}" | \
|
||||||
|
_send_raw
|
||||||
done;
|
done;
|
||||||
for key in "${!_exprashSetCookies[@]}"; do
|
for key in "${!_exprash_set_cookies[@]}"; do
|
||||||
printf '%s\n' "Set-Cookie: ${key}=${_exprashSetCookies[$key]}" | _sendRaw
|
printf '%s\n' "Set-Cookie: ${key}=${_exprash_set_cookies[$key]}" | \
|
||||||
|
_send_raw
|
||||||
done;
|
done;
|
||||||
printf '\n' | _sendRaw
|
printf '\n' | _send_raw
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -267,7 +271,7 @@ function sendHeaders() {
|
|||||||
|
|
||||||
# Call this function to automatically manage sessions
|
# Call this function to automatically manage sessions
|
||||||
# $1: (optional) session dir, defaults to "session" in the current directory
|
# $1: (optional) session dir, defaults to "session" in the current directory
|
||||||
function useSession() {
|
function use_session() {
|
||||||
local session_dir=$1
|
local session_dir=$1
|
||||||
|
|
||||||
# Create default session directory
|
# Create default session directory
|
||||||
@ -277,10 +281,10 @@ function useSession() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Setup session globals
|
# Setup session globals
|
||||||
_exprashUseSession=1
|
_exprash_use_session=1
|
||||||
_exprashSessionDir=$session_dir
|
_exprash_session_dir=$session_dir
|
||||||
|
|
||||||
_loadSession || _createSession || return 1
|
_load_session || _create_session || return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# Set a session variable
|
# Set a session variable
|
||||||
@ -290,9 +294,9 @@ function session() {
|
|||||||
local name="$1"
|
local name="$1"
|
||||||
if [[ -v 2 ]]; then
|
if [[ -v 2 ]]; then
|
||||||
local value="$2"
|
local value="$2"
|
||||||
_exprashSession["$name"]="$value"
|
_exprash_session["$name"]="$value"
|
||||||
else
|
else
|
||||||
printf '%s' "${_exprashSession["$name"]}"
|
printf '%s' "${_exprash_session["$name"]}"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -300,21 +304,21 @@ function session() {
|
|||||||
# Internals
|
# Internals
|
||||||
# =========
|
# =========
|
||||||
|
|
||||||
function _sendRaw() {
|
function _send_raw() {
|
||||||
[ -n "$_exprashRedirectStdout" ] && exec >&5;
|
[ -n "$_exprash_redirect_stdout" ] && exec >&5;
|
||||||
cat;
|
cat;
|
||||||
[ -n "$_exprashRedirectStdout" ] && exec 1>>"$_exprashRedirectStdout";
|
[ -n "$_exprash_redirect_stdout" ] && exec 1>>"$_exprash_redirect_stdout";
|
||||||
}
|
}
|
||||||
|
|
||||||
# $1: path
|
# $1: path
|
||||||
# $2: (nameref) array
|
# $2: (nameref) array
|
||||||
function _pathToArray() {
|
function _path_to_array() {
|
||||||
readarray -t "$2" < <(printf '%s\n' "$1" | tr '/' '\n' | grep .);
|
readarray -t "$2" < <(printf '%s\n' "$1" | tr '/' '\n' | grep .);
|
||||||
}
|
}
|
||||||
|
|
||||||
# $1: route
|
# $1: route
|
||||||
# $2: (nameref) associative array for params
|
# $2: (nameref) associative array for params
|
||||||
function _pathMatch() {
|
function _path_match() {
|
||||||
[ -z ${PATH_INFO+x} ] && return 1;
|
[ -z ${PATH_INFO+x} ] && return 1;
|
||||||
[ -z ${1+x} ] && return 1;
|
[ -z ${1+x} ] && return 1;
|
||||||
|
|
||||||
@ -322,33 +326,33 @@ function _pathMatch() {
|
|||||||
local route="$1";
|
local route="$1";
|
||||||
|
|
||||||
# Params associative array
|
# Params associative array
|
||||||
local -n routeParams="$2";
|
local -n route_params="$2";
|
||||||
|
|
||||||
local pathArr;
|
local path_arr;
|
||||||
_pathToArray "$path" pathArr;
|
_path_to_array "$path" path_arr;
|
||||||
|
|
||||||
local routeArr;
|
local route_arr;
|
||||||
_pathToArray "$route" routeArr;
|
_path_to_array "$route" route_arr;
|
||||||
|
|
||||||
# Get max path length
|
# Get max path length
|
||||||
local routeLen=${#routeArr[@]};
|
local route_len=${#route_arr[@]};
|
||||||
local pathLen=${#pathArr[@]};
|
local path_len=${#path_arr[@]};
|
||||||
local maxLen=$(( routeLen >= pathLen ? routeLen : pathLen ));
|
local maxLen=$(( route_len >= path_len ? route_len : path_len ));
|
||||||
|
|
||||||
for ((i=0; i<maxLen; i++)); do
|
for ((i=0; i<maxLen; i++)); do
|
||||||
local routeComponent="${routeArr[$i]}";
|
local route_component="${route_arr[$i]}";
|
||||||
local pathComponent="${pathArr[$i]}";
|
local path_component="${path_arr[$i]}";
|
||||||
|
|
||||||
# If route component starts with ":"
|
# If route component starts with ":"
|
||||||
if [[ "$routeComponent" == :* ]] && [ -n "$pathComponent" ]; then
|
if [[ "$route_component" == :* ]] && [ -n "$path_component" ]; then
|
||||||
routeParams["${routeComponent:1}"]="$pathComponent";
|
route_params["${route_component:1}"]="$path_component";
|
||||||
elif [[ "$routeComponent" == '*' ]] && [ -n "$pathComponent" ]; then
|
elif [[ "$route_component" == '*' ]] && [ -n "$path_component" ]; then
|
||||||
continue;
|
continue;
|
||||||
elif [[ "$routeComponent" == '**' ]] && [ -n "$pathComponent" ]; then
|
elif [[ "$route_component" == '**' ]] && [ -n "$path_component" ]; then
|
||||||
break;
|
break;
|
||||||
else
|
else
|
||||||
# Confirm paths match
|
# Confirm paths match
|
||||||
[ "$routeComponent" != "$pathComponent" ] && return 1;
|
[ "$route_component" != "$path_component" ] && return 1;
|
||||||
fi;
|
fi;
|
||||||
done;
|
done;
|
||||||
|
|
||||||
@ -362,24 +366,24 @@ function _pathMatch() {
|
|||||||
# $1: (nameref) associative array
|
# $1: (nameref) associative array
|
||||||
# $2: key
|
# $2: key
|
||||||
# $3: value
|
# $3: value
|
||||||
function _multiAdd() {
|
function _multi_add() {
|
||||||
local -n multiArr="$1"
|
local -n multi_arr="$1"
|
||||||
local key="$2"
|
local key="$2"
|
||||||
local value="$3"
|
local value="$3"
|
||||||
local i=0
|
local i=0
|
||||||
while [[ -v "multiArr[$i,$key]" ]]; do
|
while [[ -v "multi_arr[$i,$key]" ]]; do
|
||||||
(( i++ ))
|
(( i++ ))
|
||||||
done
|
done
|
||||||
multiArr["$i,$key"]="$value"
|
multi_arr["$i,$key"]="$value"
|
||||||
}
|
}
|
||||||
|
|
||||||
# $1: (nameref) associative array
|
# $1: (nameref) associative array
|
||||||
# $2: key
|
# $2: key
|
||||||
function _multiLen() {
|
function _multi_len() {
|
||||||
local -n multiArr="$1"
|
local -n multi_arr="$1"
|
||||||
local key="$2"
|
local key="$2"
|
||||||
local i=0
|
local i=0
|
||||||
while [[ -v "multiArr[$i,$key]" ]]; do
|
while [[ -v "multi_arr[$i,$key]" ]]; do
|
||||||
(( i++ ))
|
(( i++ ))
|
||||||
done
|
done
|
||||||
printf '%s' "$i"
|
printf '%s' "$i"
|
||||||
@ -387,20 +391,20 @@ function _multiLen() {
|
|||||||
|
|
||||||
# $1: (nameref) associative array
|
# $1: (nameref) associative array
|
||||||
# $2: key
|
# $2: key
|
||||||
function _multiHas() {
|
function _multi_has() {
|
||||||
local -n multiArr="$1"
|
local -n multi_arr="$1"
|
||||||
local key="$2"
|
local key="$2"
|
||||||
[[ -v "multiArr[0,$key]" ]]
|
[[ -v "multi_arr[0,$key]" ]]
|
||||||
}
|
}
|
||||||
|
|
||||||
# $1: (nameref) associative array
|
# $1: (nameref) associative array
|
||||||
# $2: key
|
# $2: key
|
||||||
# $3: index
|
# $3: index
|
||||||
function _multiGet() {
|
function _multi_get() {
|
||||||
local -n multiArr="$1"
|
local -n multi_arr="$1"
|
||||||
local key="$2"
|
local key="$2"
|
||||||
local i="${3:-0}";
|
local i="${3:-0}";
|
||||||
printf '%s' "${multiArr[$i,$key]}"
|
printf '%s' "${multi_arr[$i,$key]}"
|
||||||
}
|
}
|
||||||
|
|
||||||
# ==================
|
# ==================
|
||||||
@ -408,24 +412,24 @@ function _multiGet() {
|
|||||||
# ==================
|
# ==================
|
||||||
|
|
||||||
# $1 | stdin: urlencoded string
|
# $1 | stdin: urlencoded string
|
||||||
decodeUri () {
|
decode_uri () {
|
||||||
local input_str="${1-"$(< /dev/stdin)"}"
|
local input_str="${1-"$(cat)"}"
|
||||||
input_str="${input_str//+/ }";
|
input_str="${input_str//+/ }";
|
||||||
echo -e "${input_str//%/\\x}";
|
echo -e "${input_str//%/\\x}";
|
||||||
}
|
}
|
||||||
|
|
||||||
# $1: (nameref) multi associative array
|
# $1: (nameref) multi associative array
|
||||||
# $2 | stdin: url encoded data
|
# $2 | stdin: url encoded data
|
||||||
function _parseUrlEncoded() {
|
function _parse_url_encoded() {
|
||||||
local -n parsedArr="$1"
|
local -n parsedArr="$1"
|
||||||
local url_encoded_str="${2-"$(< /dev/stdin)"}"
|
local url_encoded_str="${2-"$(cat)"}"
|
||||||
local pair name value
|
local pair name value
|
||||||
|
|
||||||
while IFS= read -d '&' -r pair || [ "$pair" ]; do
|
while IFS= read -d '&' -r pair || [ "$pair" ]; do
|
||||||
name=$(decodeUri "${pair%%=*}")
|
name=$(decode_uri "${pair%%=*}")
|
||||||
value=$(decodeUri "${pair#*=}")
|
value=$(decode_uri "${pair#*=}")
|
||||||
if [ -n "$name" ]; then
|
if [ -n "$name" ]; then
|
||||||
_multiAdd parsedArr "$name" "$value"
|
_multi_add parsedArr "$name" "$value"
|
||||||
fi;
|
fi;
|
||||||
done <<< "$url_encoded_str"
|
done <<< "$url_encoded_str"
|
||||||
}
|
}
|
||||||
@ -436,15 +440,15 @@ function _parseUrlEncoded() {
|
|||||||
|
|
||||||
# $1: (nameref) associative array
|
# $1: (nameref) associative array
|
||||||
# $2 | stdin: cookie string to parse
|
# $2 | stdin: cookie string to parse
|
||||||
function _parseCookies() {
|
function _parse_cookies() {
|
||||||
local -n parsedArr="$1"
|
local -n parsedArr="$1"
|
||||||
local cookie_str="${2-"$(< /dev/stdin)"}"
|
local cookie_str="${2-"$(< /dev/stdin)"}"
|
||||||
|
|
||||||
local pair name value
|
local pair name value
|
||||||
|
|
||||||
while IFS= read -d ';' -r pair || [ "$pair" ]; do
|
while IFS= read -d ';' -r pair || [ "$pair" ]; do
|
||||||
name="$(_trim "${pair%%=*}" | decodeUri)"
|
name="$(_trim "${pair%%=*}" | decode_uri)"
|
||||||
value="$(_trim "${pair#*=}" | decodeUri)"
|
value="$(_trim "${pair#*=}" | decode_uri)"
|
||||||
if [ -n "$name" ]; then
|
if [ -n "$name" ]; then
|
||||||
parsedArr["$name"]="$value"
|
parsedArr["$name"]="$value"
|
||||||
fi
|
fi
|
||||||
@ -455,29 +459,29 @@ function _parseCookies() {
|
|||||||
# Session
|
# Session
|
||||||
# =======
|
# =======
|
||||||
|
|
||||||
function _loadSession() {
|
function _load_session() {
|
||||||
[ "$_exprashUseSession" -eq 1 ] || return 1
|
[ "$_exprash_use_session" -eq 1 ] || return 1
|
||||||
[ -n "$_exprashSessionDir" ] || return 1
|
[ -n "$_exprash_session_dir" ] || return 1
|
||||||
hasCookie "$_exprashSessionCookieName" || return 1
|
has_cookie "$_exprash_session_cookie_name" || return 1
|
||||||
|
|
||||||
local session_id
|
local session_id
|
||||||
session_id="$(cookie "$_exprashSessionCookieName")"
|
session_id="$(cookie "$_exprash_session_cookie_name")"
|
||||||
local session_file="${_exprashSessionDir%/}/$session_id.session"
|
local session_file="${_exprash_session_dir%/}/$session_id.session"
|
||||||
|
|
||||||
# shellcheck disable=SC1090
|
# shellcheck disable=SC1090
|
||||||
source "$session_file" || return 1
|
source "$session_file" || return 1
|
||||||
|
|
||||||
# Set globals
|
# Set globals
|
||||||
_exprashSessionId=$session_id
|
_exprash_sessionId=$session_id
|
||||||
}
|
}
|
||||||
|
|
||||||
function _createSession() {
|
function _create_session() {
|
||||||
[ "$_exprashUseSession" -eq 1 ] || return 1
|
[ "$_exprash_use_session" -eq 1 ] || return 1
|
||||||
[ -n "$_exprashSessionDir" ] || return 1
|
[ -n "$_exprash_session_dir" ] || return 1
|
||||||
|
|
||||||
# mktemp args
|
# mktemp args
|
||||||
local args=()
|
local args=()
|
||||||
args+=('-p' "$_exprashSessionDir")
|
args+=('-p' "$_exprash_session_dir")
|
||||||
args+=("$(printf 'X%.0s' {1..32}).session")
|
args+=("$(printf 'X%.0s' {1..32}).session")
|
||||||
|
|
||||||
local session_file
|
local session_file
|
||||||
@ -486,21 +490,21 @@ function _createSession() {
|
|||||||
local session_id=${session_file_name%%.*}
|
local session_id=${session_file_name%%.*}
|
||||||
|
|
||||||
# Set cookie
|
# Set cookie
|
||||||
setCookie "$_exprashSessionCookieName" "$session_id"
|
set_cookie "$_exprash_session_cookie_name" "$session_id"
|
||||||
|
|
||||||
# Set globals
|
# Set globals
|
||||||
_exprashSessionId=$session_id
|
_exprash_sessionId=$session_id
|
||||||
_exprashSession=()
|
_exprash_session=()
|
||||||
}
|
}
|
||||||
|
|
||||||
function _saveSession() {
|
function _save_session() {
|
||||||
[ "$_exprashUseSession" -eq 1 ] || return 1
|
[ "$_exprash_use_session" -eq 1 ] || return 1
|
||||||
[ -n "$_exprashSessionDir" ] || return 1
|
[ -n "$_exprash_session_dir" ] || return 1
|
||||||
[ -n "$_exprashSessionId" ] || return 1
|
[ -n "$_exprash_sessionId" ] || return 1
|
||||||
|
|
||||||
local session_file="${_exprashSessionDir%/}/${_exprashSessionId}.session"
|
local session_file="${_exprash_session_dir%/}/${_exprash_sessionId}.session"
|
||||||
|
|
||||||
declare -p _exprashSession | sed '1 s/\([^-]*-\)/\1g/' > "$session_file"
|
declare -p _exprash_session | sed '1 s/\([^-]*-\)/\1g/' > "$session_file"
|
||||||
}
|
}
|
||||||
|
|
||||||
# =================
|
# =================
|
||||||
@ -523,44 +527,44 @@ function _trim() {
|
|||||||
# Shutdown
|
# Shutdown
|
||||||
# ========
|
# ========
|
||||||
|
|
||||||
function _exprashShutdown() {
|
function _exprash_shutdown() {
|
||||||
_saveSession
|
_save_session
|
||||||
}
|
}
|
||||||
|
|
||||||
# ==============
|
# ==============
|
||||||
# Initialization
|
# Initialization
|
||||||
# ==============
|
# ==============
|
||||||
function _exprashInit() {
|
function _exprash_init() {
|
||||||
_exprashRedirectStdout=''
|
_exprash_redirect_stdout=''
|
||||||
|
|
||||||
declare -gA _exprashParams=()
|
declare -gA _exprash_params=()
|
||||||
declare -gA _exprashBody=()
|
declare -gA _exprash_body=()
|
||||||
declare -gA _exprashQuery=()
|
declare -gA _exprash_query=()
|
||||||
declare -gA _exprashHeaders=()
|
declare -gA _exprash_headers=()
|
||||||
declare -gA _exprashCookies=()
|
declare -gA _exprash_cookies=()
|
||||||
declare -gA _exprashSetCookies=()
|
declare -gA _exprash_set_cookies=()
|
||||||
|
|
||||||
_exprashUseSession=0
|
_exprash_use_session=0
|
||||||
_exprashSessionDir=''
|
_exprash_session_dir=''
|
||||||
_exprashSessionId=''
|
_exprash_sessionId=''
|
||||||
declare -gA _exprashSession=()
|
declare -gA _exprash_session=()
|
||||||
_exprashSessionCookieName='exprash_session'
|
_exprash_session_cookie_name='exprash_session'
|
||||||
|
|
||||||
_exprashRouteHandled=0
|
_exprash_route_handled=0
|
||||||
_exprashErrorMessage=''
|
_exprash_error_message=''
|
||||||
_exprashHeadersSent=0
|
_exprash_headers_sent=0
|
||||||
_exprashHeaders['Content-Type']='text/html'
|
_exprash_headers['Content-Type']='text/html'
|
||||||
|
|
||||||
# Parse query string
|
# Parse query string
|
||||||
_parseUrlEncoded _exprashQuery "$QUERY_STRING"
|
_parse_url_encoded _exprash_query "$QUERY_STRING"
|
||||||
|
|
||||||
# Parse cookies
|
# Parse cookies
|
||||||
_parseCookies _exprashCookies "$HTTP_COOKIE"
|
_parse_cookies _exprash_cookies "$HTTP_COOKIE"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Shutdown trap
|
# Shutdown trap
|
||||||
trap _exprashShutdown EXIT
|
trap _exprash_shutdown EXIT
|
||||||
|
|
||||||
# Initialize exprash
|
# Initialize exprash
|
||||||
_exprashInit
|
_exprash_init
|
||||||
|
|
||||||
|
@ -1,20 +1,22 @@
|
|||||||
|
# shellcheck disable=SC2154,SC2034
|
||||||
|
|
||||||
it "Should parse cookies" "$({
|
it "Should parse cookies" "$({
|
||||||
HTTP_COOKIE='bob=gob; job=tob'
|
HTTP_COOKIE='bob=gob; job=tob'
|
||||||
_exprashInit
|
_exprash_init
|
||||||
output=$(declare -p _exprashCookies)
|
output=$(declare -p _exprash_cookies)
|
||||||
expected_output='declare -A _exprashCookies=([bob]="gob" [job]="tob" )'
|
expected_output='declare -A _exprash_cookies=([bob]="gob" [job]="tob" )'
|
||||||
[ "$output" == "$expected_output" ]
|
[ "$output" == "$expected_output" ]
|
||||||
})"
|
})"
|
||||||
|
|
||||||
it "Should get cookie value by name" "$({
|
it "Should get cookie value by name" "$({
|
||||||
HTTP_COOKIE='bob=gob; job=tob'
|
HTTP_COOKIE='bob=gob; job=tob'
|
||||||
_exprashInit
|
_exprash_init
|
||||||
[ "$(cookie 'bob')" == 'gob' ]
|
[ "$(cookie 'bob')" == 'gob' ]
|
||||||
})"
|
})"
|
||||||
|
|
||||||
it "Should determine if a cookie exists by name" "$({
|
it "Should determine if a cookie exists by name" "$({
|
||||||
HTTP_COOKIE='bob=gob; job=tob'
|
HTTP_COOKIE='bob=gob; job=tob'
|
||||||
_exprashInit
|
_exprash_init
|
||||||
hasCookie 'bob' && ! hasCookie 'kob'
|
has_cookie 'bob' && ! has_cookie 'kob'
|
||||||
})"
|
})"
|
||||||
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
|
# shellcheck disable=SC2154,SC2034
|
||||||
|
|
||||||
it "Should parse query string" "$({
|
it "Should parse query string" "$({
|
||||||
QUERY_STRING='ohmy=zsh&exit=vim'
|
QUERY_STRING='ohmy=zsh&exit=vim'
|
||||||
_exprashInit
|
_exprash_init
|
||||||
hasQuery 'ohmy' && ! hasQuery 'ohno' && [ "$(query 'exit')" == 'vim' ]
|
has_query 'ohmy' && ! has_query 'ohno' && [ "$(query 'exit')" == 'vim' ]
|
||||||
})"
|
})"
|
||||||
|
|
||||||
|
@ -1,62 +1,64 @@
|
|||||||
it "Should match plain route" $({
|
# shellcheck disable=SC2154,SC2034
|
||||||
|
|
||||||
|
it "Should match plain route" "$({
|
||||||
PATH_INFO='/plain/route'
|
PATH_INFO='/plain/route'
|
||||||
_exprashInit
|
_exprash_init
|
||||||
all '/plain/route'
|
all '/plain/route'
|
||||||
})
|
})"
|
||||||
|
|
||||||
it "Should not match incorrect plain route" $({
|
it "Should not match incorrect plain route" "$({
|
||||||
PATH_INFO='/plain/WRONG'
|
PATH_INFO='/plain/WRONG'
|
||||||
_exprashInit
|
_exprash_init
|
||||||
! all '/plain/route'
|
! all '/plain/route'
|
||||||
})
|
})"
|
||||||
|
|
||||||
it "Should extract parameter" $({
|
it "Should extract parameter" "$({
|
||||||
PATH_INFO='/cats/calico/pet'
|
PATH_INFO='/cats/calico/pet'
|
||||||
_exprashInit
|
_exprash_init
|
||||||
all '/cats/:cat/pet' && hasParam 'cat' && [ "$(param 'cat')" == 'calico' ]
|
all '/cats/:cat/pet' && has_param 'cat' && [ "$(param 'cat')" == 'calico' ]
|
||||||
})
|
})"
|
||||||
|
|
||||||
it "Should match wildcard route" $({
|
it "Should match wildcard route" "$({
|
||||||
PATH_INFO='/cats/calico/pet'
|
PATH_INFO='/cats/calico/pet'
|
||||||
_exprashInit
|
_exprash_init
|
||||||
all '/cats/*/pet'
|
all '/cats/*/pet'
|
||||||
})
|
})"
|
||||||
|
|
||||||
it "Should not match incorrect wildcard route" $({
|
it "Should not match incorrect wildcard route" "$({
|
||||||
PATH_INFO='/cats/calico/'
|
PATH_INFO='/cats/calico/'
|
||||||
_exprashInit
|
_exprash_init
|
||||||
! all '/cats/*/pet'
|
! all '/cats/*/pet'
|
||||||
})
|
})"
|
||||||
|
|
||||||
it "Should match multi-wildcard route" $({
|
it "Should match multi-wildcard route" "$({
|
||||||
PATH_INFO='/cats/calico/pet/donkey'
|
PATH_INFO='/cats/calico/pet/donkey'
|
||||||
_exprashInit
|
_exprash_init
|
||||||
all '/cats/**'
|
all '/cats/**'
|
||||||
})
|
})"
|
||||||
|
|
||||||
it "Should not match incorrect multi-wildcard route" $({
|
it "Should not match incorrect multi-wildcard route" "$({
|
||||||
PATH_INFO='/INCORRECT/calico/pet/donkey'
|
PATH_INFO='/INCORRECT/calico/pet/donkey'
|
||||||
_exprashInit
|
_exprash_init
|
||||||
! all '/cats/**'
|
! all '/cats/**'
|
||||||
})
|
})"
|
||||||
|
|
||||||
it "Should not match path shorter than route" $({
|
it "Should not match path shorter than route" "$({
|
||||||
PATH_INFO='/year'
|
PATH_INFO='/year'
|
||||||
_exprashInit
|
_exprash_init
|
||||||
! all '/year/:year'
|
! all '/year/:year'
|
||||||
})
|
})"
|
||||||
|
|
||||||
it "Should match get route" $({
|
it "Should match get route" "$({
|
||||||
REQUEST_METHOD='GET'
|
REQUEST_METHOD='GET'
|
||||||
PATH_INFO='/simple/route'
|
PATH_INFO='/simple/route'
|
||||||
_exprashInit
|
_exprash_init
|
||||||
get '/simple/route'
|
get '/simple/route'
|
||||||
})
|
})"
|
||||||
|
|
||||||
it "Should not match get route with incorrect method" $({
|
it "Should not match get route with incorrect method" "$({
|
||||||
REQUEST_METHOD='POST'
|
REQUEST_METHOD='POST'
|
||||||
PATH_INFO='/simple/route'
|
PATH_INFO='/simple/route'
|
||||||
_exprashInit
|
_exprash_init
|
||||||
! get '/simple/route'
|
! get '/simple/route'
|
||||||
})
|
})"
|
||||||
|
|
||||||
|
@ -1,30 +1,30 @@
|
|||||||
# shellcheck disable=SC2154,SC2034
|
# shellcheck disable=SC2154,SC2034
|
||||||
|
|
||||||
testSessionDir='/tmp/exprash_test_sessions'
|
test_session_dir='/tmp/exprash_test_sessions'
|
||||||
mkdir -p "$testSessionDir"
|
mkdir -p "$test_session_dir"
|
||||||
|
|
||||||
it "Should set session directory" "$({
|
it "Should set session directory" "$({
|
||||||
_exprashInit
|
_exprash_init
|
||||||
useSession "$testSessionDir"
|
use_session "$test_session_dir"
|
||||||
[ "$_exprashSessionDir" == "$testSessionDir" ]
|
[ "$_exprash_session_dir" == "$test_session_dir" ]
|
||||||
})"
|
})"
|
||||||
|
|
||||||
it "Should save and load session" "$({
|
it "Should save and load session" "$({
|
||||||
_exprashInit
|
_exprash_init
|
||||||
useSession "$testSessionDir"
|
use_session "$test_session_dir"
|
||||||
session 'data' 'value'
|
session 'data' 'value'
|
||||||
session_id=$_exprashSessionId
|
session_id=$_exprash_session_id
|
||||||
cookie_value=${_exprashSetCookies[$_exprashSessionCookieName]}
|
cookie_value=${_exprash_set_cookies[$_exprash_session_cookie_name]}
|
||||||
_exprashShutdown
|
_exprash_shutdown
|
||||||
|
|
||||||
# Forcibly clear session data just in case (but it should also get cleared by
|
# Forcibly clear session data just in case (but it should also get cleared by
|
||||||
# _exprashInit)
|
# _exprash_init)
|
||||||
_exprashSession=()
|
_exprash_session=()
|
||||||
_exprashSessionId=''
|
_exprash_session_id=''
|
||||||
|
|
||||||
HTTP_COOKIE="${_exprashSessionCookieName}=$cookie_value"
|
HTTP_COOKIE="${_exprash_session_cookie_name}=$cookie_value"
|
||||||
_exprashInit
|
_exprash_init
|
||||||
useSession "$testSessionDir"
|
use_session "$test_session_dir"
|
||||||
[ "$(session 'data')" == "value" ]
|
[ "$(session 'data')" == "value" ]
|
||||||
})"
|
})"
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user