Skip to content

Commit f407dbb

Browse files
committed
Revert "TUN-8592: Use metadata from the edge to determine if request body is empty for QUIC transport"
This reverts commit e2064c8.
1 parent 92e0f5f commit f407dbb

File tree

2 files changed

+8
-164
lines changed

2 files changed

+8
-164
lines changed

connection/quic.go

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,12 @@ const (
4242
HTTPMethodKey = "HttpMethod"
4343
// HTTPHostKey is used to get or set http Method in QUIC ALPN if the underlying proxy connection type is HTTP.
4444
HTTPHostKey = "HttpHost"
45-
// HTTPRequestBodyHintKey is used in ConnectRequest metadata to indicate if the request has body
46-
HTTPRequestBodyHintKey = "HttpReqBodyHint"
4745

4846
QUICMetadataFlowID = "FlowID"
4947
// emperically this capacity has been working well
5048
demuxChanCapacity = 16
5149
)
5250

53-
type RequestBodyHint uint64
54-
55-
const (
56-
RequestBodyHintMissing RequestBodyHint = iota
57-
RequestBodyHintEmpty
58-
RequestBodyHintHasData
59-
)
60-
61-
func (rbh RequestBodyHint) String() string {
62-
return [...]string{"missing", "empty", "data"}[rbh]
63-
}
64-
6551
var (
6652
portForConnIndex = make(map[uint8]int, 0)
6753
portMapMutex sync.Mutex
@@ -500,6 +486,7 @@ func buildHTTPRequest(
500486
dest := connectRequest.Dest
501487
method := metadata[HTTPMethodKey]
502488
host := metadata[HTTPHostKey]
489+
isWebsocket := connectRequest.Type == pogs.ConnectionTypeWebsocket
503490

504491
req, err := http.NewRequestWithContext(ctx, method, dest, body)
505492
if err != nil {
@@ -524,8 +511,13 @@ func buildHTTPRequest(
524511
return nil, fmt.Errorf("Error setting content-length: %w", err)
525512
}
526513

527-
if shouldSetRequestBodyToEmpty(connectRequest, metadata, req) {
528-
log.Debug().Str("host", req.Host).Str("method", req.Method).Msg("Set request to have no body")
514+
// Go's client defaults to chunked encoding after a 200ms delay if the following cases are true:
515+
// * the request body blocks
516+
// * the content length is not set (or set to -1)
517+
// * the method doesn't usually have a body (GET, HEAD, DELETE, ...)
518+
// * there is no transfer-encoding=chunked already set.
519+
// So, if transfer cannot be chunked and content length is 0, we dont set a request body.
520+
if !isWebsocket && !isTransferEncodingChunked(req) && req.ContentLength == 0 {
529521
req.Body = http.NoBody
530522
}
531523
stripWebsocketUpgradeHeader(req)
@@ -550,35 +542,6 @@ func isTransferEncodingChunked(req *http.Request) bool {
550542
return strings.Contains(strings.ToLower(transferEncodingVal), "chunked")
551543
}
552544

553-
// Borrowed from https://github.com/golang/go/blob/go1.22.6/src/net/http/request.go#L1541
554-
func requestMethodUsuallyLacksBody(req *http.Request) bool {
555-
switch strings.ToUpper(req.Method) {
556-
case "GET", "HEAD", "DELETE", "OPTIONS", "PROPFIND", "SEARCH":
557-
return true
558-
}
559-
return false
560-
}
561-
562-
func shouldSetRequestBodyToEmpty(connectRequest *pogs.ConnectRequest, metadata map[string]string, req *http.Request) bool {
563-
switch metadata[HTTPRequestBodyHintKey] {
564-
case RequestBodyHintEmpty.String():
565-
return true
566-
case RequestBodyHintHasData.String():
567-
return false
568-
default:
569-
}
570-
571-
isWebsocket := connectRequest.Type == pogs.ConnectionTypeWebsocket
572-
// Go's client defaults to chunked encoding after a 200ms delay if the following cases are true:
573-
// * the request body blocks
574-
// * the content length is not set (or set to -1)
575-
// * the method doesn't usually have a body (GET, HEAD, DELETE, ...)
576-
// * there is no transfer-encoding=chunked already set.
577-
// So, if transfer cannot be chunked and content length is 0, we dont set a request body.
578-
// Reference: https://github.com/golang/go/blob/go1.22.2/src/net/http/transfer.go#L192-L206
579-
return !isWebsocket && requestMethodUsuallyLacksBody(req) && !isTransferEncodingChunked(req) && req.ContentLength == 0
580-
}
581-
582545
// A helper struct that guarantees a call to close only affects read side, but not write side.
583546
type nopCloserReadWriter struct {
584547
io.ReadWriteCloser

connection/quic_test.go

Lines changed: 0 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -486,125 +486,6 @@ func TestBuildHTTPRequest(t *testing.T) {
486486
},
487487
body: io.NopCloser(&bytes.Buffer{}),
488488
},
489-
{
490-
name: "if edge sends the body is empty hint, set body to empty",
491-
connectRequest: &pogs.ConnectRequest{
492-
Dest: "http://test.com",
493-
Metadata: []pogs.Metadata{
494-
{
495-
Key: "HttpHeader:Another-Header",
496-
Val: "Misc",
497-
},
498-
{
499-
Key: "HttpHost",
500-
Val: "cf.host",
501-
},
502-
{
503-
Key: "HttpMethod",
504-
Val: "put",
505-
},
506-
{
507-
Key: HTTPRequestBodyHintKey,
508-
Val: RequestBodyHintEmpty.String(),
509-
},
510-
},
511-
},
512-
req: &http.Request{
513-
Method: "put",
514-
URL: &url.URL{
515-
Scheme: "http",
516-
Host: "test.com",
517-
},
518-
Proto: "HTTP/1.1",
519-
ProtoMajor: 1,
520-
ProtoMinor: 1,
521-
Header: http.Header{
522-
"Another-Header": []string{"Misc"},
523-
},
524-
ContentLength: 0,
525-
Host: "cf.host",
526-
Body: http.NoBody,
527-
},
528-
body: io.NopCloser(&bytes.Buffer{}),
529-
},
530-
{
531-
name: "if edge sends the body has data hint, don't set body to empty",
532-
connectRequest: &pogs.ConnectRequest{
533-
Dest: "http://test.com",
534-
Metadata: []pogs.Metadata{
535-
{
536-
Key: "HttpHeader:Another-Header",
537-
Val: "Misc",
538-
},
539-
{
540-
Key: "HttpHost",
541-
Val: "cf.host",
542-
},
543-
{
544-
Key: "HttpMethod",
545-
Val: "put",
546-
},
547-
{
548-
Key: HTTPRequestBodyHintKey,
549-
Val: RequestBodyHintHasData.String(),
550-
},
551-
},
552-
},
553-
req: &http.Request{
554-
Method: "put",
555-
URL: &url.URL{
556-
Scheme: "http",
557-
Host: "test.com",
558-
},
559-
Proto: "HTTP/1.1",
560-
ProtoMajor: 1,
561-
ProtoMinor: 1,
562-
Header: http.Header{
563-
"Another-Header": []string{"Misc"},
564-
},
565-
ContentLength: 0,
566-
Host: "cf.host",
567-
Body: io.NopCloser(&bytes.Buffer{}),
568-
},
569-
body: io.NopCloser(&bytes.Buffer{}),
570-
},
571-
{
572-
name: "if the http method usually has body, don't set body to empty",
573-
connectRequest: &pogs.ConnectRequest{
574-
Dest: "http://test.com",
575-
Metadata: []pogs.Metadata{
576-
{
577-
Key: "HttpHeader:Another-Header",
578-
Val: "Misc",
579-
},
580-
{
581-
Key: "HttpHost",
582-
Val: "cf.host",
583-
},
584-
{
585-
Key: "HttpMethod",
586-
Val: "post",
587-
},
588-
},
589-
},
590-
req: &http.Request{
591-
Method: "post",
592-
URL: &url.URL{
593-
Scheme: "http",
594-
Host: "test.com",
595-
},
596-
Proto: "HTTP/1.1",
597-
ProtoMajor: 1,
598-
ProtoMinor: 1,
599-
Header: http.Header{
600-
"Another-Header": []string{"Misc"},
601-
},
602-
ContentLength: 0,
603-
Host: "cf.host",
604-
Body: io.NopCloser(&bytes.Buffer{}),
605-
},
606-
body: io.NopCloser(&bytes.Buffer{}),
607-
},
608489
}
609490

610491
log := zerolog.Nop()

0 commit comments

Comments
 (0)