From: Ivan Krasin Date: Wed, 16 Dec 2009 00:27:45 +0000 (-0800) Subject: Add basic http authentication support. X-Git-Tag: weekly.2009-12-22~47 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=8e2608eca8960819791eb4d89b3028e26a29498d;p=gostls13.git Add basic http authentication support. Fixes #407. R=rsc, ajstarks CC=ushakov https://golang.org/cl/176076 --- diff --git a/src/pkg/http/client.go b/src/pkg/http/client.go index aa3d3be6dc..af11a4b745 100644 --- a/src/pkg/http/client.go +++ b/src/pkg/http/client.go @@ -8,6 +8,7 @@ package http import ( "bufio" + "encoding/base64" "fmt" "io" "net" @@ -118,6 +119,16 @@ func send(req *Request) (resp *Response, err os.Error) { if !hasPort(addr) { addr += ":http" } + info := req.URL.Userinfo + if len(info) > 0 { + enc := base64.URLEncoding + encoded := make([]byte, enc.EncodedLen(len(info))) + enc.Encode(encoded, strings.Bytes(info)) + if req.Header == nil { + req.Header = make(map[string]string) + } + req.Header["Authorization"] = "Basic " + string(encoded) + } conn, err := net.Dial("tcp", "", addr) if err != nil { return nil, err