Skip to content

Commit

Permalink
webclient refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
rbarazzutti committed Dec 27, 2021
1 parent a14c845 commit de49390
Showing 1 changed file with 54 additions and 48 deletions.
102 changes: 54 additions & 48 deletions app/webclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"crypto/tls"
"crypto/x509"
"fever.ch/http-ping/net/sockettrace"
"fever.ch/http-ping/stats"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -161,20 +160,59 @@ func (webClient *webClientImpl) URL() string {
return webClient.url.String()
}

func (webClient *webClientImpl) checkRedirectFollow(req *http.Request, _ []*http.Request) error {
webClient.config.Target = req.URL.String()
webClient.url = req.URL
if webClient.runtimeConfig.RedirectCallBack != nil {
webClient.runtimeConfig.RedirectCallBack(req.URL.String())
}
updateConnTarget(webClient)
return nil
}

func (webClient *webClientImpl) prepareReq(req *http.Request) {
if len(webClient.config.Parameters) > 0 || webClient.config.ExtraParam {
q := req.URL.Query()

if webClient.config.ExtraParam {
q.Add("extra_parameter_http_ping", fmt.Sprintf("%d", time.Now().UnixMicro()))
}

for _, c := range webClient.config.Parameters {
q.Add(c.Name, c.Value)
}
req.URL.RawQuery = q.Encode()
}

req.Header.Set("User-Agent", webClient.config.UserAgent)
if webClient.config.Referrer != "" {
req.Header.Set("Referer", webClient.config.Referrer)
}

if webClient.config.AuthUsername != "" || webClient.config.AuthPassword != "" {
req.SetBasicAuth(webClient.config.AuthUsername, webClient.config.AuthPassword)
}

// Host is considered as a special header in net/http, for simplicity we use here a common way to handle both
for _, header := range webClient.config.Headers {
if strings.ToLower(header.Name) != "host" {
req.Header.Set(header.Name, header.Value)
} else {
req.Host = header.Value
}
}

}

// DoMeasure evaluates the latency to a specific HTTP/S server
func (webClient *webClientImpl) DoMeasure(followRedirect bool) *HTTPMeasure {

webClient.httpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if followRedirect {
webClient.config.Target = req.URL.String()
webClient.url = req.URL
if webClient.runtimeConfig.RedirectCallBack != nil {
webClient.runtimeConfig.RedirectCallBack(req.URL.String())
}
updateConnTarget(webClient)
return nil
if followRedirect {
webClient.httpClient.CheckRedirect = webClient.checkRedirectFollow
} else {
webClient.httpClient.CheckRedirect = func(_ *http.Request, _ []*http.Request) error {
return http.ErrUseLastResponse
}
return http.ErrUseLastResponse
}

req, _ := http.NewRequest(webClient.config.Method, webClient.config.Target, nil)
Expand All @@ -193,6 +231,7 @@ func (webClient *webClientImpl) DoMeasure(followRedirect bool) *HTTPMeasure {
var reused bool
var remoteAddr string

totalTimer := newTimer()
connTimer := newTimer()
dnsTimer := newTimer()
tlsTimer := newTimer()
Expand All @@ -202,7 +241,6 @@ func (webClient *webClientImpl) DoMeasure(followRedirect bool) *HTTPMeasure {
responseTimer := newTimer()

clientTrace := &httptrace.ClientTrace{

TLSHandshakeStart: func() {
tlsTimer.start()
},
Expand Down Expand Up @@ -232,7 +270,6 @@ func (webClient *webClientImpl) DoMeasure(followRedirect bool) *HTTPMeasure {
WroteRequest: func(info httptrace.WroteRequestInfo) {
reqTimer.stop()
waitTimer.start()

},

GotFirstResponseByte: func() {
Expand Down Expand Up @@ -261,40 +298,9 @@ func (webClient *webClientImpl) DoMeasure(followRedirect bool) *HTTPMeasure {

req = req.WithContext(traceCtx)

if len(webClient.config.Parameters) > 0 || webClient.config.ExtraParam {
q := req.URL.Query()

if webClient.config.ExtraParam {
q.Add("extra_parameter_http_ping", fmt.Sprintf("%d", time.Now().UnixMicro()))
}

for _, c := range webClient.config.Parameters {
q.Add(c.Name, c.Value)
}
req.URL.RawQuery = q.Encode()
}

req.Header.Set("User-Agent", webClient.config.UserAgent)
if webClient.config.Referrer != "" {
req.Header.Set("Referer", webClient.config.Referrer)
}

if webClient.config.AuthUsername != "" || webClient.config.AuthPassword != "" {
req.SetBasicAuth(webClient.config.AuthUsername, webClient.config.AuthPassword)
}

// Host is considered as a special header in net/http, for simplicity we use here a common way to handle both
for _, header := range webClient.config.Headers {
if strings.ToLower(header.Name) != "host" {
req.Header.Set(header.Name, header.Value)
} else {
req.Host = header.Value
}
}

start := time.Now()
//reqTimer.start()
webClient.prepareReq(req)

totalTimer.start()
res, err := webClient.httpClient.Do(req)

if err != nil {
Expand All @@ -314,7 +320,7 @@ func (webClient *webClientImpl) DoMeasure(followRedirect bool) *HTTPMeasure {

_ = res.Body.Close()
responseTimer.stop()
var d = time.Since(start)
totalTimer.stop()

failed := false
failureCause := ""
Expand All @@ -339,7 +345,7 @@ func (webClient *webClientImpl) DoMeasure(followRedirect bool) *HTTPMeasure {

return &HTTPMeasure{
Proto: res.Proto,
TotalTime: stats.Measure(d),
TotalTime: totalTimer.measure(),
StatusCode: res.StatusCode,
Bytes: s,
InBytes: i,
Expand Down

0 comments on commit de49390

Please sign in to comment.