~julian-edwards/gwacl/licence-readme-etc

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
45
46
// Copyright 2013 Canonical Ltd.  This software is licensed under the
// GNU Lesser General Public License version 3 (see the file COPYING).

package gwacl

import (
    "fmt"
    "net/url"
)

// checkPathComponents checks that none of the passed components contains any
// special characters, where special means "needs percent-encoding in a URI",
// does not contain any forward slashes, and is not the string "..".
func checkPathComponents(components ...string) {
    for _, component := range components {
        if component != url.QueryEscape(component) {
            panic(fmt.Errorf("'%s' contains URI special characters", component))
        }
        if component == ".." {
            panic(fmt.Errorf("'..' is not allowed"))
        }
    }
}

// addURLQueryParams adds query parameters to a URL (and escapes as needed).
// Parameters are URL, [key, value, [key, value, [...]]].
// The original URL must be correct, i.e. it should parse without error.
func addURLQueryParams(originalURL string, params ...string) string {
    if len(params)%2 != 0 {
        panic(fmt.Errorf("got %d parameter argument(s), instead of matched key/value pairs", len(params)))
    }
    parsedURL, err := url.Parse(originalURL)
    if err != nil {
        panic(err)
    }
    query := parsedURL.Query()

    for index := 0; index < len(params); index += 2 {
        key := params[index]
        value := params[index+1]
        query.Add(key, value)
    }

    parsedURL.RawQuery = query.Encode()
    return parsedURL.String()
}