@@ -526,7 +526,18 @@ void HttpResponseImpl::makeHeaderString(trantor::MsgBuffer &buffer)
526526 if (!passThrough_)
527527 {
528528 buffer.ensureWritableBytes (64 );
529- if (streamCallback_ || asyncStreamCallback_)
529+ if (!contentLengthIsAllowed ())
530+ {
531+ len = 0 ;
532+ if ((bodyPtr_ && bodyPtr_->length () > 0 ) ||
533+ !sendfileName_.empty () || streamCallback_ ||
534+ asyncStreamCallback_)
535+ {
536+ LOG_ERROR << " The body should be empty when the content-length "
537+ " is not allowed!" ;
538+ }
539+ }
540+ else if (streamCallback_ || asyncStreamCallback_)
530541 {
531542 // When the headers are created, it is time to set the transfer
532543 // encoding to chunked if the contents size is not specified
@@ -538,15 +549,15 @@ void HttpResponseImpl::makeHeaderString(trantor::MsgBuffer &buffer)
538549 }
539550 len = 0 ;
540551 }
541- else if (sendfileName_.empty () && contentLengthIsAllowed () )
552+ else if (sendfileName_.empty ())
542553 {
543554 auto bodyLength = bodyPtr_ ? bodyPtr_->length () : 0 ;
544555 len = snprintf (buffer.beginWrite (),
545556 buffer.writableBytes (),
546557 contentLengthFormatString<decltype (bodyLength)>(),
547558 bodyLength);
548559 }
549- else if ( contentLengthIsAllowed ())
560+ else
550561 {
551562 auto bodyLength = sendfileRange_.second ;
552563 len = snprintf (buffer.beginWrite (),
@@ -629,7 +640,7 @@ void HttpResponseImpl::renderToBuffer(trantor::MsgBuffer &buffer)
629640 {
630641 buffer.append (" \r\n " );
631642 }
632- if (bodyPtr_)
643+ if (bodyPtr_ && contentLengthIsAllowed () )
633644 buffer.append (bodyPtr_->data (), bodyPtr_->length ());
634645}
635646
@@ -958,7 +969,8 @@ bool HttpResponseImpl::shouldBeCompressed() const
958969{
959970 if (streamCallback_ || asyncStreamCallback_ || !sendfileName_.empty () ||
960971 contentType () >= CT_APPLICATION_OCTET_STREAM ||
961- getBody ().length () < 1024 || !(getHeaderBy (" content-encoding" ).empty ()))
972+ getBody ().length () < 1024 ||
973+ !(getHeaderBy (" content-encoding" ).empty ()) || !contentLengthIsAllowed ())
962974 {
963975 return false ;
964976 }
0 commit comments