writeRequest sends a request. It returns nil after the request is written, the response read, and the request stream is half-closed by the peer. It returns non-nil if the request ends otherwise. If the returned error is StreamError, the error Code may be used in resetting the stream.
(req *http.Request, streamf func(*clientStream))
| 1321 | // It returns non-nil if the request ends otherwise. |
| 1322 | // If the returned error is StreamError, the error Code may be used in resetting the stream. |
| 1323 | func (cs *clientStream) writeRequest(req *http.Request, streamf func(*clientStream)) (err error) { |
| 1324 | cc := cs.cc |
| 1325 | ctx := cs.ctx |
| 1326 | |
| 1327 | if err := checkConnHeaders(req); err != nil { |
| 1328 | return err |
| 1329 | } |
| 1330 | |
| 1331 | // Acquire the new-request lock by writing to reqHeaderMu. |
| 1332 | // This lock guards the critical section covering allocating a new stream ID |
| 1333 | // (requires mu) and creating the stream (requires wmu). |
| 1334 | if cc.reqHeaderMu == nil { |
| 1335 | panic("RoundTrip on uninitialized ClientConn") // for tests |
| 1336 | } |
| 1337 | select { |
| 1338 | case cc.reqHeaderMu <- struct{}{}: |
| 1339 | case <-cs.reqCancel: |
| 1340 | return common.ErrRequestCanceled |
| 1341 | case <-ctx.Done(): |
| 1342 | return ctx.Err() |
| 1343 | } |
| 1344 | |
| 1345 | cc.mu.Lock() |
| 1346 | if cc.idleTimer != nil { |
| 1347 | cc.idleTimer.Stop() |
| 1348 | } |
| 1349 | cc.decrStreamReservationsLocked() |
| 1350 | if err := cc.awaitOpenSlotForStreamLocked(cs); err != nil { |
| 1351 | cc.mu.Unlock() |
| 1352 | <-cc.reqHeaderMu |
| 1353 | return err |
| 1354 | } |
| 1355 | cc.addStreamLocked(cs) // assigns stream ID |
| 1356 | if isConnectionCloseRequest(req) { |
| 1357 | cc.doNotReuse = true |
| 1358 | } |
| 1359 | cc.mu.Unlock() |
| 1360 | |
| 1361 | if streamf != nil { |
| 1362 | streamf(cs) |
| 1363 | } |
| 1364 | |
| 1365 | continueTimeout := cc.t.ExpectContinueTimeout |
| 1366 | if continueTimeout != 0 { |
| 1367 | if !httpguts.HeaderValuesContainsToken(req.Header["Expect"], "100-continue") { |
| 1368 | continueTimeout = 0 |
| 1369 | } else { |
| 1370 | cs.on100 = make(chan struct{}, 1) |
| 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | var dumps []*dump.Dumper |
| 1375 | if t := cs.cc.t; t != nil { |
| 1376 | dumps = dump.GetDumpers(req.Context(), t.Dump) |
| 1377 | } |
| 1378 | |
| 1379 | // Past this point (where we send request headers), it is possible for |
| 1380 | // RoundTrip to return successfully. Since the RoundTrip contract permits |
no test coverage detected