Skip to content

Commit

Permalink
Sort headers alphabetically
Browse files Browse the repository at this point in the history
  • Loading branch information
falzm committed Feb 2, 2016
1 parent 207a923 commit 60ce565
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/httpdump/httpdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"sort"
"strings"

"github.com/fatih/color"
Expand All @@ -16,8 +17,10 @@ func dump(w http.ResponseWriter, r *http.Request) {

defer r.Body.Close()

for headerName, headerValue := range r.Header {
fmt.Printf("%s %s: %s\n", color.YellowString("[H]"), headerName, headerValue[0])
r.Header.Set("Host", r.Host)

for _, header := range sortHeaderKeys(r.Header) {
fmt.Printf("%s %s: %s\n", color.YellowString("[H]"), header, r.Header.Get(header))
}

body, err := ioutil.ReadAll(r.Body)
Expand Down Expand Up @@ -63,3 +66,15 @@ func getContentType(input interface{}) string {

return contentType
}

func sortHeaderKeys(headers http.Header) []string {
var keys []string

for k := range headers {
keys = append(keys, k)
}

sort.Strings(keys)

return keys
}

0 comments on commit 60ce565

Please sign in to comment.