src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

url.go (885B)


      1 package http
      2 
      3 import "strings"
      4 
      5 // JoinPath returns an absolute URL path composed of the two paths provided.
      6 // Enforces that the returned path begins with '/'. If added path is empty the
      7 // returned path suffix will match the first parameter suffix.
      8 func JoinPath(a, b string) string {
      9 	if len(a) == 0 {
     10 		a = "/"
     11 	} else if a[0] != '/' {
     12 		a = "/" + a
     13 	}
     14 
     15 	if len(b) != 0 && b[0] == '/' {
     16 		b = b[1:]
     17 	}
     18 
     19 	if len(b) != 0 && len(a) > 1 && a[len(a)-1] != '/' {
     20 		a = a + "/"
     21 	}
     22 
     23 	return a + b
     24 }
     25 
     26 // JoinRawQuery returns an absolute raw query expression. Any duplicate '&'
     27 // will be collapsed to single separator between values.
     28 func JoinRawQuery(a, b string) string {
     29 	a = strings.TrimFunc(a, isAmpersand)
     30 	b = strings.TrimFunc(b, isAmpersand)
     31 
     32 	if len(a) == 0 {
     33 		return b
     34 	}
     35 	if len(b) == 0 {
     36 		return a
     37 	}
     38 
     39 	return a + "&" + b
     40 }
     41 
     42 func isAmpersand(v rune) bool {
     43 	return v == '&'
     44 }