elazarl / goproxy

An HTTP proxy library for Go
BSD 3-Clause "New" or "Revised" License
5.89k stars 1.07k forks source link

Improve the https scheme matching #508

Open ryoii opened 11 months ago

ryoii commented 11 months ago
httpsRegexp     = regexp.MustCompile(`^https:\/\/`)

if !httpsRegexp.MatchString(req.URL.String()) {
    req.URL, err = url.Parse("https://" + r.Host + req.URL.String())
}

There is some code like above. In this case, it is a little bit inappropriate to use the regular expression. This regular expression is used to determine whether the url starts with https scheme. Here, a simple HasPrefix is fine

I write a benchmark to show this.

var httpsRegexp = regexp.MustCompile(`^https:\/\/`)

// BenchmarkMatch-12       11180230                99.44 ns/op
func BenchmarkMatch(b *testing.B) {
    b.Run("use-regex", func(b *testing.B) {
        for i := 0; i < b.N; i++ {
            httpsRegexp.MatchString("https://abc.abc.abc")
        }
    })

    b.Run("use-prefix", func(b *testing.B) {
        for i := 0; i < b.N; i++ {
            strings.HasPrefix("https://abc.abc.abc", "https")
        }
    })
}

// BenchmarkMatch
// BenchmarkMatch/use-regex
// BenchmarkMatch/use-regex-12             11810976               100.2 ns/op
// BenchmarkMatch/use-prefix
// BenchmarkMatch/use-prefix-12            347787476                3.451 ns/op
// PASS

It seems better.