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.

http.*Request.BuildRequestURL   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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