| skipped 60 lines |
61 | 61 | | } |
62 | 62 | | |
63 | 63 | | if h.user != "" && h.pass != "" { |
64 | | - | auth := h.user + ":" + h.pass |
65 | | - | req.Header.Add("Proxy-Authorization", |
66 | | - | fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(auth)))) |
| 64 | + | req.Header.Set("Proxy-Authorization", basicAuth(h.user, h.pass)) |
67 | 65 | | } |
68 | 66 | | |
69 | 67 | | if err := req.Write(rw); err != nil { |
| skipped 5 lines |
75 | 73 | | return err |
76 | 74 | | } |
77 | 75 | | |
78 | | - | if resp.StatusCode == http.StatusOK { |
| 76 | + | switch resp.StatusCode { |
| 77 | + | case http.StatusOK: |
79 | 78 | | return nil |
80 | | - | } |
81 | | - | |
82 | | - | if resp.StatusCode == http.StatusProxyAuthRequired { |
83 | | - | return errors.New("HTTP need auth") |
84 | | - | } |
85 | | - | |
86 | | - | if resp.StatusCode == http.StatusMethodNotAllowed { |
| 79 | + | case http.StatusProxyAuthRequired: |
| 80 | + | return errors.New("HTTP auth required by proxy") |
| 81 | + | case http.StatusMethodNotAllowed: |
87 | 82 | | return errors.New("CONNECT method not allowed by proxy") |
| 83 | + | default: |
| 84 | + | return fmt.Errorf("HTTP connect status: %s", resp.Status) |
88 | 85 | | } |
| 86 | + | } |
89 | 87 | | |
90 | | - | if resp.StatusCode >= http.StatusInternalServerError { |
91 | | - | return errors.New(resp.Status) |
92 | | - | } |
93 | | - | |
94 | | - | return fmt.Errorf("HTTP connect status code: %d", resp.StatusCode) |
| 88 | + | // The Basic authentication scheme is based on the model that the client |
| 89 | + | // needs to authenticate itself with a user-id and a password for each |
| 90 | + | // protection space ("realm"). The realm value is a free-form string |
| 91 | + | // that can only be compared for equality with other realms on that |
| 92 | + | // server. The server will service the request only if it can validate |
| 93 | + | // the user-id and password for the protection space applying to the |
| 94 | + | // requested resource. |
| 95 | + | func basicAuth(username, password string) string { |
| 96 | + | auth := username + ":" + password |
| 97 | + | return base64.StdEncoding.EncodeToString([]byte(auth)) |
95 | 98 | | } |
96 | 99 | | |