Commit 34eeaea5 authored by Honfika's avatar Honfika

more cancellationtokens

parent 36be993a
......@@ -211,7 +211,7 @@ namespace Titanium.Web.Proxy.EventArguments
{
while (contentLength > copyStream.ReadBytes)
{
long read = await ReadUntilBoundaryAsync(copyStreamReader, contentLength, boundary);
long read = await ReadUntilBoundaryAsync(copyStreamReader, contentLength, boundary, cancellationToken);
if (read == 0)
{
break;
......@@ -287,7 +287,7 @@ namespace Titanium.Web.Proxy.EventArguments
/// Read a line from the byte stream
/// </summary>
/// <returns></returns>
private async Task<long> ReadUntilBoundaryAsync(CustomBinaryReader reader, long totalBytesToRead, string boundary)
private async Task<long> ReadUntilBoundaryAsync(CustomBinaryReader reader, long totalBytesToRead, string boundary, CancellationToken cancellationToken)
{
int bufferDataLength = 0;
......@@ -297,7 +297,7 @@ namespace Titanium.Web.Proxy.EventArguments
int boundaryLength = boundary.Length + 4;
long bytesRead = 0;
while (bytesRead < totalBytesToRead && (reader.DataAvailable || await reader.FillBufferAsync()))
while (bytesRead < totalBytesToRead && (reader.DataAvailable || await reader.FillBufferAsync(cancellationToken)))
{
byte newChar = reader.ReadByteFromBuffer();
buffer[bufferDataLength] = newChar;
......
......@@ -168,7 +168,7 @@ namespace Titanium.Web.Proxy.Helpers
}
//If not chunked then its easy just read the amount of bytes mentioned in content length header
return CopyBytesFromStream(streamReader, contentLength, onCopy);
return CopyBytesFromStream(streamReader, contentLength, onCopy, cancellationToken);
}
/// <summary>
......@@ -214,7 +214,7 @@ namespace Titanium.Web.Proxy.Helpers
if (chunkSize != 0)
{
await CopyBytesFromStream(reader, chunkSize, onCopy);
await CopyBytesFromStream(reader, chunkSize, onCopy, cancellationToken);
}
await WriteLineAsync(cancellationToken);
......@@ -235,8 +235,9 @@ namespace Titanium.Web.Proxy.Helpers
/// <param name="reader"></param>
/// <param name="count"></param>
/// <param name="onCopy"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private async Task CopyBytesFromStream(CustomBinaryReader reader, long count, Action<byte[], int, int> onCopy)
private async Task CopyBytesFromStream(CustomBinaryReader reader, long count, Action<byte[], int, int> onCopy, CancellationToken cancellationToken)
{
var buffer = reader.Buffer;
long remainingBytes = count;
......@@ -249,7 +250,7 @@ namespace Titanium.Web.Proxy.Helpers
bytesToRead = (int)remainingBytes;
}
int bytesRead = await reader.ReadBytesAsync(buffer, bytesToRead);
int bytesRead = await reader.ReadBytesAsync(buffer, bytesToRead, cancellationToken);
if (bytesRead == 0)
{
break;
......@@ -257,7 +258,7 @@ namespace Titanium.Web.Proxy.Helpers
remainingBytes -= bytesRead;
await WriteAsync(buffer, 0, bytesRead);
await WriteAsync(buffer, 0, bytesRead, cancellationToken);
onCopy?.Invoke(buffer, 0, bytesRead);
}
......
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Titanium.Web.Proxy.Helpers;
using Titanium.Web.Proxy.Http;
......@@ -64,11 +65,11 @@ namespace Titanium.Web.Proxy.Models
return result;
}
internal async Task WriteToStreamAsync(HttpWriter writer)
internal async Task WriteToStreamAsync(HttpWriter writer, CancellationToken cancellationToken)
{
await writer.WriteAsync(Name);
await writer.WriteAsync(": ");
await writer.WriteLineAsync(Value);
await writer.WriteAsync(Name, cancellationToken);
await writer.WriteAsync(": ", cancellationToken);
await writer.WriteLineAsync(Value, cancellationToken);
}
}
}
......@@ -80,7 +80,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
if (!string.IsNullOrEmpty(externalProxy.UserName) && externalProxy.Password != null)
{
await HttpHeader.ProxyConnectionKeepAlive.WriteToStreamAsync(writer);
await HttpHeader.ProxyConnectionKeepAlive.WriteToStreamAsync(writer, cancellationToken);
await writer.WriteLineAsync(KnownHeaders.ProxyAuthorization + ": Basic " +
Convert.ToBase64String(Encoding.UTF8.GetBytes(
externalProxy.UserName + ":" + externalProxy.Password)), cancellationToken);
......
......@@ -172,7 +172,7 @@ namespace Titanium.Web.Proxy
") failed. Please check credentials.</h2></div>";
string originalErrorMessage =
"<div class=\"inserted-by-proxy\"><h3>Response from remote web server below.</h3></div><br/>";
string body = await args.GetResponseBodyAsString();
string body = await args.GetResponseBodyAsString(args.CancellationTokenSource.Token);
int idx = body.IndexOfIgnoreCase("<body>");
if (idx >= 0)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment