Skip to content

Commit f641e07

Browse files
author
Mazov Sergey
committed
Fixes #12 Read response to the end before converting it to string
1 parent d83498b commit f641e07

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

RELEASE-NOTES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
v.5.3.0
22
- Fixed potential infinite loop in socket reading method
3+
- Fixed potential response string corruption for registries which use multi-byte encoding
34

45
v.5.2.0
56
- Moved TcpClient creation to separate class, enabling usage in special cases like flowing traffic through SOCKS proxy

WhoisClient.NET/WhoisClient.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ private static string RawQuery(string query, EndPoint server, IQueryOptions opti
298298
return string.Empty;
299299
}
300300

301-
var res = new StringBuilder();
301+
var responseBytes = new List<byte>(4096);
302302
try
303303
{
304304
using (var s = tcpClient.GetStream())
@@ -317,12 +317,12 @@ private static string RawQuery(string query, EndPoint server, IQueryOptions opti
317317
do
318318
{
319319
cbRead = s.Read(readBuff, 0, readBuff.Length);
320-
res.Append(options.Encoding.GetString(readBuff, 0, cbRead));
320+
responseBytes.AddRange(readBuff.Take(cbRead));
321321
if (cbRead > 0)
322322
Thread.Sleep(100);
323323
} while (cbRead > 0);
324324

325-
return res.ToString();
325+
return options.Encoding.GetString(responseBytes.ToArray());
326326
}
327327
}
328328
catch
@@ -333,7 +333,7 @@ private static string RawQuery(string query, EndPoint server, IQueryOptions opti
333333
if (options.RethrowExceptions)
334334
throw;
335335

336-
return res.ToString();
336+
return options.Encoding.GetString(responseBytes.ToArray());
337337
}
338338
finally
339339
{
@@ -406,7 +406,7 @@ private static async Task<string> RawQueryAsync(string query, EndPoint server, I
406406
return string.Empty;
407407
}
408408

409-
var res = new StringBuilder();
409+
var responseBytes = new List<byte>(4096);
410410
try
411411
{
412412
using (var s = tcpClient.GetStream())
@@ -425,12 +425,12 @@ private static async Task<string> RawQueryAsync(string query, EndPoint server, I
425425
do
426426
{
427427
cbRead = await s.ReadAsync(readBuff, 0, buffSize, token).ConfigureAwait(false);
428-
res.Append(options.Encoding.GetString(readBuff, 0, cbRead));
428+
responseBytes.AddRange(readBuff.Take(cbRead));
429429
if (cbRead > 0)
430430
await Task.Delay(100, token).ConfigureAwait(false);
431431
} while (cbRead > 0);
432432

433-
return res.ToString();
433+
return options.Encoding.GetString(responseBytes.ToArray());
434434
}
435435
}
436436
catch (Exception)
@@ -441,7 +441,7 @@ private static async Task<string> RawQueryAsync(string query, EndPoint server, I
441441
if (options.RethrowExceptions)
442442
throw;
443443

444-
return res.ToString();
444+
return options.Encoding.GetString(responseBytes.ToArray());
445445
}
446446
finally
447447
{

0 commit comments

Comments
 (0)