-
Notifications
You must be signed in to change notification settings - Fork 12
/
http_auth.go
165 lines (141 loc) · 3.85 KB
/
http_auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// Copyright 2009 Bill Casarin <billcasarin@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// From gotweet - a command line twitter client by Dmitry Chestnykh
// modified by Bill Casarin
//
package twitter
import (
"net/http"
"encoding/base64"
"io"
"strings"
"net"
"bufio"
"fmt"
"bytes"
"net/url"
)
type readClose struct {
io.Reader
io.Closer
}
type badStringError struct {
what string
str string
}
func (e *badStringError) Error() string { return fmt.Sprintf("%s %q", e.what, e.str) }
// Given a string of the form "host", "host:port", or "[ipv6::address]:port",
// return true if the string includes a port.
func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") }
func send(req *http.Request) (resp *http.Response, err error) {
addr := req.URL.Host
if !hasPort(addr) {
addr += ":http"
}
conn, err := net.Dial("tcp", addr)
if err != nil {
return nil, err
}
err = req.Write(conn)
if err != nil {
conn.Close()
return nil, err
}
reader := bufio.NewReader(conn)
resp, err = http.ReadResponse(reader, req)
if err != nil {
conn.Close()
return nil, err
}
r := io.Reader(reader)
if n := resp.ContentLength; n != -1 {
r = io.LimitReader(r, n)
}
resp.Body = readClose{r, conn}
return
}
func encodedUsernameAndPassword(user, pwd string) string {
bb := &bytes.Buffer{}
encoder := base64.NewEncoder(base64.StdEncoding, bb)
encoder.Write([]byte(user + ":" + pwd))
encoder.Close()
return bb.String()
}
func authGet(url_, user, pwd string) (r *http.Response, err error) {
var req http.Request
h := make(http.Header)
h.Add("Authorization", "Basic "+
encodedUsernameAndPassword(user, pwd))
req.Header = h
if req.URL, err = url.Parse(url_); err != nil {
return
}
if r, err = send(&req); err != nil {
return
}
return
}
// Post issues a POST to the specified URL.
//
// Caller should close r.Body when done reading it.
func authPost(url_, user, pwd, client, clientURL, version, agent, bodyType string,
body io.Reader) (r *http.Response, err error) {
var req http.Request
req.Method = "POST"
req.Body = body.(io.ReadCloser)
h := make(http.Header)
h.Add("Content-Type", bodyType)
h.Add("Transfer-Encoding", "chunked")
h.Add("User-Agent", agent)
h.Add("X-Twitter-Client", client)
h.Add("X-Twitter-Client-URL", clientURL)
h.Add("X-Twitter-Version", version)
h.Add("Authorization", "Basic "+encodedUsernameAndPassword(user, pwd))
req.Header = h
req.URL, err = url.Parse(url_)
if err != nil {
return nil, err
}
return send(&req)
}
// Do an authenticated Get if we've called Authenticated, otherwise
// just Get it without authentication
func httpGet(url_, user, pass string) (*http.Response, error) {
var r *http.Response
var err error
if user != "" && pass != "" {
r, err = authGet(url_, user, pass)
} else {
r, err = http.Get(url_)
}
return r, err
}
// Do an authenticated Post if we've called Authenticated, otherwise
// just Post it without authentication
func httpPost(url_, user, pass, client, clientURL, version, agent,
data string) (*http.Response, error) {
var r *http.Response
var err error
body := bytes.NewBufferString(data)
bodyType := "application/x-www-form-urlencoded"
if user != "" && pass != "" {
r, err = authPost(url_, user, pass, client, clientURL,
version, agent, bodyType, body)
} else {
r, err = http.Post(url_, bodyType, body)
}
return r, err
}