#!/bin/bash PROGNAME=${0##*/} VERSION="0.0.1" error_exit() { printf "%s: %s\n" "$PROGNAME" "${1:-"Unknown Error"}" >&2 exit 1 } usage() { printf "usage: %s [OPTIONS] URI [ARGS]\n" "$PROGNAME" printf "Simple client for spartan protocol\n" } request(){ page=$(printf "$host /$path $( $post | wc -c )\r\n$post" | nc $host $port) status=$(echo $page | awk 'NR == 1 {print $1}') meta=$(echo $page | awk 'NR == 1 {print $2}') if [[ $status == 2 ]]; then printf "$host /$path $( $post | wc -c )\r\n$post" | nc $host $port | awk 'NR > 1 {print}' elif [[ $status == 3 ]]; then path=$meta printf "$host $path 0\r\n" | nc $host 300 echo $host $path elif [[ $status == 4 ]]; then printf "Client error: %s\n" "$meta" elif [[ $status == 5 ]]; then printf "$host /$path $( $post | wc -c )\r\n$post" | nc $host $port #printf "Server error: %s\n" "$meta" fi } while getopts ":h:p:i:u:" arg; do case $arg in h | --help) usage; exit ;; p | --port) export port=$OPTARG ;; 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} request else echo $url_unparsed is not a Spartan URL fi ;; * | -* | --*) usage; error_exit "unknown option $1" ;; esac done