GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

credentials/internal/http/http.go   A
last analyzed

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 93
dl 0
loc 141
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
F http.Do 0 79 15
A http.*Request.BuildRequestURL 0 12 3
1
package http
2
3
import (
4
	"context"
5
	"fmt"
6
	"io"
7
	"io/ioutil"
8
	"net"
9
	"net/http"
10
	"net/url"
11
	"strings"
12
	"time"
13
14
	"github.com/alibabacloud-go/debug/debug"
15
	"github.com/aliyun/credentials-go/credentials/internal/utils"
16
)
17
18
type Request struct {
19
	Method         string // http request method
20
	URL            string // http url
21
	Protocol       string // http or https
22
	Host           string // http host
23
	ReadTimeout    time.Duration
24
	ConnectTimeout time.Duration
25
	Proxy          string            // http proxy
26
	Form           map[string]string // http form
27
	Body           []byte            // request body for JSON or stream
28
	Path           string
29
	Queries        map[string]string
30
	Headers        map[string]string
31
}
32
33
func (req *Request) BuildRequestURL() string {
34
	httpUrl := fmt.Sprintf("%s://%s%s", req.Protocol, req.Host, req.Path)
35
	if req.URL != "" {
36
		httpUrl = req.URL
37
	}
38
39
	querystring := utils.GetURLFormedMap(req.Queries)
40
	if querystring != "" {
41
		httpUrl = httpUrl + "?" + querystring
42
	}
43
44
	return fmt.Sprintf("%s %s", req.Method, httpUrl)
45
}
46
47
type Response struct {
48
	StatusCode int
49
	Headers    map[string]string
50
	Body       []byte
51
}
52
53
var newRequest = http.NewRequest
54
55
type do func(req *http.Request) (*http.Response, error)
56
57
var hookDo = func(fn do) do {
58
	return fn
59
}
60
61
var debuglog = debug.Init("credential")
62
63
func Do(req *Request) (res *Response, err error) {
64
	querystring := utils.GetURLFormedMap(req.Queries)
65
	// do request
66
	httpUrl := fmt.Sprintf("%s://%s%s?%s", req.Protocol, req.Host, req.Path, querystring)
67
	if req.URL != "" {
68
		httpUrl = req.URL
69
	}
70
71
	var body io.Reader
72
	if req.Method == "GET" {
73
		body = strings.NewReader("")
74
	} else {
75
		body = strings.NewReader(utils.GetURLFormedMap(req.Form))
76
	}
77
78
	httpRequest, err := newRequest(req.Method, httpUrl, body)
79
	if err != nil {
80
		return
81
	}
82
83
	if req.Form != nil {
84
		httpRequest.Header["Content-Type"] = []string{"application/x-www-form-urlencoded"}
85
	}
86
87
	for key, value := range req.Headers {
88
		if value != "" {
89
			debuglog("> %s: %s", key, value)
90
			httpRequest.Header.Set(key, value)
91
		}
92
	}
93
94
	httpClient := &http.Client{}
95
96
	if req.ReadTimeout != 0 {
97
		httpClient.Timeout = req.ReadTimeout + req.ConnectTimeout
98
	}
99
100
	transport := http.DefaultTransport.(*http.Transport).Clone()
101
	if req.Proxy != "" {
102
		var proxy *url.URL
103
		proxy, err = url.Parse(req.Proxy)
104
		if err != nil {
105
			return
106
		}
107
		transport.Proxy = http.ProxyURL(proxy)
108
	}
109
110
	if req.ConnectTimeout != 0 {
111
		transport.DialContext = func(ctx context.Context, network, address string) (net.Conn, error) {
112
			return (&net.Dialer{
113
				Timeout:   req.ConnectTimeout,
114
				DualStack: true,
115
			}).DialContext(ctx, network, address)
116
		}
117
	}
118
119
	httpClient.Transport = transport
120
121
	httpResponse, err := hookDo(httpClient.Do)(httpRequest)
122
	if err != nil {
123
		return
124
	}
125
126
	defer httpResponse.Body.Close()
127
128
	responseBody, err := ioutil.ReadAll(httpResponse.Body)
129
	if err != nil {
130
		return
131
	}
132
	res = &Response{
133
		StatusCode: httpResponse.StatusCode,
134
		Headers:    make(map[string]string),
135
		Body:       responseBody,
136
	}
137
	for key, v := range httpResponse.Header {
138
		res.Headers[key] = v[0]
139
	}
140
141
	return
142
}
143