Commit 34eeaea5 authored by Honfika's avatar Honfika

more cancellationtokens

parent 36be993a
...@@ -211,7 +211,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -211,7 +211,7 @@ namespace Titanium.Web.Proxy.EventArguments
{ {
while (contentLength > copyStream.ReadBytes) while (contentLength > copyStream.ReadBytes)
{ {
long read = await ReadUntilBoundaryAsync(copyStreamReader, contentLength, boundary); long read = await ReadUntilBoundaryAsync(copyStreamReader, contentLength, boundary, cancellationToken);
if (read == 0) if (read == 0)
{ {
break; break;
...@@ -287,7 +287,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -287,7 +287,7 @@ namespace Titanium.Web.Proxy.EventArguments
/// Read a line from the byte stream /// Read a line from the byte stream
/// </summary> /// </summary>
/// <returns></returns> /// <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; int bufferDataLength = 0;
...@@ -297,7 +297,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -297,7 +297,7 @@ namespace Titanium.Web.Proxy.EventArguments
int boundaryLength = boundary.Length + 4; int boundaryLength = boundary.Length + 4;
long bytesRead = 0; long bytesRead = 0;
while (bytesRead < totalBytesToRead && (reader.DataAvailable || await reader.FillBufferAsync())) while (bytesRead < totalBytesToRead && (reader.DataAvailable || await reader.FillBufferAsync(cancellationToken)))
{ {
byte newChar = reader.ReadByteFromBuffer(); byte newChar = reader.ReadByteFromBuffer();
buffer[bufferDataLength] = newChar; buffer[bufferDataLength] = newChar;
......
...@@ -168,7 +168,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -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 //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> /// <summary>
...@@ -214,7 +214,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -214,7 +214,7 @@ namespace Titanium.Web.Proxy.Helpers
if (chunkSize != 0) if (chunkSize != 0)
{ {
await CopyBytesFromStream(reader, chunkSize, onCopy); await CopyBytesFromStream(reader, chunkSize, onCopy, cancellationToken);
} }
await WriteLineAsync(cancellationToken); await WriteLineAsync(cancellationToken);
...@@ -235,8 +235,9 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -235,8 +235,9 @@ namespace Titanium.Web.Proxy.Helpers
/// <param name="reader"></param> /// <param name="reader"></param>
/// <param name="count"></param> /// <param name="count"></param>
/// <param name="onCopy"></param> /// <param name="onCopy"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns> /// <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; var buffer = reader.Buffer;
long remainingBytes = count; long remainingBytes = count;
...@@ -249,7 +250,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -249,7 +250,7 @@ namespace Titanium.Web.Proxy.Helpers
bytesToRead = (int)remainingBytes; bytesToRead = (int)remainingBytes;
} }
int bytesRead = await reader.ReadBytesAsync(buffer, bytesToRead); int bytesRead = await reader.ReadBytesAsync(buffer, bytesToRead, cancellationToken);
if (bytesRead == 0) if (bytesRead == 0)
{ {
break; break;
...@@ -257,7 +258,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -257,7 +258,7 @@ namespace Titanium.Web.Proxy.Helpers
remainingBytes -= bytesRead; remainingBytes -= bytesRead;
await WriteAsync(buffer, 0, bytesRead); await WriteAsync(buffer, 0, bytesRead, cancellationToken);
onCopy?.Invoke(buffer, 0, bytesRead); onCopy?.Invoke(buffer, 0, bytesRead);
} }
......
using System; using System;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Titanium.Web.Proxy.Helpers; using Titanium.Web.Proxy.Helpers;
using Titanium.Web.Proxy.Http; using Titanium.Web.Proxy.Http;
...@@ -64,11 +65,11 @@ namespace Titanium.Web.Proxy.Models ...@@ -64,11 +65,11 @@ namespace Titanium.Web.Proxy.Models
return result; return result;
} }
internal async Task WriteToStreamAsync(HttpWriter writer) internal async Task WriteToStreamAsync(HttpWriter writer, CancellationToken cancellationToken)
{ {
await writer.WriteAsync(Name); await writer.WriteAsync(Name, cancellationToken);
await writer.WriteAsync(": "); await writer.WriteAsync(": ", cancellationToken);
await writer.WriteLineAsync(Value); await writer.WriteLineAsync(Value, cancellationToken);
} }
} }
} }
...@@ -80,7 +80,7 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -80,7 +80,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
if (!string.IsNullOrEmpty(externalProxy.UserName) && externalProxy.Password != null) 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 " + await writer.WriteLineAsync(KnownHeaders.ProxyAuthorization + ": Basic " +
Convert.ToBase64String(Encoding.UTF8.GetBytes( Convert.ToBase64String(Encoding.UTF8.GetBytes(
externalProxy.UserName + ":" + externalProxy.Password)), cancellationToken); externalProxy.UserName + ":" + externalProxy.Password)), cancellationToken);
......
...@@ -172,7 +172,7 @@ namespace Titanium.Web.Proxy ...@@ -172,7 +172,7 @@ namespace Titanium.Web.Proxy
") failed. Please check credentials.</h2></div>"; ") failed. Please check credentials.</h2></div>";
string originalErrorMessage = string originalErrorMessage =
"<div class=\"inserted-by-proxy\"><h3>Response from remote web server below.</h3></div><br/>"; "<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>"); int idx = body.IndexOfIgnoreCase("<body>");
if (idx >= 0) 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