File: url.go

package info (click to toggle)
golang-github-aws-smithy-go 1.13.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,228 kB
  • sloc: java: 12,359; xml: 166; sh: 131; makefile: 47
file content (44 lines) | stat: -rw-r--r-- 885 bytes parent folder | download | duplicates (5)
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
40
41
42
43
44
package http

import "strings"

// JoinPath returns an absolute URL path composed of the two paths provided.
// Enforces that the returned path begins with '/'. If added path is empty the
// returned path suffix will match the first parameter suffix.
func JoinPath(a, b string) string {
	if len(a) == 0 {
		a = "/"
	} else if a[0] != '/' {
		a = "/" + a
	}

	if len(b) != 0 && b[0] == '/' {
		b = b[1:]
	}

	if len(b) != 0 && len(a) > 1 && a[len(a)-1] != '/' {
		a = a + "/"
	}

	return a + b
}

// JoinRawQuery returns an absolute raw query expression. Any duplicate '&'
// will be collapsed to single separator between values.
func JoinRawQuery(a, b string) string {
	a = strings.TrimFunc(a, isAmpersand)
	b = strings.TrimFunc(b, isAmpersand)

	if len(a) == 0 {
		return b
	}
	if len(b) == 0 {
		return a
	}

	return a + "&" + b
}

func isAmpersand(v rune) bool {
	return v == '&'
}