Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit e82ad51

Browse files
Merge pull request #219 from justcoding121/develop
Merge with develop
2 parents e928c25 + 8e02c62 commit e82ad51

19 files changed

+642
-132
lines changed

Titanium.Web.Proxy.sln.DotSettings

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateConstants/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
3+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
4+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
5+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=dda2ffa1_002D435c_002D4111_002D88eb_002D1a7c93c382f0/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Static, Instance" AccessRightKinds="Private" Description="Property (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="PROPERTY" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;&lt;/Policy&gt;</s:String></wpf:ResourceDictionary>

Titanium.Web.Proxy/Extensions/HttpWebRequestExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Text;
1+
using System;
2+
using System.Text;
23
using Titanium.Web.Proxy.Http;
34
using Titanium.Web.Proxy.Shared;
45

@@ -29,7 +30,7 @@ internal static Encoding GetEncoding(this Request request)
2930
foreach (var contentType in contentTypes)
3031
{
3132
var encodingSplit = contentType.Split('=');
32-
if (encodingSplit.Length == 2 && encodingSplit[0].ToLower().Trim() == "charset")
33+
if (encodingSplit.Length == 2 && encodingSplit[0].Trim().Equals("charset", StringComparison.CurrentCultureIgnoreCase))
3334
{
3435
return Encoding.GetEncoding(encodingSplit[1]);
3536
}

Titanium.Web.Proxy/Extensions/HttpWebResponseExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Text;
1+
using System;
2+
using System.Text;
23
using Titanium.Web.Proxy.Http;
34
using Titanium.Web.Proxy.Shared;
45

@@ -26,7 +27,7 @@ internal static Encoding GetResponseCharacterEncoding(this Response response)
2627
foreach (var contentType in contentTypes)
2728
{
2829
var encodingSplit = contentType.Split('=');
29-
if (encodingSplit.Length == 2 && encodingSplit[0].ToLower().Trim() == "charset")
30+
if (encodingSplit.Length == 2 && encodingSplit[0].Trim().Equals("charset", StringComparison.CurrentCultureIgnoreCase))
3031
{
3132
return Encoding.GetEncoding(encodingSplit[1]);
3233
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Titanium.Web.Proxy.Extensions
9+
{
10+
internal static class StringExtensions
11+
{
12+
internal static bool ContainsIgnoreCase(this string str, string value)
13+
{
14+
return CultureInfo.CurrentCulture.CompareInfo.IndexOf(str, value, CompareOptions.IgnoreCase) >= 0;
15+
}
16+
}
17+
}

Titanium.Web.Proxy/Helpers/CustomBinaryReader.cs

Lines changed: 94 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.IO;
44
using System.Text;
55
using System.Threading.Tasks;
6-
using Titanium.Web.Proxy.Extensions;
76

87
namespace Titanium.Web.Proxy.Helpers
98
{
@@ -15,12 +14,30 @@ namespace Titanium.Web.Proxy.Helpers
1514
/// </summary>
1615
internal class CustomBinaryReader : IDisposable
1716
{
18-
private readonly Stream stream;
17+
private readonly CustomBufferedStream stream;
18+
private readonly int bufferSize;
1919
private readonly Encoding encoding;
2020

21-
internal CustomBinaryReader(Stream stream)
21+
[ThreadStatic]
22+
private static byte[] staticBuffer;
23+
24+
private byte[] buffer
25+
{
26+
get
27+
{
28+
if (staticBuffer == null || staticBuffer.Length != bufferSize)
29+
{
30+
staticBuffer = new byte[bufferSize];
31+
}
32+
33+
return staticBuffer;
34+
}
35+
}
36+
37+
internal CustomBinaryReader(CustomBufferedStream stream, int bufferSize)
2238
{
2339
this.stream = stream;
40+
this.bufferSize = bufferSize;
2441

2542
//default to UTF-8
2643
encoding = Encoding.UTF8;
@@ -34,33 +51,41 @@ internal CustomBinaryReader(Stream stream)
3451
/// <returns></returns>
3552
internal async Task<string> ReadLineAsync()
3653
{
37-
using (var readBuffer = new MemoryStream())
54+
var lastChar = default(byte);
55+
56+
int bufferDataLength = 0;
57+
58+
// try to use the thread static buffer, usually it is enough
59+
var buffer = this.buffer;
60+
61+
while (stream.DataAvailable || await stream.FillBufferAsync())
3862
{
39-
var lastChar = default(char);
40-
var buffer = new byte[1];
41-
42-
while ((await stream.ReadAsync(buffer, 0, 1)) > 0)
43-
{
44-
//if new line
45-
if (lastChar == '\r' && buffer[0] == '\n')
46-
{
47-
var result = readBuffer.ToArray();
48-
return encoding.GetString(result.SubArray(0, result.Length - 1));
49-
}
50-
//end of stream
51-
if (buffer[0] == '\0')
52-
{
53-
return encoding.GetString(readBuffer.ToArray());
54-
}
55-
56-
await readBuffer.WriteAsync(buffer,0,1);
57-
58-
//store last char for new line comparison
59-
lastChar = (char)buffer[0];
60-
}
61-
62-
return encoding.GetString(readBuffer.ToArray());
63+
var newChar = stream.ReadByteFromBuffer();
64+
buffer[bufferDataLength] = newChar;
65+
66+
//if new line
67+
if (lastChar == '\r' && newChar == '\n')
68+
{
69+
return encoding.GetString(buffer, 0, bufferDataLength - 1);
70+
}
71+
//end of stream
72+
if (newChar == '\0')
73+
{
74+
return encoding.GetString(buffer, 0, bufferDataLength);
75+
}
76+
77+
bufferDataLength++;
78+
79+
//store last char for new line comparison
80+
lastChar = newChar;
81+
82+
if (bufferDataLength == buffer.Length)
83+
{
84+
ResizeBuffer(ref buffer, bufferDataLength * 2);
85+
}
6386
}
87+
88+
return encoding.GetString(buffer, 0, bufferDataLength);
6489
}
6590

6691
/// <summary>
@@ -78,6 +103,17 @@ internal async Task<List<string>> ReadAllLinesAsync()
78103
return requestLines;
79104
}
80105

106+
/// <summary>
107+
/// Read until the last new line, ignores the result
108+
/// </summary>
109+
/// <returns></returns>
110+
internal async Task ReadAndIgnoreAllLinesAsync()
111+
{
112+
while (!string.IsNullOrEmpty(await ReadLineAsync()))
113+
{
114+
}
115+
}
116+
81117
/// <summary>
82118
/// Read the specified number of raw bytes from the base stream
83119
/// </summary>
@@ -91,34 +127,51 @@ internal async Task<byte[]> ReadBytesAsync(int bufferSize, long totalBytesToRead
91127
if (totalBytesToRead < bufferSize)
92128
bytesToRead = (int)totalBytesToRead;
93129

94-
var buffer = new byte[bufferSize];
130+
var buffer = this.buffer;
131+
if (bytesToRead > buffer.Length)
132+
{
133+
buffer = new byte[bytesToRead];
134+
}
95135

96-
var bytesRead = 0;
136+
int bytesRead;
97137
var totalBytesRead = 0;
98138

99-
using (var outStream = new MemoryStream())
139+
while ((bytesRead = await stream.ReadAsync(buffer, totalBytesRead, bytesToRead)) > 0)
100140
{
101-
while ((bytesRead += await stream.ReadAsync(buffer, 0, bytesToRead)) > 0)
102-
{
103-
await outStream.WriteAsync(buffer, 0, bytesRead);
104-
totalBytesRead += bytesRead;
141+
totalBytesRead += bytesRead;
142+
143+
if (totalBytesRead == totalBytesToRead)
144+
break;
105145

106-
if (totalBytesRead == totalBytesToRead)
107-
break;
146+
var remainingBytes = totalBytesToRead - totalBytesRead;
147+
bytesToRead = Math.Min(bufferSize, (int)remainingBytes);
108148

109-
bytesRead = 0;
110-
var remainingBytes = (totalBytesToRead - totalBytesRead);
111-
bytesToRead = remainingBytes > (long)bufferSize ? bufferSize : (int)remainingBytes;
149+
if (totalBytesRead + bytesToRead > buffer.Length)
150+
{
151+
ResizeBuffer(ref buffer, Math.Min(totalBytesToRead, buffer.Length * 2));
112152
}
153+
}
113154

114-
return outStream.ToArray();
155+
if (totalBytesRead != buffer.Length)
156+
{
157+
//Normally this should not happen. Resize the buffer anyway
158+
var newBuffer = new byte[totalBytesRead];
159+
Buffer.BlockCopy(buffer, 0, newBuffer, 0, totalBytesRead);
160+
buffer = newBuffer;
115161
}
116162

163+
return buffer;
117164
}
118165

119166
public void Dispose()
120167
{
168+
}
121169

170+
private void ResizeBuffer(ref byte[] buffer, long size)
171+
{
172+
var newBuffer = new byte[size];
173+
Buffer.BlockCopy(buffer, 0, newBuffer, 0, buffer.Length);
174+
buffer = newBuffer;
122175
}
123176
}
124177
}

0 commit comments

Comments
 (0)