github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/httputil/httputil.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package httputil
    15  
    16  import (
    17  	"net/http"
    18  
    19  	"github.com/pingcap/ticdc/cdc/model"
    20  	"github.com/pingcap/ticdc/pkg/security"
    21  )
    22  
    23  // Client wraps an HTTP client and support TLS requests.
    24  type Client struct {
    25  	http.Client
    26  }
    27  
    28  // NewClient creates an HTTP client with the given Credential.
    29  func NewClient(credential *security.Credential) (*Client, error) {
    30  	transport := http.DefaultTransport
    31  	if credential != nil {
    32  		tlsConf, err := credential.ToTLSConfig()
    33  		if err != nil {
    34  			return nil, err
    35  		}
    36  		if tlsConf != nil {
    37  			httpTrans := http.DefaultTransport.(*http.Transport).Clone()
    38  			httpTrans.TLSClientConfig = tlsConf
    39  			transport = httpTrans
    40  		}
    41  	}
    42  	// TODO: specific timeout in http client
    43  	return &Client{
    44  		Client: http.Client{Transport: transport},
    45  	}, nil
    46  }
    47  
    48  // IsFiltered return true if the given feedState matches the whiteList.
    49  func IsFiltered(whiteList string, feedState model.FeedState) bool {
    50  	if whiteList == "all" {
    51  		return true
    52  	}
    53  	if whiteList == "" {
    54  		switch feedState {
    55  		case model.StateNormal:
    56  			return true
    57  		case model.StateStopped:
    58  			return true
    59  		case model.StateFailed:
    60  			return true
    61  		}
    62  	}
    63  	return whiteList == string(feedState)
    64  }