Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # URI parsing function
- #
- # The function creates global variables with the parsed results.
- # It returns 0 if parsing was successful or non-zero otherwise.
- #
- # [schema://][user[:password]@]host[:port][/path][?[arg1=val1]...][#fragment]
- #
- function uri_parser() {
- # uri capture
- uri="$@"
- # safe escaping
- uri="${uri//\`/%60}"
- uri="${uri//\"/%22}"
- # top level parsing
- pattern='^(([a-z]{3,5})://)?((([^:\/]+)(:([^@\/]*))?@)?([^:\/?]*)(:([0-9]+))?)(\/[^?]*)?(\?[^#]*)?(#.*)?$'
- [[ "$uri" =~ $pattern ]] || return 1;
- # component extraction
- uri=${BASH_REMATCH[0]}
- uri_schema=${BASH_REMATCH[2]}
- uri_address=${BASH_REMATCH[3]}
- uri_user=${BASH_REMATCH[5]}
- uri_password=${BASH_REMATCH[7]}
- uri_host=${BASH_REMATCH[8]}
- uri_port=${BASH_REMATCH[10]}
- uri_path=${BASH_REMATCH[11]}
- uri_query=${BASH_REMATCH[12]}
- uri_fragment=${BASH_REMATCH[13]}
- # path parsing
- count=0
- path="$uri_path"
- pattern='^/+([^/]+)'
- while [[ $path =~ $pattern ]]; do
- eval "uri_parts[$count]=\"${BASH_REMATCH[1]}\""
- path="${path:${#BASH_REMATCH[0]}}"
- let count++
- done
- # query parsing
- count=0
- query="$uri_query"
- pattern='^[?&]+([^= ]+)(=([^&]*))?'
- while [[ $query =~ $pattern ]]; do
- eval "uri_args[$count]=\"${BASH_REMATCH[1]}\""
- eval "uri_arg_${BASH_REMATCH[1]}=\"${BASH_REMATCH[3]}\""
- query="${query:${#BASH_REMATCH[0]}}"
- let count++
- done
- # return success
- return 0
- }
- function uri_build () {
- #check schema
- if [[ -z $1 ]]; then
- echo "No schema specified"
- return 1
- else
- schema="$1://"
- fi
- #check user
- if [[ -z $2 ]]; then
- user=''
- else
- user="$2"
- at="@"
- fi
- #check password
- if [[ -z $3 ]]; then
- password=''
- else
- password=":$3"
- at="@"
- fi
- #check host
- if [[ -z $4 ]]; then
- echo "No host specified"
- # return 1
- else
- host=$4
- fi
- #check port
- if [[ -z $5 ]]; then
- port=''
- else
- port=":$5"
- fi
- #the rest of parameters are passed as is
- echo "$schema$user$password$at$host$port$6$7$8"
- }
- uri=$1
- # perform parsing and handle failure
- uri_parser "$uri" || { echo "Malformed URI!"; exit 1; }
- # main uri
- #echo "uri = $uri"
- # uri components
- #echo "uri_schema = $uri_schema"
- #echo "uri_address = $uri_address"
- #echo "uri_user = $uri_user"
- #echo "uri_password = $uri_password"
- #echo "uri_host = $uri_host"
- #echo "uri_port = $uri_port"
- #echo "uri_path = $uri_path"
- #echo "uri_query = $uri_query"
- #echo "uri_fragment = $uri_fragment"
- if [[ $uri_host =~ ^crm* ]]; then
- uri_host="tickets.company.com"
- dst=$(uri_build "$uri_schema" "$uri_user" "$uri_password" "$uri_host" "$uri_port" "$uri_path" "$uri_query" "$uri_fragment")
- open -a "Firefox" $dst
- elif [[ $uri_host =~ ^*.company.com$ ]]; then
- open -a "Firefox" $uri
- else
- open -a "Google Chrome" $uri
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement