Skip to content

Conversation

@rafalmnich
Copy link

No description provided.

@coveralls
Copy link

coveralls commented Sep 5, 2018

Coverage Status

Coverage remained the same at 98.754% when pulling 9b984ae on query_string_normalizer into 3680aa6 on master.


// WithQueryNormalizer fixes wrong php query string handling as array
func WithQueryNormalizer(h http.Handler) http.Handler {
rxp, e := regexp.Compile("\\[[0-9]+\\]")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e should be err

}

func getNormalizedValue(key string, qVal []string, rxp *regexp.Regexp) (string, []string) {
isNastyArray := rxp.Match([]byte(key))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would think twice about using Regex here. It is very expensive.

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()

normalizedQuery := make(url.Values, 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either use url.Values{} or optimise for the expected length. The latter is preferable.

func WithQueryNormalizer(h http.Handler) http.Handler {
rxp, e := regexp.Compile("\\[[0-9]+\\]")
if e != nil {
panic(e)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is panicing here a good idea?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only reason why there can be error is badly created regexp. If so, then why not panic?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose it is ok in this case, but rather use MustCompile.

})
}

func getNormalizedValue(key string, qVal []string, rxp *regexp.Regexp) (string, []string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you normalising the value or the key?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think normizeKey would be better

func WithQueryNormalizer(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if i := strings.Index(r.URL.RawQuery, "["); i == -1 {
return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to delegate the request before returning

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ofc.

// WithQueryNormalizer fixes wrong php query string handling as array
func WithQueryNormalizer(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if i := strings.Index(r.URL.RawQuery, "["); i == -1 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i is not needed here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true

})
}

func getNormalizedValue(key string) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

naming: You are not normalising a value. Call it normalizeQueryKey instead

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

}

q := r.URL.Query()
normalizedQuery := make(url.Values, 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pessimistically assign the map size

Copy link
Contributor

@michalkurzeja michalkurzeja Sep 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use len(q), but for small maps it would make very little to no difference. If we needlessly allocated more space than needed, it could get, in fact, slower. I would leave it up to @rafalmnich .

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing anything in benchmarks with even for query with many changes like:
/?param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val&param[smth][0]=zero&param[smth][0][val][1][asdf][4][asetrhyty][5][ewrytu][7][aerterwert][7]=val

q := r.URL.Query()
normalizedQuery := make(url.Values, 0)

for key, qVal := range q {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

naming: qVal should be vals

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@rafalmnich rafalmnich merged commit 1f81d98 into master Sep 6, 2018
@rafalmnich rafalmnich deleted the query_string_normalizer branch September 6, 2018 11:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants