From cd4d0004fadd61a860fbb9ef70e42bcf956e0b49 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 13 May 2011 15:43:46 -0700 Subject: http: add Request.SetBasicAuth method R=golang-dev, dsymonds, rsc CC=golang-dev https://golang.org/cl/4543050 --- src/pkg/http/request.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/pkg/http/request.go') diff --git a/src/pkg/http/request.go b/src/pkg/http/request.go index 8545d75660..353b1c62c9 100644 --- a/src/pkg/http/request.go +++ b/src/pkg/http/request.go @@ -12,6 +12,7 @@ import ( "bufio" "crypto/tls" "container/vector" + "encoding/base64" "fmt" "io" "io/ioutil" @@ -479,6 +480,18 @@ func NewRequest(method, url string, body io.Reader) (*Request, os.Error) { return req, nil } +// SetBasicAuth sets the request's Authorization header to use HTTP +// Basic Authentication with the provided username and password. +// +// With HTTP Basic Authentication the provided username and password +// are not encrypted. +func (r *Request) SetBasicAuth(username, password string) { + s := username + ":" + password + buf := make([]byte, base64.StdEncoding.EncodedLen(len(s))) + base64.StdEncoding.Encode(buf, []byte(s)) + r.Header.Set("Authorization", "Basic "+string(buf)) +} + // ReadRequest reads and parses a request from b. func ReadRequest(b *bufio.Reader) (req *Request, err os.Error) { -- cgit v1.3-5-g9baa