2015-01-08 02:00:00 -06:00
|
|
|
package util
|
2014-10-03 02:03:16 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
2014-12-29 06:35:51 -06:00
|
|
|
"strings"
|
2014-10-03 02:03:16 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type UrlQueryReader struct {
|
|
|
|
values url.Values
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUrlQueryReader(url *url.URL) *UrlQueryReader {
|
|
|
|
return &UrlQueryReader{
|
|
|
|
values: url.Query(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *UrlQueryReader) Get(name string, def string) string {
|
|
|
|
val := r.values[name]
|
|
|
|
if len(val) == 0 {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
|
|
|
|
return val[0]
|
|
|
|
}
|
2014-12-29 06:35:51 -06:00
|
|
|
|
|
|
|
func JoinUrlFragments(a, b string) string {
|
|
|
|
aslash := strings.HasSuffix(a, "/")
|
|
|
|
bslash := strings.HasPrefix(b, "/")
|
2016-01-29 11:31:00 -06:00
|
|
|
|
|
|
|
if len(b) == 0 {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2014-12-29 06:35:51 -06:00
|
|
|
switch {
|
|
|
|
case aslash && bslash:
|
|
|
|
return a + b[1:]
|
|
|
|
case !aslash && !bslash:
|
|
|
|
return a + "/" + b
|
|
|
|
}
|
|
|
|
return a + b
|
|
|
|
}
|