Skip to content

Commit

Permalink
optimize: sort keys
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jin-gou committed Feb 2, 2023
1 parent 741e579 commit f77e2d0
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions http2curl_fasthttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package http2curl

import (
"fmt"
"sort"
"strings"

"github.com/valyala/fasthttp"
Expand Down Expand Up @@ -33,18 +34,22 @@ func GetCurlCommandFastHttp(req *fasthttp.Request) (*CurlCommand, error) {
}

headers := make(map[string][]string)
keys := make([]string, 0)

req.Header.VisitAll(func(key, value []byte) {
_, ok := headers[b2s(key)]
if !ok {
keys = append(keys, b2s(key))
headers[b2s(key)] = []string{b2s(value)}
} else {
headers[b2s(key)] = append(headers[b2s(key)], b2s(value))
}
})

for key, values := range headers {
command.append("-H", bashEscape(fmt.Sprintf("%s: %s", key, strings.Join(values, " "))))
sort.Strings(keys)

for _, key := range keys {
command.append("-H", bashEscape(fmt.Sprintf("%s: %s", key, strings.Join(headers[key], " "))))
}

command.append(bashEscape(requestURL))
Expand Down

0 comments on commit f77e2d0

Please sign in to comment.