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
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/foomo/contentserver/content"
|
||||
@ -32,7 +35,23 @@ func NewClientWithTransport(
|
||||
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) {
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
|
||||
@ -14,6 +14,8 @@ type httpTransport struct {
|
||||
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 {
|
||||
return &httpTransport{
|
||||
endpoint: server,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user