David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame] | 1 | // Copyright 2012-2016 Canonical Ltd. |
| 2 | // Licensed under the LGPLv3, see LICENCE file for details. |
| 3 | |
| 4 | package gomaasapi |
| 5 | |
| 6 | import ( |
| 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. |
| 14 | func 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.) |
| 22 | func EnsureTrailingSlash(URL string) string { |
| 23 | if strings.HasSuffix(URL, "/") { |
| 24 | return URL |
| 25 | } |
| 26 | return URL + "/" |
| 27 | } |