@@ -85,6 +85,11 @@ private async Task readRequestBodyAsync(CancellationToken cancellationToken)
8585 // If not already read (not cached yet)
8686 if ( ! request . IsBodyRead )
8787 {
88+ if ( request . IsBodyReceived )
89+ {
90+ throw new Exception ( "Request body was already received." ) ;
91+ }
92+
8893 if ( request . HttpVersion == HttpHeader . Version20 )
8994 {
9095 // do not send to the remote endpoint
@@ -103,6 +108,7 @@ private async Task readRequestBodyAsync(CancellationToken cancellationToken)
103108 // Now set the flag to true
104109 // So that next time we can deliver body from cache
105110 request . IsBodyRead = true ;
111+ request . IsBodyReceived = true ;
106112 }
107113 else
108114 {
@@ -115,6 +121,7 @@ private async Task readRequestBodyAsync(CancellationToken cancellationToken)
115121 // Now set the flag to true
116122 // So that next time we can deliver body from cache
117123 request . IsBodyRead = true ;
124+ request . IsBodyReceived = true ;
118125 }
119126 }
120127 }
@@ -160,6 +167,11 @@ private async Task readResponseBodyAsync(CancellationToken cancellationToken)
160167 // If not already read (not cached yet)
161168 if ( ! response . IsBodyRead )
162169 {
170+ if ( response . IsBodyReceived )
171+ {
172+ throw new Exception ( "Response body was already received." ) ;
173+ }
174+
163175 if ( response . HttpVersion == HttpHeader . Version20 )
164176 {
165177 // do not send to the remote endpoint
@@ -178,6 +190,7 @@ private async Task readResponseBodyAsync(CancellationToken cancellationToken)
178190 // Now set the flag to true
179191 // So that next time we can deliver body from cache
180192 response . IsBodyRead = true ;
193+ response . IsBodyReceived = true ;
181194 }
182195 else
183196 {
@@ -190,6 +203,7 @@ private async Task readResponseBodyAsync(CancellationToken cancellationToken)
190203 // Now set the flag to true
191204 // So that next time we can deliver body from cache
192205 response . IsBodyRead = true ;
206+ response . IsBodyReceived = true ;
193207 }
194208 }
195209 }
@@ -221,14 +235,15 @@ private async Task<byte[]> readBodyAsync(bool isRequest, CancellationToken cance
221235 internal async Task SyphonOutBodyAsync ( bool isRequest , CancellationToken cancellationToken )
222236 {
223237 var requestResponse = isRequest ? ( RequestResponseBase ) HttpClient . Request : HttpClient . Response ;
224- if ( requestResponse . IsBodyRead || ! requestResponse . OriginalHasBody )
238+ if ( requestResponse . IsBodyReceived || ! requestResponse . OriginalHasBody )
225239 {
226240 return ;
227241 }
228242
229243 var reader = isRequest ? ( HttpStream ) ClientStream : HttpClient . Connection . Stream ;
230244
231245 await reader . CopyBodyAsync ( requestResponse , true , NullWriter . Instance , TransformationMode . None , isRequest , this , cancellationToken ) ;
246+ requestResponse . IsBodyReceived = true ;
232247 }
233248
234249 /// <summary>
@@ -272,11 +287,15 @@ internal async Task CopyRequestBodyAsync(IHttpStreamWriter writer, Transformatio
272287 {
273288 await reader . CopyBodyAsync ( request , false , writer , transformation , true , this , cancellationToken ) ;
274289 }
290+
291+ request . IsBodyReceived = true ;
275292 }
276293
277294 private async Task copyResponseBodyAsync ( IHttpStreamWriter writer , TransformationMode transformation , CancellationToken cancellationToken )
278295 {
279- await HttpClient . Connection . Stream . CopyBodyAsync ( HttpClient . Response , false , writer , transformation , false , this , cancellationToken ) ;
296+ var response = HttpClient . Response ;
297+ await HttpClient . Connection . Stream . CopyBodyAsync ( response , false , writer , transformation , false , this , cancellationToken ) ;
298+ response . IsBodyReceived = true ;
280299 }
281300
282301 /// <summary>
0 commit comments