forked from rainycape/gondola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
56 lines (50 loc) · 1.4 KB
/
http.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
package oauth
import (
"errors"
"net/http"
"net/url"
"strings"
"gondola/net/httpclient"
)
var (
NoRedirectAllowedError = errors.New("redirects not allowed with oAuth")
)
func preventRedirect(req *http.Request, via []*http.Request) error {
return NoRedirectAllowedError
}
func req(method, url string, headers map[string]string, values url.Values) (*http.Request, error) {
var req *http.Request
var err error
// We have to do all this dancing because due to the way
// interfaces are implemented, if we define a variable
// for the request body and not set it, NewRequest will
// get an interface to a nil pointer rather than a "bare nil"
// and will not work properly.
if len(values) > 0 {
if method == "GET" || method == "HEAD" {
url = url + "?" + values.Encode()
req, err = http.NewRequest(method, url, nil)
} else {
req, err = http.NewRequest(method, url, strings.NewReader(values.Encode()))
if req != nil {
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
}
}
} else {
req, err = http.NewRequest(method, url, nil)
}
if err != nil {
return nil, err
}
for k, v := range headers {
req.Header.Add(k, v)
}
return req, nil
}
func (c *Consumer) sendReq(method, url string, headers map[string]string, values url.Values) (*httpclient.Response, error) {
r, err := req(method, url, headers, values)
if err != nil {
return nil, err
}
return c.client().Do(r)
}