blob: 3f95ac90b1648784c7829c16a88c702e1e5037fb [file] [log] [blame]
David K. Bainbridge528b3182017-01-23 08:51:59 -08001// Copyright 2012-2016 Canonical Ltd.
2// Licensed under the LGPLv3, see LICENCE file for details.
3
4package gomaasapi
5
6import (
7 "strings"
8)
9
10// JoinURLs joins a base URL and a subpath together.
11// Regardless of whether baseURL ends in a trailing slash (or even multiple
12// trailing slashes), or whether there are any leading slashes at the begining
13// of path, the two will always be joined together by a single slash.
14func JoinURLs(baseURL, path string) string {
15 return strings.TrimRight(baseURL, "/") + "/" + strings.TrimLeft(path, "/")
16}
17
18// EnsureTrailingSlash appends a slash at the end of the given string unless
19// there already is one.
20// This is used to create the kind of normalized URLs that Django expects.
21// (to avoid Django's redirection when an URL does not ends with a slash.)
22func EnsureTrailingSlash(URL string) string {
23 if strings.HasSuffix(URL, "/") {
24 return URL
25 }
26 return URL + "/"
27}