Skip to content

Commit d2a72fa

Browse files
committed
fixes EvilBeaver#1472 неверное чтение тела ответа при запросе HEAD
1 parent 75050e7 commit d2a72fa

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

src/OneScript.StandardLibrary/Http/HttpResponseBody.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This Source Code Form is subject to the terms of the
99
using System.IO;
1010
using System.IO.Compression;
1111
using System.Net;
12+
using System.Net.Http;
1213

1314
namespace OneScript.StandardLibrary.Http
1415
{
@@ -27,6 +28,12 @@ class HttpResponseBody : IDisposable
2728

2829
public HttpResponseBody(HttpWebResponse response, string dumpToFile)
2930
{
31+
if (response.Method == HttpMethod.Head.Method)
32+
{
33+
_inMemBody = Array.Empty<byte>();
34+
return;
35+
}
36+
3037
_autoDecompress = string.Equals(response.ContentEncoding, "gzip", StringComparison.OrdinalIgnoreCase);
3138
_contentSize = _autoDecompress ? -1 : response.ContentLength;
3239

@@ -140,24 +147,22 @@ private void ReadToStream(HttpWebResponse response)
140147
private void ReadToArray(HttpWebResponse response)
141148
{
142149
System.Diagnostics.Debug.Assert(_contentSize <= INMEMORY_BODY_LIMIT);
150+
151+
using var stream = GetResponseStream(response);
152+
var mustRead = (int)_contentSize;
153+
_inMemBody = new byte[mustRead];
154+
int offset = 0;
143155

144-
using (var stream = GetResponseStream(response))
156+
while (mustRead > 0)
145157
{
146-
var mustRead = (int)_contentSize;
147-
_inMemBody = new byte[mustRead];
148-
int offset = 0;
149-
150-
while (mustRead > 0)
151-
{
152-
int portion = Math.Min(CHUNK_SIZE, (int)mustRead);
153-
var read = stream.Read(_inMemBody, offset, portion);
158+
int portion = Math.Min(CHUNK_SIZE, (int)mustRead);
159+
var read = stream.Read(_inMemBody, offset, portion);
154160

155-
if (read == 0)
156-
break;
161+
if (read == 0)
162+
break;
157163

158-
mustRead -= read;
159-
offset += read;
160-
}
164+
mustRead -= read;
165+
offset += read;
161166
}
162167
}
163168

0 commit comments

Comments
 (0)