Skip to content

Commit 843fb48

Browse files
authored
Merge pull request #120 from patchkit-net/feature/v3.x.x/1226-dynamic-url-testing
Feature/v3.x.x/1226 dynamic url testing
2 parents 2b4269d + d58ef1a commit 843fb48

File tree

12 files changed

+629
-279
lines changed

12 files changed

+629
-279
lines changed
Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.IO;
33
using System.Net;
4+
using System.Collections.Generic;
45
using JetBrains.Annotations;
56
using PatchKit.Logging;
67
using PatchKit.Network;
@@ -14,26 +15,25 @@ public sealed class BaseHttpDownloader : IBaseHttpDownloader
1415
{
1516
private readonly ILogger _logger;
1617

17-
private static readonly int BufferSize = 5 * (int) Units.MB;
18+
private static readonly ulong DefaultBufferSize = 5 * (ulong) Units.MB;
1819

1920
private readonly string _url;
2021
private readonly int _timeout;
2122
private readonly IHttpClient _httpClient;
2223

24+
private readonly ulong _bufferSize;
2325
private readonly byte[] _buffer;
2426

2527
private bool _downloadHasBeenCalled;
2628
private BytesRange? _bytesRange;
2729

28-
public event DataAvailableHandler DataAvailable;
29-
3030
public BaseHttpDownloader(string url, int timeout) :
31-
this(url, timeout, new DefaultHttpClient(), PatcherLogManager.DefaultLogger)
31+
this(url, timeout, new DefaultHttpClient(), PatcherLogManager.DefaultLogger, DefaultBufferSize)
3232
{
3333
}
3434

3535
public BaseHttpDownloader([NotNull] string url, int timeout, [NotNull] IHttpClient httpClient,
36-
[NotNull] ILogger logger)
36+
[NotNull] ILogger logger, ulong bufferSize)
3737
{
3838
if (string.IsNullOrEmpty(url)) throw new ArgumentException("Value cannot be null or empty.", "url");
3939
if (timeout <= 0) throw new ArgumentOutOfRangeException("timeout");
@@ -45,7 +45,8 @@ public BaseHttpDownloader([NotNull] string url, int timeout, [NotNull] IHttpClie
4545
_httpClient = httpClient;
4646
_logger = logger;
4747

48-
_buffer = new byte[BufferSize];
48+
_bufferSize = bufferSize;
49+
_buffer = new byte[_bufferSize];
4950

5051
ServicePointManager.ServerCertificateValidationCallback =
5152
(sender, certificate, chain, errors) => true;
@@ -62,13 +63,26 @@ public void SetBytesRange(BytesRange? range)
6263
}
6364
}
6465

65-
public void Download(CancellationToken cancellationToken)
66+
public void Download(CancellationToken cancellationToken, [NotNull] DataAvailableHandler onDataAvailable)
67+
{
68+
if (onDataAvailable == null)
69+
{
70+
throw new ArgumentNullException("onDataAvailable");
71+
}
72+
73+
foreach (DataPacket packet in ReadPackets(cancellationToken))
74+
{
75+
onDataAvailable(packet.Data, packet.Length);
76+
}
77+
}
78+
79+
public IEnumerable<DataPacket> ReadPackets(CancellationToken cancellationToken)
6680
{
6781
try
6882
{
6983
_logger.LogDebug("Downloading...");
7084
_logger.LogTrace("url = " + _url);
71-
_logger.LogTrace("bufferSize = " + BufferSize);
85+
_logger.LogTrace("bufferSize = " + _bufferSize);
7286
_logger.LogTrace("bytesRange = " + (_bytesRange.HasValue
7387
? _bytesRange.Value.Start + "-" + _bytesRange.Value.End
7488
: "(none)"));
@@ -84,37 +98,7 @@ public void Download(CancellationToken cancellationToken)
8498
ReadWriteTimeout = _timeout,
8599
};
86100

87-
using (var response = _httpClient.Get(request))
88-
{
89-
cancellationToken.ThrowIfCancellationRequested();
90-
91-
_logger.LogDebug("Received response from server.");
92-
_logger.LogTrace("statusCode = " + response.StatusCode);
93-
94-
if (Is2XXStatus(response.StatusCode))
95-
{
96-
_logger.LogDebug("Successful response. Reading response stream...");
97-
98-
//TODO: Could response.ContentStream be null? Need to check it.
99-
100-
ReadResponseStream(response.ContentStream, cancellationToken);
101-
102-
_logger.LogDebug("Stream has been read.");
103-
}
104-
else if (Is4XXStatus(response.StatusCode))
105-
{
106-
throw new DataNotAvailableException(string.Format(
107-
"Request data for {0} is not available (status: {1})", _url, response.StatusCode));
108-
}
109-
else
110-
{
111-
throw new ServerErrorException(string.Format(
112-
"Server has experienced some issues with request for {0} which resulted in {1} status code.",
113-
_url, response.StatusCode));
114-
}
115-
}
116-
117-
_logger.LogDebug("Downloading finished.");
101+
return ReadResponseStream(request, cancellationToken);
118102
}
119103
catch (WebException webException)
120104
{
@@ -129,14 +113,45 @@ public void Download(CancellationToken cancellationToken)
129113
}
130114
}
131115

132-
private void ReadResponseStream(Stream responseStream, CancellationToken cancellationToken)
116+
private IEnumerable<DataPacket> ReadResponseStream(HttpGetRequest request, CancellationToken cancellationToken)
133117
{
134-
int bufferRead;
135-
while ((bufferRead = responseStream.Read(_buffer, 0, BufferSize)) > 0)
118+
using (var response = _httpClient.Get(request))
136119
{
137120
cancellationToken.ThrowIfCancellationRequested();
138121

139-
OnDataAvailable(_buffer, bufferRead);
122+
_logger.LogDebug("Received response from server.");
123+
_logger.LogTrace("statusCode = " + response.StatusCode);
124+
125+
if (Is2XXStatus(response.StatusCode))
126+
{
127+
_logger.LogDebug("Successful response. Reading response stream...");
128+
129+
//TODO: Could response.ContentStream be null? Need to check it.
130+
131+
var responseStream = response.ContentStream;
132+
int bufferRead;
133+
while ((bufferRead = responseStream.Read(_buffer, 0, (int) _bufferSize)) > 0)
134+
{
135+
cancellationToken.ThrowIfCancellationRequested();
136+
137+
var dataPacket = new DataPacket { Data = _buffer, Length = bufferRead, };
138+
139+
yield return dataPacket;
140+
}
141+
142+
_logger.LogDebug("Downloading finished.");
143+
}
144+
else if (Is4XXStatus(response.StatusCode))
145+
{
146+
throw new DataNotAvailableException(string.Format(
147+
"Request data for {0} is not available (status: {1})", _url, response.StatusCode));
148+
}
149+
else
150+
{
151+
throw new ServerErrorException(string.Format(
152+
"Server has experienced some issues with request for {0} which resulted in {1} status code.",
153+
_url, response.StatusCode));
154+
}
140155
}
141156
}
142157

@@ -151,11 +166,5 @@ private static bool Is4XXStatus(HttpStatusCode statusCode)
151166
{
152167
return (int) statusCode >= 400 && (int) statusCode <= 499;
153168
}
154-
155-
private void OnDataAvailable(byte[] data, int length)
156-
{
157-
var handler = DataAvailable;
158-
if (handler != null) handler(data, length);
159-
}
160169
}
161170
}

0 commit comments

Comments
 (0)