Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions shared/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ func (sc ServerConfigurations) URL(index int, variables map[string]string) (stri
}
server := sc[index]
url := server.URL
if !strings.Contains(url, "http://") && !strings.Contains(url, "https://") {
return "", fmt.Errorf(
"the URL provided appears to be missing the protocol scheme prefix (\"https://\" or \"http://\"), please verify and try again: %s",
url,
)
}

// go through variables and replace placeholders
for name, variable := range server.Variables {
Expand Down Expand Up @@ -251,14 +257,12 @@ func getServerUrl(serverUrl string) string {
if serverUrl == "" {
return DefaultIonosServerUrl + DefaultIonosBasePath
}
// Support both HTTPS & HTTP schemas
if !strings.HasPrefix(serverUrl, "https://") && !strings.HasPrefix(serverUrl, "http://") {
serverUrl = fmt.Sprintf("https://%s", serverUrl)
}

if !strings.HasSuffix(serverUrl, DefaultIonosBasePath) {
serverUrl = fmt.Sprintf("%s%s", serverUrl, DefaultIonosBasePath)
}
return serverUrl

return EnsureURLFormat(serverUrl)
}

// ServerURLWithContext returns a new server URL given an endpoint
Expand Down
22 changes: 22 additions & 0 deletions shared/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package shared

import (
"fmt"
"reflect"
"strings"
"unicode/utf8"
)

Expand Down Expand Up @@ -68,3 +70,23 @@ func IsNil(i interface{}) bool {
}
return false
}

// EnsureURLFormat checks that the URL has the correct format (no trailing slash,
// has http/https scheme prefix) and updates it if necessary
func EnsureURLFormat(url string) string {
length := len(url)

if length <= 1 {
return url
}

if url[length-1] == '/' {
url = url[:length-1]
}

if !strings.HasPrefix(url, "https://") && !strings.HasPrefix(url, "http://") {
url = fmt.Sprintf("https://%s", url)
}

return url
}
Loading