Advertisement
rockdrilla

dumb way to parse&reconstruct URL, rev.0

Oct 29th, 2013
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.10 KB | None | 0 0
  1. url_parse() {
  2.   sed -nre '
  3.  /^(([^:]+):(\/{0,2}))?(([^:]+):([^@]+)@)?([^:\/]*)?:?([^/]+)?(.*)$/ {
  4.    h
  5.    s##U[scheme]="\1"#p;g
  6.    s##U[user]="\5"#p;g
  7.    s##U[pass]="\6"#p;g
  8.    s##U[domain]="\7"#p;g
  9.    s##U[port]="\8"#p;g
  10.    s##U[path]="\9"#;s#/./#/#g;s#/+#/#g;p
  11.  }' | tr '"' "'"
  12. }
  13. url_construct() {
  14.   echo ${U[scheme]:-''}${U[user]}${U[pass]:+':'}${U[pass]}${U[user]:+'@'}${U[domain]:-''}${U[port]:+':'}${U[port]}${U[path]:-'/'}
  15. }
  16.  
  17. declare -A U
  18. u1='https://UsErNaMe:My5tR0nGp4s5@domain.ru:8088//./dump.txt'
  19. u2='file:///home/krd/lulz.txt'
  20.  
  21. echo "${u1}" | url_parse
  22. # output:
  23. #   U[scheme]='https://'
  24. #   U[user]='UsErNaMe'
  25. #   U[pass]='My5tR0nGp4s5'
  26. #   U[domain]='domain.ru'
  27. #   U[port]='8088'
  28. #   U[path]='/dump.txt'
  29. eval $(echo "${u1}" | url_parse)
  30. url_construct
  31. # output:
  32. #   https://UsErNaMe:My5tR0nGp4s5@domain.ru:8088/dump.txt
  33.  
  34. echo "${u2}" | url_parse
  35. # output:
  36. #   U[scheme]='file://'
  37. #   U[user]=''
  38. #   U[pass]=''
  39. #   U[domain]=''
  40. #   U[port]=''
  41. #   U[path]='/home/krd/lulz.txt'
  42. eval $(echo "${u2}" | url_parse)
  43. url_construct
  44. # output:
  45. #   file:///home/krd/lulz.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement