mirror of
https://github.com/foomo/contentserver.git
synced 2025-10-16 12:25:44 +00:00
fix: validate server url in NewHTTPClient and return an error for invalid configurations
This commit is contained in:
parent
006bd6ea0a
commit
c2837eec07
@ -1,7 +1,10 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/foomo/contentserver/content"
|
"github.com/foomo/contentserver/content"
|
||||||
@ -32,7 +35,23 @@ func NewClientWithTransport(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrEmptyServerURL = errors.New("empty contentserver url provided")
|
||||||
|
ErrInvalidServerURL = errors.New("invalid contentserver url provided")
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewHTTPClient constructs a new client to talk to the contentserver.
|
||||||
|
// It returns an error if the provided url is empty or invalid.
|
||||||
func NewHTTPClient(server string) (c *Client, err error) {
|
func NewHTTPClient(server string) (c *Client, err error) {
|
||||||
|
|
||||||
|
if server == "" {
|
||||||
|
return nil, ErrEmptyServerURL
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err = url.Parse(server); err != nil {
|
||||||
|
return nil, fmt.Errorf("%w: %s", ErrInvalidServerURL, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
return NewHTTPClientWithTransport(NewHTTPTransport(server, http.DefaultClient))
|
return NewHTTPClientWithTransport(NewHTTPTransport(server, http.DefaultClient))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -14,6 +14,8 @@ type httpTransport struct {
|
|||||||
endpoint string
|
endpoint string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewHTTPTransport will create a new http transport for the given server and client.
|
||||||
|
// Caution: the provided server url is not validated!
|
||||||
func NewHTTPTransport(server string, client *http.Client) transport {
|
func NewHTTPTransport(server string, client *http.Client) transport {
|
||||||
return &httpTransport{
|
return &httpTransport{
|
||||||
endpoint: server,
|
endpoint: server,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user