Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1507)

Side by Side Diff: src/pkg/net/http/request.go

Issue 76540043: code review 76540043: net/http: add BasicAuth method to *http.Request
Patch Set: diff -r e5c87cefb57fffc5767209542be5c4e58123e3c6 https://code.google.com/p/go Created 9 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/pkg/net/http/request_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2009 The Go Authors. All rights reserved. 1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 // HTTP Request reading and parsing. 5 // HTTP Request reading and parsing.
6 6
7 package http 7 package http
8 8
9 import ( 9 import (
10 "bufio" 10 "bufio"
11 "bytes" 11 "bytes"
12 "crypto/tls" 12 "crypto/tls"
13 "encoding/base64"
13 "errors" 14 "errors"
14 "fmt" 15 "fmt"
15 "io" 16 "io"
16 "io/ioutil" 17 "io/ioutil"
17 "mime" 18 "mime"
18 "mime/multipart" 19 "mime/multipart"
19 "net/textproto" 20 "net/textproto"
20 "net/url" 21 "net/url"
21 "strconv" 22 "strconv"
22 "strings" 23 "strings"
(...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after
514 case *bytes.Reader: 515 case *bytes.Reader:
515 req.ContentLength = int64(v.Len()) 516 req.ContentLength = int64(v.Len())
516 case *strings.Reader: 517 case *strings.Reader:
517 req.ContentLength = int64(v.Len()) 518 req.ContentLength = int64(v.Len())
518 } 519 }
519 } 520 }
520 521
521 return req, nil 522 return req, nil
522 } 523 }
523 524
525 // BasicAuth returns the username and password provided in the request's
526 // Authorization header, if the request uses HTTP Basic Authentication.
527 // See RFC 2617, Section 2.
528 func (r *Request) BasicAuth() (username, password string, ok bool) {
529 auth := r.Header.Get("Authorization")
530 if auth == "" {
531 return
532 }
533 return parseBasicAuth(auth)
534 }
535
536 // parseBasicAuth parses an HTTP Basic Authentication string.
537 // "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" returns ("Aladdin", "open sesame", true) .
538 func parseBasicAuth(auth string) (username, password string, ok bool) {
539 if !strings.HasPrefix(auth, "Basic ") {
540 return
541 }
542 c, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(auth, "Basi c "))
543 if err != nil {
544 return
545 }
546 cs := string(c)
547 s := strings.IndexByte(cs, ':')
548 if s < 0 {
549 return
550 }
551 return cs[:s], cs[s+1:], true
552 }
553
524 // SetBasicAuth sets the request's Authorization header to use HTTP 554 // SetBasicAuth sets the request's Authorization header to use HTTP
525 // Basic Authentication with the provided username and password. 555 // Basic Authentication with the provided username and password.
526 // 556 //
527 // With HTTP Basic Authentication the provided username and password 557 // With HTTP Basic Authentication the provided username and password
528 // are not encrypted. 558 // are not encrypted.
529 func (r *Request) SetBasicAuth(username, password string) { 559 func (r *Request) SetBasicAuth(username, password string) {
530 r.Header.Set("Authorization", "Basic "+basicAuth(username, password)) 560 r.Header.Set("Authorization", "Basic "+basicAuth(username, password))
531 } 561 }
532 562
533 // parseRequestLine parses "GET /foo HTTP/1.1" into its three parts. 563 // parseRequestLine parses "GET /foo HTTP/1.1" into its three parts.
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after
881 911
882 func (r *Request) wantsClose() bool { 912 func (r *Request) wantsClose() bool {
883 return hasToken(r.Header.get("Connection"), "close") 913 return hasToken(r.Header.get("Connection"), "close")
884 } 914 }
885 915
886 func (r *Request) closeBody() { 916 func (r *Request) closeBody() {
887 if r.Body != nil { 917 if r.Body != nil {
888 r.Body.Close() 918 r.Body.Close()
889 } 919 }
890 } 920 }
OLDNEW
« no previous file with comments | « no previous file | src/pkg/net/http/request_test.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b