Flexible parsing of the input URL

This commit is contained in:
Manfred Touron 2015-11-01 19:31:46 +01:00
parent c8178c41d8
commit c7186d44ee
No known key found for this signature in database
GPG Key ID: 0DCB9CE0CABAE1B5
2 changed files with 22 additions and 3 deletions

View File

@ -103,7 +103,7 @@ $ brew install https://raw.githubusercontent.com/moul/ssh2docker/master/contrib/
### master (unreleased)
* No entry
* Flexible parsing of the input URL
[full commits list](https://github.com/moul/gotty-client/compare/v1.3.0...master)

View File

@ -372,11 +372,30 @@ func (c *Client) SetOutput(w io.Writer) {
c.Output = w
}
// ParseURL parses an URL which may be incomplete and tries to standardize it
func ParseURL(input string) (string, error) {
parsed, err := url.Parse(input)
if err != nil {
return "", err
}
switch parsed.Scheme {
case "http", "https":
// everything is ok
default:
return ParseURL(fmt.Sprintf("http://%s", input))
}
return parsed.String(), nil
}
// NewClient returns a GoTTY client object
func NewClient(httpURL string) (*Client, error) {
func NewClient(inputURL string) (*Client, error) {
url, err := ParseURL(inputURL)
if err != nil {
return nil, err
}
return &Client{
Dialer: &websocket.Dialer{},
URL: httpURL,
URL: url,
WriteMutex: &sync.Mutex{},
Output: os.Stdout,
QuitChan: make(chan struct{}),