Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. #
  2. # URI parsing function
  3. #
  4. # The function creates global variables with the parsed results.
  5. # It returns 0 if parsing was successful or non-zero otherwise.
  6. #
  7. # [schema://][user[:password]@]host[:port][/path][?[arg1=val1]...][#fragment]
  8. #
  9. function uri_parser() {
  10. # uri capture
  11. uri="$@"
  12.  
  13. # safe escaping
  14. uri="${uri//\`/%60}"
  15. uri="${uri//\"/%22}"
  16.  
  17. # top level parsing
  18. pattern='^(([a-z]{3,5})://)?((([^:\/]+)(:([^@\/]*))?@)?([^:\/?]*)(:([0-9]+))?)(\/[^?]*)?(\?[^#]*)?(#.*)?$'
  19. [[ "$uri" =~ $pattern ]] || return 1;
  20.  
  21. # component extraction
  22. uri=${BASH_REMATCH[0]}
  23. uri_schema=${BASH_REMATCH[2]}
  24. uri_address=${BASH_REMATCH[3]}
  25. uri_user=${BASH_REMATCH[5]}
  26. uri_password=${BASH_REMATCH[7]}
  27. uri_host=${BASH_REMATCH[8]}
  28. uri_port=${BASH_REMATCH[10]}
  29. uri_path=${BASH_REMATCH[11]}
  30. uri_query=${BASH_REMATCH[12]}
  31. uri_fragment=${BASH_REMATCH[13]}
  32.  
  33. # path parsing
  34. count=0
  35. path="$uri_path"
  36. pattern='^/+([^/]+)'
  37. while [[ $path =~ $pattern ]]; do
  38. eval "uri_parts[$count]=\"${BASH_REMATCH[1]}\""
  39. path="${path:${#BASH_REMATCH[0]}}"
  40. let count++
  41. done
  42.  
  43. # query parsing
  44. count=0
  45. query="$uri_query"
  46. pattern='^[?&]+([^= ]+)(=([^&]*))?'
  47. while [[ $query =~ $pattern ]]; do
  48. eval "uri_args[$count]=\"${BASH_REMATCH[1]}\""
  49. eval "uri_arg_${BASH_REMATCH[1]}=\"${BASH_REMATCH[3]}\""
  50. query="${query:${#BASH_REMATCH[0]}}"
  51. let count++
  52. done
  53.  
  54. # return success
  55. return 0
  56. }
  57.  
  58. function uri_build () {
  59. #check schema
  60. if [[ -z $1 ]]; then
  61. echo "No schema specified"
  62. return 1
  63. else
  64. schema="$1://"
  65. fi
  66. #check user
  67. if [[ -z $2 ]]; then
  68. user=''
  69. else
  70. user="$2"
  71. at="@"
  72. fi
  73. #check password
  74. if [[ -z $3 ]]; then
  75. password=''
  76. else
  77. password=":$3"
  78. at="@"
  79. fi
  80. #check host
  81. if [[ -z $4 ]]; then
  82. echo "No host specified"
  83. # return 1
  84. else
  85. host=$4
  86. fi
  87. #check port
  88. if [[ -z $5 ]]; then
  89. port=''
  90. else
  91. port=":$5"
  92. fi
  93. #the rest of parameters are passed as is
  94. echo "$schema$user$password$at$host$port$6$7$8"
  95. }
  96.  
  97. uri=$1
  98.  
  99. # perform parsing and handle failure
  100. uri_parser "$uri" || { echo "Malformed URI!"; exit 1; }
  101.  
  102. # main uri
  103. #echo "uri = $uri"
  104.  
  105. # uri components
  106. #echo "uri_schema = $uri_schema"
  107. #echo "uri_address = $uri_address"
  108. #echo "uri_user = $uri_user"
  109. #echo "uri_password = $uri_password"
  110. #echo "uri_host = $uri_host"
  111. #echo "uri_port = $uri_port"
  112. #echo "uri_path = $uri_path"
  113. #echo "uri_query = $uri_query"
  114. #echo "uri_fragment = $uri_fragment"
  115.  
  116. if [[ $uri_host =~ ^crm* ]]; then
  117. uri_host="tickets.company.com"
  118. dst=$(uri_build "$uri_schema" "$uri_user" "$uri_password" "$uri_host" "$uri_port" "$uri_path" "$uri_query" "$uri_fragment")
  119. open -a "Firefox" $dst
  120. elif [[ $uri_host =~ ^*.company.com$ ]]; then
  121. open -a "Firefox" $uri
  122. else
  123. open -a "Google Chrome" $uri
  124. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement