2013-08-02 06:47:58 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2014-07-24 20:37:44 +00:00
|
|
|
|
2014-10-24 17:12:35 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2013-08-02 06:47:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// VersionInfo is used to model entities which has a version.
|
|
|
|
// It is basically a tupple with name and version.
|
|
|
|
type VersionInfo interface {
|
|
|
|
Name() string
|
|
|
|
Version() string
|
|
|
|
}
|
|
|
|
|
|
|
|
func validVersion(version VersionInfo) bool {
|
2014-05-16 23:46:22 +00:00
|
|
|
const stopChars = " \t\r\n/"
|
|
|
|
name := version.Name()
|
|
|
|
vers := version.Version()
|
|
|
|
if len(name) == 0 || strings.ContainsAny(name, stopChars) {
|
2013-08-02 06:47:58 +00:00
|
|
|
return false
|
|
|
|
}
|
2014-05-16 23:46:22 +00:00
|
|
|
if len(vers) == 0 || strings.ContainsAny(vers, stopChars) {
|
2013-08-02 06:47:58 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert versions to a string and append the string to the string base.
|
|
|
|
//
|
|
|
|
// Each VersionInfo will be converted to a string in the format of
|
|
|
|
// "product/version", where the "product" is get from the Name() method, while
|
|
|
|
// version is get from the Version() method. Several pieces of verson information
|
|
|
|
// will be concatinated and separated by space.
|
|
|
|
func appendVersions(base string, versions ...VersionInfo) string {
|
|
|
|
if len(versions) == 0 {
|
|
|
|
return base
|
|
|
|
}
|
|
|
|
|
2014-05-16 23:46:22 +00:00
|
|
|
verstrs := make([]string, 0, 1+len(versions))
|
2013-08-02 06:47:58 +00:00
|
|
|
if len(base) > 0 {
|
2014-05-16 23:46:22 +00:00
|
|
|
verstrs = append(verstrs, base)
|
2013-08-02 06:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range versions {
|
|
|
|
if !validVersion(v) {
|
|
|
|
continue
|
|
|
|
}
|
2014-05-16 23:46:22 +00:00
|
|
|
verstrs = append(verstrs, v.Name()+"/"+v.Version())
|
2013-08-02 06:47:58 +00:00
|
|
|
}
|
2014-05-16 23:46:22 +00:00
|
|
|
return strings.Join(verstrs, " ")
|
2013-08-02 06:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPRequestDecorator is used to change an instance of
|
|
|
|
// http.Request. It could be used to add more header fields,
|
|
|
|
// change body, etc.
|
|
|
|
type HTTPRequestDecorator interface {
|
|
|
|
// ChangeRequest() changes the request accordingly.
|
|
|
|
// The changed request will be returned or err will be non-nil
|
|
|
|
// if an error occur.
|
|
|
|
ChangeRequest(req *http.Request) (newReq *http.Request, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPUserAgentDecorator appends the product/version to the user agent field
|
|
|
|
// of a request.
|
|
|
|
type HTTPUserAgentDecorator struct {
|
|
|
|
versions []VersionInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHTTPUserAgentDecorator(versions ...VersionInfo) HTTPRequestDecorator {
|
2013-12-20 16:11:35 +00:00
|
|
|
return &HTTPUserAgentDecorator{
|
|
|
|
versions: versions,
|
|
|
|
}
|
2013-08-02 06:47:58 +00:00
|
|
|
}
|
|
|
|
|
2013-11-18 23:35:56 +00:00
|
|
|
func (h *HTTPUserAgentDecorator) ChangeRequest(req *http.Request) (newReq *http.Request, err error) {
|
2013-08-02 06:47:58 +00:00
|
|
|
if req == nil {
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:35:56 +00:00
|
|
|
userAgent := appendVersions(req.UserAgent(), h.versions...)
|
2013-08-02 06:47:58 +00:00
|
|
|
if len(userAgent) > 0 {
|
|
|
|
req.Header.Set("User-Agent", userAgent)
|
|
|
|
}
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
2013-08-22 19:15:31 +00:00
|
|
|
type HTTPMetaHeadersDecorator struct {
|
|
|
|
Headers map[string][]string
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:35:56 +00:00
|
|
|
func (h *HTTPMetaHeadersDecorator) ChangeRequest(req *http.Request) (newReq *http.Request, err error) {
|
|
|
|
if h.Headers == nil {
|
2013-08-22 19:15:31 +00:00
|
|
|
return req, nil
|
|
|
|
}
|
2013-11-18 23:35:56 +00:00
|
|
|
for k, v := range h.Headers {
|
2013-08-22 19:15:31 +00:00
|
|
|
req.Header[k] = v
|
|
|
|
}
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
2013-10-22 18:48:29 +00:00
|
|
|
type HTTPAuthDecorator struct {
|
2013-12-20 16:11:35 +00:00
|
|
|
login string
|
2013-10-22 18:48:29 +00:00
|
|
|
password string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHTTPAuthDecorator(login, password string) HTTPRequestDecorator {
|
2013-12-20 16:11:35 +00:00
|
|
|
return &HTTPAuthDecorator{
|
|
|
|
login: login,
|
|
|
|
password: password,
|
|
|
|
}
|
2013-10-22 18:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *HTTPAuthDecorator) ChangeRequest(req *http.Request) (*http.Request, error) {
|
|
|
|
req.SetBasicAuth(self.login, self.password)
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
2013-08-02 06:47:58 +00:00
|
|
|
// HTTPRequestFactory creates an HTTP request
|
|
|
|
// and applies a list of decorators on the request.
|
|
|
|
type HTTPRequestFactory struct {
|
|
|
|
decorators []HTTPRequestDecorator
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHTTPRequestFactory(d ...HTTPRequestDecorator) *HTTPRequestFactory {
|
2013-11-18 23:35:56 +00:00
|
|
|
return &HTTPRequestFactory{
|
|
|
|
decorators: d,
|
|
|
|
}
|
2013-08-02 06:47:58 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 16:11:35 +00:00
|
|
|
func (self *HTTPRequestFactory) AddDecorator(d ...HTTPRequestDecorator) {
|
2013-10-22 18:48:29 +00:00
|
|
|
self.decorators = append(self.decorators, d...)
|
|
|
|
}
|
|
|
|
|
2014-10-07 01:54:52 +00:00
|
|
|
func (self *HTTPRequestFactory) GetDecorators() []HTTPRequestDecorator {
|
|
|
|
return self.decorators
|
|
|
|
}
|
|
|
|
|
2013-08-02 06:47:58 +00:00
|
|
|
// NewRequest() creates a new *http.Request,
|
|
|
|
// applies all decorators in the HTTPRequestFactory on the request,
|
|
|
|
// then applies decorators provided by d on the request.
|
2013-11-18 23:35:56 +00:00
|
|
|
func (h *HTTPRequestFactory) NewRequest(method, urlStr string, body io.Reader, d ...HTTPRequestDecorator) (*http.Request, error) {
|
2013-08-02 06:47:58 +00:00
|
|
|
req, err := http.NewRequest(method, urlStr, body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-08-02 07:23:46 +00:00
|
|
|
|
|
|
|
// By default, a nil factory should work.
|
2013-11-18 23:35:56 +00:00
|
|
|
if h == nil {
|
2013-08-02 07:23:46 +00:00
|
|
|
return req, nil
|
|
|
|
}
|
2013-11-18 23:35:56 +00:00
|
|
|
for _, dec := range h.decorators {
|
2013-08-02 06:47:58 +00:00
|
|
|
req, err = dec.ChangeRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, dec := range d {
|
|
|
|
req, err = dec.ChangeRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2014-07-24 20:37:44 +00:00
|
|
|
log.Debugf("%v -- HEADERS: %v", req.URL, req.Header)
|
2013-08-02 06:47:58 +00:00
|
|
|
return req, err
|
|
|
|
}
|