From 13eb2fe87bed82582febf6178c5c3e1a545cc14f Mon Sep 17 00:00:00 2001 From: shin- Date: Tue, 22 Oct 2013 20:48:29 +0200 Subject: [PATCH] Added HTTPAuthDecorator Upstream-commit: bbf9135adcaec9edc2e9d0ebce6a78ba3ade3689 Component: engine --- components/engine/utils/http.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/components/engine/utils/http.go b/components/engine/utils/http.go index 5eb77d1949..463d3b4fdd 100644 --- a/components/engine/utils/http.go +++ b/components/engine/utils/http.go @@ -107,6 +107,23 @@ func (h *HTTPMetaHeadersDecorator) ChangeRequest(req *http.Request) (newReq *htt return req, nil } +type HTTPAuthDecorator struct { + login string + password string +} + +func NewHTTPAuthDecorator(login, password string) HTTPRequestDecorator { + ret := new(HTTPAuthDecorator) + ret.login = login + ret.password = password + return ret +} + +func (self *HTTPAuthDecorator) ChangeRequest(req *http.Request) (*http.Request, error) { + req.SetBasicAuth(self.login, self.password) + return req, nil +} + // HTTPRequestFactory creates an HTTP request // and applies a list of decorators on the request. type HTTPRequestFactory struct { @@ -119,6 +136,10 @@ func NewHTTPRequestFactory(d ...HTTPRequestDecorator) *HTTPRequestFactory { } } +func (self *HTTPRequestFactory) AddDecorator(d... HTTPRequestDecorator) { + self.decorators = append(self.decorators, d...) +} + // NewRequest() creates a new *http.Request, // applies all decorators in the HTTPRequestFactory on the request, // then applies decorators provided by d on the request. @@ -144,5 +165,6 @@ func (h *HTTPRequestFactory) NewRequest(method, urlStr string, body io.Reader, d return nil, err } } + Debugf("%v -- HEADERS: %v", req.URL, req.Header) return req, err }