mirror of
https://github.com/golang/go
synced 2024-11-25 07:47:56 -07:00
http: sort header keys when writing Response or Request to wire
R=rsc CC=golang-dev https://golang.org/cl/203050
This commit is contained in:
parent
ce85868a00
commit
4fdab85178
@ -49,6 +49,8 @@ type badStringError struct {
|
|||||||
|
|
||||||
func (e *badStringError) String() string { return fmt.Sprintf("%s %q", e.what, e.str) }
|
func (e *badStringError) String() string { return fmt.Sprintf("%s %q", e.what, e.str) }
|
||||||
|
|
||||||
|
var reqExcludeHeader = map[string]int{"Host": 0, "User-Agent": 0, "Referer": 0}
|
||||||
|
|
||||||
// A Request represents a parsed HTTP request header.
|
// A Request represents a parsed HTTP request header.
|
||||||
type Request struct {
|
type Request struct {
|
||||||
Method string // GET, POST, PUT, etc.
|
Method string // GET, POST, PUT, etc.
|
||||||
@ -169,14 +171,7 @@ func (req *Request) Write(w io.Writer) os.Error {
|
|||||||
// from Request, and introduce Request methods along the lines of
|
// from Request, and introduce Request methods along the lines of
|
||||||
// Response.{GetHeader,AddHeader} and string constants for "Host",
|
// Response.{GetHeader,AddHeader} and string constants for "Host",
|
||||||
// "User-Agent" and "Referer".
|
// "User-Agent" and "Referer".
|
||||||
for k, v := range req.Header {
|
writeSortedKeyValue(w, req.Header, reqExcludeHeader)
|
||||||
// Host, User-Agent, and Referer were sent from structure fields
|
|
||||||
// above; ignore them if they also appear in req.Header.
|
|
||||||
if k == "Host" || k == "User-Agent" || k == "Referer" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
io.WriteString(w, k+": "+v+"\r\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
io.WriteString(w, "\r\n")
|
io.WriteString(w, "\r\n")
|
||||||
|
|
||||||
|
@ -8,12 +8,16 @@ package http
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var respExcludeHeader = map[string]int{}
|
||||||
|
|
||||||
// Response represents the response from an HTTP request.
|
// Response represents the response from an HTTP request.
|
||||||
//
|
//
|
||||||
type Response struct {
|
type Response struct {
|
||||||
@ -455,9 +459,7 @@ func (resp *Response) Write(w io.Writer) os.Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Rest of header
|
// Rest of header
|
||||||
for k, v := range resp.Header {
|
writeSortedKeyValue(w, resp.Header, respExcludeHeader)
|
||||||
io.WriteString(w, k+": "+v+"\r\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
// End-of-header
|
// End-of-header
|
||||||
io.WriteString(w, "\r\n")
|
io.WriteString(w, "\r\n")
|
||||||
@ -491,3 +493,19 @@ func (resp *Response) Write(w io.Writer) os.Error {
|
|||||||
// Success
|
// Success
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func writeSortedKeyValue(w io.Writer, kvm map[string]string, exclude map[string]int) {
|
||||||
|
kva := make([]string, len(kvm))
|
||||||
|
i := 0
|
||||||
|
for k, v := range kvm {
|
||||||
|
if _, exc := exclude[k]; !exc {
|
||||||
|
kva[i] = fmt.Sprint(k + ": " + v + "\r\n")
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
kva = kva[0:i]
|
||||||
|
sort.SortStrings(kva)
|
||||||
|
for _, l := range kva {
|
||||||
|
io.WriteString(w, l)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user