@@ -1462,23 +1462,7 @@ function _resume (client, sync) {
14621462 return
14631463 }
14641464
1465- if ( util . isStream ( request . body ) && util . bodyLength ( request . body ) === 0 ) {
1466- request . body
1467- . on ( 'data' , /* istanbul ignore next */ function ( ) {
1468- /* istanbul ignore next */
1469- assert ( false )
1470- } )
1471- . on ( 'error' , function ( err ) {
1472- errorRequest ( client , request , err )
1473- } )
1474- . on ( 'end' , function ( ) {
1475- util . destroy ( this )
1476- } )
1477-
1478- request . body = null
1479- }
1480-
1481- if ( client [ kRunning ] > 0 &&
1465+ if ( client [ kRunning ] > 0 && util . bodyLength ( request . body ) !== 0 &&
14821466 ( util . isStream ( request . body ) || util . isAsyncIterable ( request . body ) ) ) {
14831467 // Request with stream or iterator body can error while other requests
14841468 // are inflight and indirectly error those as well.
@@ -1499,6 +1483,11 @@ function _resume (client, sync) {
14991483 }
15001484}
15011485
1486+ // https://www.rfc-editor.org/rfc/rfc7230#section-3.3.2
1487+ function shouldSendContentLength ( method ) {
1488+ return method !== 'GET' && method !== 'HEAD' && method !== 'OPTIONS' && method !== 'TRACE' && method !== 'CONNECT'
1489+ }
1490+
15021491function write ( client , request ) {
15031492 if ( client [ kHTTPConnVersion ] === 'h2' ) {
15041493 writeH2 ( client , client [ kHTTP2Session ] , request )
@@ -1527,7 +1516,9 @@ function write (client, request) {
15271516 body . read ( 0 )
15281517 }
15291518
1530- let contentLength = util . bodyLength ( body )
1519+ const bodyLength = util . bodyLength ( body )
1520+
1521+ let contentLength = bodyLength
15311522
15321523 if ( contentLength === null ) {
15331524 contentLength = request . contentLength
@@ -1542,7 +1533,9 @@ function write (client, request) {
15421533 contentLength = null
15431534 }
15441535
1545- if ( request . contentLength !== null && request . contentLength !== contentLength ) {
1536+ // https://github.com/nodejs/undici/issues/2046
1537+ // A user agent may send a Content-Length header with 0 value, this should be allowed.
1538+ if ( shouldSendContentLength ( method ) && contentLength > 0 && request . contentLength !== null && request . contentLength !== contentLength ) {
15461539 if ( client [ kStrictContentLength ] ) {
15471540 errorRequest ( client , request , new RequestContentLengthMismatchError ( ) )
15481541 return false
@@ -1623,7 +1616,7 @@ function write (client, request) {
16231616 }
16241617
16251618 /* istanbul ignore else: assertion */
1626- if ( ! body ) {
1619+ if ( ! body || bodyLength === 0 ) {
16271620 if ( contentLength === 0 ) {
16281621 socket . write ( `${ header } content-length: 0\r\n\r\n` , 'latin1' )
16291622 } else {
@@ -1763,7 +1756,9 @@ function writeH2 (client, session, request) {
17631756 contentLength = null
17641757 }
17651758
1766- if ( request . contentLength != null && request . contentLength !== contentLength ) {
1759+ // https://github.com/nodejs/undici/issues/2046
1760+ // A user agent may send a Content-Length header with 0 value, this should be allowed.
1761+ if ( shouldSendContentLength ( method ) && contentLength > 0 && request . contentLength != null && request . contentLength !== contentLength ) {
17671762 if ( client [ kStrictContentLength ] ) {
17681763 errorRequest ( client , request , new RequestContentLengthMismatchError ( ) )
17691764 return false
0 commit comments