Skip to content

Commit

Permalink
Refactor: share excluded headers between http2 and http3
Browse files Browse the repository at this point in the history
  • Loading branch information
imroc committed Jul 31, 2023
1 parent f0b2f73 commit 68815fd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 40 deletions.
28 changes: 28 additions & 0 deletions internal/header/header.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package header

import "strings"

const (
DefaultUserAgent = "req/v3 (https://github.com/imroc/req)"
UserAgent = "User-Agent"
Expand All @@ -14,3 +16,29 @@ const (
HeaderOderKey = "__header_order__"
PseudoHeaderOderKey = "__pseudo_header_order__"
)

var reqWriteExcludeHeader = map[string]bool{
// Host is :authority, already sent.
// Content-Length is automatic.
"host": true,
"content-length": true,
// Per 8.1.2.2 Connection-Specific Header
// Fields, don't send connection-specific
// fields. We have already checked if any
// are error-worthy so just ignore the rest.
"connection": true,
"proxy-connection": true,
"transfer-encoding": true,
"upgrade": true,
"keep-alive": true,
// Ignore header order keys which is only used internally.
HeaderOderKey: true,
PseudoHeaderOderKey: true,
}

func IsExcluded(key string) bool {
if reqWriteExcludeHeader[strings.ToLower(key)] {
return true
}
return false
}
21 changes: 1 addition & 20 deletions internal/http2/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -1748,25 +1748,6 @@ func (cs *clientStream) awaitFlowControl(maxBytes int) (taken int32, err error)
}
}

var reqWriteExcludeHeader = map[string]bool{
// Host is :authority, already sent.
// Content-Length is automatic.
"host": true,
"content-length": true,
// Per 8.1.2.2 Connection-Specific Header
// Fields, don't send connection-specific
// fields. We have already checked if any
// are error-worthy so just ignore the rest.
"connection": true,
"proxy-connection": true,
"transfer-encoding": true,
"upgrade": true,
"keep-alive": true,
// Ignore header order keys which is only used internally.
header.HeaderOderKey: true,
header.PseudoHeaderOderKey: true,
}

var errNilRequestURL = errors.New("http2: Request.URI is nil")

// requires cc.wmu be held.
Expand Down Expand Up @@ -1884,7 +1865,7 @@ func (cc *ClientConn) encodeHeaders(req *http.Request, addGzipHeader bool, trail

var didUA bool
for k, vv := range req.Header {
if reqWriteExcludeHeader[strings.ToLower(k)] {
if header.IsExcluded(k) {
continue
} else if ascii.EqualFold(k, "user-agent") {
// Match Go's http1 behavior: at most one
Expand Down
21 changes: 1 addition & 20 deletions internal/http3/request_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,6 @@ func (w *requestWriter) writeHeaders(wr io.Writer, req *http.Request, gzip bool,
return err
}

var reqWriteExcludeHeader = map[string]bool{
// Host is :authority, already sent.
// Content-Length is automatic.
"host": true,
"content-length": true,
// Per 8.1.2.2 Connection-Specific Header
// Fields, don't send connection-specific
// fields. We have already checked if any
// are error-worthy so just ignore the rest.
"connection": true,
"proxy-connection": true,
"transfer-encoding": true,
"upgrade": true,
"keep-alive": true,
// Ignore header order keys which is only used internally.
header.HeaderOderKey: true,
header.PseudoHeaderOderKey: true,
}

// copied from net/transport.go
// Modified to support Extended CONNECT:
// Contrary to what the godoc for the http.Request says,
Expand Down Expand Up @@ -207,7 +188,7 @@ func (w *requestWriter) encodeHeaders(req *http.Request, addGzipHeader bool, tra

var didUA bool
for k, vv := range req.Header {
if reqWriteExcludeHeader[strings.ToLower(k)] {
if header.IsExcluded(k) {
continue
} else if strings.EqualFold(k, "user-agent") {
// Match Go's http1 behavior: at most one
Expand Down

0 comments on commit 68815fd

Please sign in to comment.