Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
rbarazzutti committed Jul 20, 2023
1 parent bb086cc commit 64b5fad
Showing 1 changed file with 60 additions and 50 deletions.
110 changes: 60 additions & 50 deletions app/httpping.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,68 +37,78 @@ type httpPingImpl struct {
logger logger
}

// NewHTTPPing builds a new instance of HTTPPing or error if something goes wrong
func NewHTTPPing(config *Config, stdout io.Writer) (HTTPPing, error) {
type httpPingTestVersion struct {
baseConfig *Config
advertisedHttp3 bool
}

runtimeConfig := &RuntimeConfig{
RedirectCallBack: func(url string) {
_, _ = fmt.Fprintf(stdout, " ─→ Redirected to %s\n\n", url)
},
func (h httpPingTestVersion) Run() error {
http1 := h.checkHttp(func(c *Config) {
c.DisableHTTP2 = true
}, "HTTP/1")

http2 := h.checkHttp(func(c *Config) {
// default is HTTP/2
}, "HTTP/2")

http3 := h.checkHttp(func(c *Config) {
c.Http3 = true
}, "HTTP/3")
println("Checking available versions of HTTP protocol on " + h.baseConfig.Target)
println()
println(" - v1 " + <-http1)
println(" - v2 " + <-http2)
println(" - v3 " + <-http3)
println()
if h.advertisedHttp3 {
println(" (*) advertises HTTP/3 availability in HTTP headers")
}
return nil
}

if config.TestVersion {
advertisedHttp3 := false
checkHttp := func(prep func(*Config), prefix string) <-chan (string) {
r := make(chan string)

go func() {
configCopy := *config
func (h httpPingTestVersion) checkHttp(prep func(*Config), prefix string) <-chan string {
r := make(chan string)

configCopy.DisableHTTP2 = false
configCopy.Http3 = false
prep(&configCopy)
go func() {
configCopy := *h.baseConfig

rc := RuntimeConfig{}
wc, _ := newWebClient(&configCopy, &rc)
m := wc.DoMeasure(false)
configCopy.DisableHTTP2 = false
configCopy.Http3 = false
prep(&configCopy)

http3Advertisement := ""
rc := RuntimeConfig{}
wc, _ := newWebClient(&configCopy, &rc)
m := wc.DoMeasure(false)

if m != nil && strings.HasPrefix(m.Proto, prefix) && !m.IsFailure {
if m.AltSvcH3 != "" {
advertisedHttp3 = true
http3Advertisement = " (*)"
}
http3Advertisement := ""

r <- "\u001B[32m✓\u001B[0m " + m.Proto + http3Advertisement
}
r <- "\u001B[31m✗\u001B[0m not available"
}()
if m != nil && strings.HasPrefix(m.Proto, prefix) && !m.IsFailure {
if m.AltSvcH3 != "" {
h.advertisedHttp3 = true
http3Advertisement = " (*)"
}

return r
r <- "\u001B[32m✓\u001B[0m " + m.Proto + http3Advertisement
}
r <- "\u001B[31m✗\u001B[0m not available"
}()

http1 := checkHttp(func(c *Config) {
c.DisableHTTP2 = true
}, "HTTP/1")

http2 := checkHttp(func(c *Config) {
}, "HTTP/2")

http3 := checkHttp(func(c *Config) {
c.Http3 = true
}, "HTTP/3")
println("Checking available versions of HTTP protocol on " + config.Target)
println()
println(" - v1 " + <-http1)
println(" - v2 " + <-http2)
println(" - v3 " + <-http3)
println()
if advertisedHttp3 {
println(" (*) advertises HTTP/3 availability in HTTP headers")
}
return r
}

os.Exit(0)
// NewHTTPPing builds a new instance of HTTPPing or error if something goes wrong
func NewHTTPPing(config *Config, stdout io.Writer) (HTTPPing, error) {

if config.TestVersion {
return httpPingTestVersion{
baseConfig: config,
}, nil
}

runtimeConfig := &RuntimeConfig{
RedirectCallBack: func(url string) {
_, _ = fmt.Fprintf(stdout, " ─→ Redirected to %s\n\n", url)
},
}

pinger, err := NewPinger(config, runtimeConfig)
Expand Down

0 comments on commit 64b5fad

Please sign in to comment.