Skip to content

Commit

Permalink
For intelsdi-x#260 Add url checking and comment about invalid target
Browse files Browse the repository at this point in the history
  • Loading branch information
tiffanyfay committed Feb 24, 2016
1 parent 4317e71 commit e9bdb48
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
5 changes: 5 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 15 additions & 9 deletions mgmt/rest/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (
"path/filepath"
"strings"

"github.com/asaskevich/govalidator"

"github.com/intelsdi-x/snap/mgmt/rest/rbody"
)

Expand Down Expand Up @@ -91,7 +93,6 @@ func New(url, ver string, insecure bool) *Client {
},
}
c.prefix = url + "/" + ver
// TODO (danielscottt): assert that path is valid and target is available
return c
}

Expand All @@ -111,14 +112,19 @@ func (c *Client) do(method, path string, ct contentType, body ...[]byte) (*rbody
rsp *http.Response
err error
)
// URL validation
validURL := govalidator.IsURL(c.prefix + path)
if !validURL {
return nil, errors.New("URL is not in the format of http(s)://<ip>:<port>")
}
switch method {
case "GET":
rsp, err = c.http.Get(c.prefix + path)
if err != nil {
if strings.Contains(err.Error(), "tls: oversized record") || strings.Contains(err.Error(), "malformed HTTP response") {
return nil, fmt.Errorf("error connecting to API URI: %s. Do you have an http/https mismatch?", c.URL)
}
return nil, err
return nil, fmt.Errorf("URL target is not available. %v", err)
}
case "PUT":
var b *bytes.Reader
Expand All @@ -130,14 +136,14 @@ func (c *Client) do(method, path string, ct contentType, body ...[]byte) (*rbody
req, err := http.NewRequest("PUT", c.prefix+path, b)
req.Header.Add("Content-Type", ct.String())
if err != nil {
return nil, err
return nil, fmt.Errorf("URL target is not available. %v", err)
}
rsp, err = c.http.Do(req)
if err != nil {
if strings.Contains(err.Error(), "tls: oversized record") || strings.Contains(err.Error(), "malformed HTTP response") {
return nil, fmt.Errorf("error connecting to API URI: %s. Do you have an http/https mismatch?", c.URL)
}
return nil, err
return nil, fmt.Errorf("URL target is not available. %v", err)
}
case "DELETE":
var b *bytes.Reader
Expand All @@ -149,14 +155,14 @@ func (c *Client) do(method, path string, ct contentType, body ...[]byte) (*rbody
req, err := http.NewRequest("DELETE", c.prefix+path, b)
req.Header.Add("Content-Type", "application/json")
if err != nil {
return nil, err
return nil, fmt.Errorf("URL target is not available. %v", err)
}
rsp, err = c.http.Do(req)
if err != nil {
if strings.Contains(err.Error(), "tls: oversized record") || strings.Contains(err.Error(), "malformed HTTP response") {
return nil, fmt.Errorf("error connecting to API URI: %s. Do you have an http/https mismatch?", c.URL)
}
return nil, err
return nil, fmt.Errorf("URL target is not available. %v", err)
}
case "POST":
var b *bytes.Reader
Expand All @@ -170,7 +176,7 @@ func (c *Client) do(method, path string, ct contentType, body ...[]byte) (*rbody
if strings.Contains(err.Error(), "tls: oversized record") || strings.Contains(err.Error(), "malformed HTTP response") {
return nil, fmt.Errorf("error connecting to API URI: %s. Do you have an http/https mismatch?", c.URL)
}
return nil, err
return nil, fmt.Errorf("URL target is not available. %v", err)
}
}

Expand Down Expand Up @@ -237,7 +243,7 @@ func (c *Client) pluginUploadRequest(pluginPaths []string) (*rbody.APIResponse,

req, err := http.NewRequest("POST", c.prefix+"/plugins", pr)
if err != nil {
return nil, err
return nil, fmt.Errorf("URL target is not available. %v", err)
}

req.Header.Add("Content-Type", writer.FormDataContentType())
Expand All @@ -249,7 +255,7 @@ func (c *Client) pluginUploadRequest(pluginPaths []string) (*rbody.APIResponse,
if strings.Contains(err.Error(), "tls: oversized record") || strings.Contains(err.Error(), "malformed HTTP response") {
return nil, fmt.Errorf("error connecting to API URI: %s. Do you have an http/https mismatch?", c.URL)
}
return nil, err
return nil, fmt.Errorf("URL target is not available. %v", err)
}
cErr := <-errChan
if cErr != nil {
Expand Down

0 comments on commit e9bdb48

Please sign in to comment.