-
Notifications
You must be signed in to change notification settings - Fork 25
/
uri-relate
executable file
·39 lines (37 loc) · 980 Bytes
/
uri-relate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/bash
set -euf -o pipefail
out () { printf %s\\n "$*" ; }
##
# Combine a base URI and relative URI to produce an absolute URI.
#
# Examples:
#
# $ uri-relate "http://example.com/a/b/c/" "index.html"
# http://example.com/a/b/c/index.html"
#
# $ uri-relate "http://example.com/a/b/c/" "/index.html"
# http://example.com/index.html"
#
# $ uri-relate "http://example.com/a/b/c/" "//my.com/index.html"
# http://my.com/index.html"
#
# Important: this script merely handles a few of the common cases,
# sufficient for our immediate needs for our current projects.
#
# TODO: add more syntax possiblities; implement URI standards.
#
##
base="$1"
rel="$2"
abs=""
if [[ "$rel" =~ :// ]]; then
abs="$rel"
elif [[ "$rel" =~ ^// ]]; then
abs=$(out "$base" | uri-scheme name)":$rel"
elif [[ "$rel" =~ ^/ ]]; then
read name authority < <(out "$base" | uri-scheme name authority)
abs="$name://$authority$rel"
else
abs="$base/$rel"
fi
out "$abs"