MCPcopy Create free account
hub / github.com/imroc/req / shouldSendChunkedRequestBody

Method shouldSendChunkedRequestBody

transfer.go:141–162  ·  view source on GitHub ↗

shouldSendChunkedRequestBody reports whether we should try to send a chunked request body to the server. In particular, the case we really want to prevent is sending a GET or other typically-bodyless request to a server with a chunked body when the body has zero bytes, since GETs with bodies (while

()

Source from the content-addressed store, hash-verified

139// common case, we act as if their Body were nil instead, and don't send
140// a body.
141func (t *transferWriter) shouldSendChunkedRequestBody() bool {
142 // Note that t.ContentLength is the corrected content length
143 // from rr.outgoingLength, so 0 actually means zero, not unknown.
144 if t.ContentLength >= 0 || t.Body == nil { // redundant checks; caller did them
145 return false
146 }
147 if t.Method == "CONNECT" {
148 return false
149 }
150 if requestMethodUsuallyLacksBody(t.Method) {
151 // Only probe the Request.Body for GET/HEAD/DELETE/etc
152 // requests, because it's only those types of requests
153 // that confuse servers.
154 t.probeRequestBody() // adjusts t.Body, t.ContentLength
155 return t.Body != nil
156 }
157 // For all other request types (PUT, POST, PATCH, or anything
158 // made-up we've never heard of), assume it's normal and the server
159 // can deal with a chunked request body. Maybe we'll adjust this
160 // later.
161 return true
162}
163
164// probeRequestBody reads a byte from t.Body to see whether it's empty
165// (returns io.EOF right away).

Callers 1

newTransferWriterFunction · 0.80

Calls 2

probeRequestBodyMethod · 0.95

Tested by

no test coverage detected