Skip to content

Commit

Permalink
rm port
Browse files Browse the repository at this point in the history
  • Loading branch information
wenzuochao committed Jan 6, 2021
1 parent 2a316b9 commit 5da4c33
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
27 changes: 7 additions & 20 deletions tea/tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,20 +271,9 @@ func DoRequest(request *Request, requestRuntime map[string]interface{}) (respons
request.Protocol = String(strings.ToLower(StringValue(request.Protocol)))
}

if StringValue(request.Protocol) == "http" {
request.Port = Int(80)
} else if StringValue(request.Protocol) == "https" {
request.Port = Int(443)
}

requestURL := ""
request.Domain = request.Headers["host"]
matched, _ := regexp.MatchString(":", StringValue(request.Domain))
if matched {
requestURL = fmt.Sprintf("%s://%s%s", StringValue(request.Protocol), StringValue(request.Domain), StringValue(request.Pathname))
} else {
requestURL = fmt.Sprintf("%s://%s:%d%s", StringValue(request.Protocol), StringValue(request.Domain), IntValue(request.Port), StringValue(request.Pathname))
}
requestURL = fmt.Sprintf("%s://%s%s", StringValue(request.Protocol), StringValue(request.Domain), StringValue(request.Pathname))
queryParams := request.Query
// sort QueryParams by key
q := url.Values{}
Expand Down Expand Up @@ -425,15 +414,15 @@ func getHttpTransport(req *Request, runtime *RuntimeObject) (*http.Transport, er
&net.Dialer{
Timeout: time.Duration(IntValue(runtime.ConnectTimeout)) * time.Millisecond,
DualStack: true,
LocalAddr: getLocalAddr(StringValue(runtime.LocalAddr), IntValue(req.Port)),
LocalAddr: getLocalAddr(StringValue(runtime.LocalAddr)),
})
if err != nil {
return nil, err
}
trans.Dial = dialer.Dial
}
} else {
trans.DialContext = setDialContext(runtime, IntValue(req.Port))
trans.DialContext = setDialContext(runtime)
}
return trans, nil
}
Expand Down Expand Up @@ -521,22 +510,20 @@ func getSocks5Proxy(runtime *RuntimeObject) (proxy *url.URL, err error) {
return proxy, err
}

func getLocalAddr(localAddr string, port int) (addr *net.TCPAddr) {
func getLocalAddr(localAddr string) (addr *net.TCPAddr) {
if localAddr != "" {
addr = &net.TCPAddr{
Port: port,
IP: []byte(localAddr),
IP: []byte(localAddr),
}
}
return addr
}

func setDialContext(runtime *RuntimeObject, port int) func(cxt context.Context, net, addr string) (c net.Conn, err error) {
func setDialContext(runtime *RuntimeObject) func(cxt context.Context, net, addr string) (c net.Conn, err error) {
return func(ctx context.Context, network, address string) (net.Conn, error) {
if runtime.LocalAddr != nil && StringValue(runtime.LocalAddr) != "" {
netAddr := &net.TCPAddr{
Port: port,
IP: []byte(StringValue(runtime.LocalAddr)),
IP: []byte(StringValue(runtime.LocalAddr)),
}
return (&net.Dialer{
Timeout: time.Duration(IntValue(runtime.ConnectTimeout)) * time.Second,
Expand Down
3 changes: 1 addition & 2 deletions tea/tea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,6 @@ func Test_DoRequest(t *testing.T) {
}
}
request := NewRequest()
request.Port = Int(80)
request.Method = String("TEA TEST")
resp, err := DoRequest(request, nil)
utils.AssertNil(t, resp)
Expand Down Expand Up @@ -564,7 +563,7 @@ func Test_getHttpProxy(t *testing.T) {

func Test_SetDialContext(t *testing.T) {
runtime := &RuntimeObject{}
dialcontext := setDialContext(runtime, 80)
dialcontext := setDialContext(runtime)
ctx, cancelFunc := context.WithTimeout(context.Background(), 1*time.Second)
utils.AssertNotNil(t, cancelFunc)
c, err := dialcontext(ctx, "127.0.0.1", "127.0.0.2")
Expand Down

0 comments on commit 5da4c33

Please sign in to comment.