Skip to content

Commit 1604319

Browse files
committed
Revert "Merge pull request #120 from patchkit-net/feature/v3.x.x/1226-dynamic-url-testing"
This reverts commit 843fb48, reversing changes made to 2b4269d.
1 parent 37391dd commit 1604319

File tree

12 files changed

+279
-629
lines changed

12 files changed

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

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

2019
private readonly string _url;
2120
private readonly int _timeout;
2221
private readonly IHttpClient _httpClient;
2322

24-
private readonly ulong _bufferSize;
2523
private readonly byte[] _buffer;
2624

2725
private bool _downloadHasBeenCalled;
2826
private BytesRange? _bytesRange;
2927

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

3535
public BaseHttpDownloader([NotNull] string url, int timeout, [NotNull] IHttpClient httpClient,
36-
[NotNull] ILogger logger, ulong bufferSize)
36+
[NotNull] ILogger logger)
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,8 +45,7 @@ public BaseHttpDownloader([NotNull] string url, int timeout, [NotNull] IHttpClie
4545
_httpClient = httpClient;
4646
_logger = logger;
4747

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

5150
ServicePointManager.ServerCertificateValidationCallback =
5251
(sender, certificate, chain, errors) => true;
@@ -63,26 +62,13 @@ public void SetBytesRange(BytesRange? range)
6362
}
6463
}
6564

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)
65+
public void Download(CancellationToken cancellationToken)
8066
{
8167
try
8268
{
8369
_logger.LogDebug("Downloading...");
8470
_logger.LogTrace("url = " + _url);
85-
_logger.LogTrace("bufferSize = " + _bufferSize);
71+
_logger.LogTrace("bufferSize = " + BufferSize);
8672
_logger.LogTrace("bytesRange = " + (_bytesRange.HasValue
8773
? _bytesRange.Value.Start + "-" + _bytesRange.Value.End
8874
: "(none)"));
@@ -98,7 +84,37 @@ public IEnumerable<DataPacket> ReadPackets(CancellationToken cancellationToken)
9884
ReadWriteTimeout = _timeout,
9985
};
10086

101-
return ReadResponseStream(request, cancellationToken);
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.");
102118
}
103119
catch (WebException webException)
104120
{
@@ -113,45 +129,14 @@ public IEnumerable<DataPacket> ReadPackets(CancellationToken cancellationToken)
113129
}
114130
}
115131

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

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-
}
139+
OnDataAvailable(_buffer, bufferRead);
155140
}
156141
}
157142

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

0 commit comments

Comments
 (0)