1
0
mirror of https://github.com/golang/go synced 2024-11-12 10:30:23 -07:00

Add basic http authentication support.

Fixes #407.

R=rsc, ajstarks
CC=ushakov
https://golang.org/cl/176076
This commit is contained in:
Ivan Krasin 2009-12-15 16:27:45 -08:00 committed by Russ Cox
parent 5d754bfaea
commit 8e2608eca8

View File

@ -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