Add check if url has spartan://

This commit is contained in:
g1n 2021-07-06 15:46:53 +00:00
parent 46e53a5f74
commit 27361645cd
1 changed files with 34 additions and 20 deletions

54
laconia
View File

@ -1,14 +1,7 @@
#!/bin/sh #!/bin/bash
PROGNAME=${0##*/} PROGNAME=${0##*/}
VERSION="0.0.1" VERSION="0.0.1"
proto="$(echo $1 | grep :// | sed -e's,^\(.*://\).*,\1,g')"
url=$(echo $1 | sed -e 's,$proto,,g')
host=$(echo "$url" | sed -e 's|^[^/]*//||' -e 's|/.*$||')
path="$(echo $url | grep / | cut -d/ -f2-)"
port=300
error_exit() { error_exit() {
printf "%s: %s\n" "$PROGNAME" "${1:-"Unknown Error"}" >&2 printf "%s: %s\n" "$PROGNAME" "${1:-"Unknown Error"}" >&2
exit 1 exit 1
@ -19,15 +12,36 @@ usage() {
printf "Simple client for spartan protocol\n" printf "Simple client for spartan protocol\n"
} }
case $1 in request(){
-h | --help) printf "$host /$path $( $post | wc -c )\r\n$post" | nc $host $port
usage; exit ;; }
-p | --port)
export PORT=$3 ;; while getopts ":h:p:i:u:" arg; do
-* | --*) case $arg in
usage; error_exit "unknown option $1" ;; h | --help)
*) usage; exit ;;
echo $proto $host $path $port p | --port)
printf "$host /$path 0\r\n" | nc $host $port export port=$OPTARG
;; ;;
esac i | --input)
export post=$OPTARG
;;
u | --url)
url_unparsed=$OPTARG
#pattern=^((spartan)://)?([^/:]+)(:([0-9]+))?/(.*)$
#echo $url_unparsed
if [[ "$url_unparsed" =~ ^((spartan)://)?([^/:]+)(:([0-9]+))?/(.*)$ ]]; then
url=$(echo $url_unparsed | sed -e 's,spartan://,,g')
host=$(echo "$url" | sed -e 's|^[^/]*//||' -e 's|/.*$||')
path="$(echo $url | grep / | cut -d/ -f2-)"
port=${port:-300}
echo $port $post $host $path
request
else
echo $url_unparsed is not a Spartan URL
fi
;;
* | -* | --*)
usage; error_exit "unknown option $1" ;;
esac
done