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

Commit 19aa3b8

Browse files
committed
add comments
1 parent d16a2bf commit 19aa3b8

File tree

8 files changed

+108
-5
lines changed

8 files changed

+108
-5
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Net.Sockets;
6+
using System.Text;
7+
using Titanium.Web.Proxy.Helpers;
8+
9+
namespace Titanium.Web.Proxy.EventArguments
10+
{
11+
/// <summary>
12+
/// This class wraps Tcp connection to Server
13+
/// </summary>
14+
public class ProxyClient
15+
{
16+
/// <summary>
17+
/// TcpClient used to communicate with server
18+
/// </summary>
19+
internal TcpClient TcpClient { get; set; }
20+
21+
/// <summary>
22+
/// holds the stream to server
23+
/// </summary>
24+
internal Stream ClientStream { get; set; }
25+
26+
/// <summary>
27+
/// Used to read line by line from server
28+
/// </summary>
29+
internal CustomBinaryReader ClientStreamReader { get; set; }
30+
31+
/// <summary>
32+
/// used to write line by line to server
33+
/// </summary>
34+
internal StreamWriter ClientStreamWriter { get; set; }
35+
36+
}
37+
}

Titanium.Web.Proxy/Exceptions/BodyNotFoundException.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Titanium.Web.Proxy.Exceptions
44
{
5+
/// <summary>
6+
/// An expception thrown when body is unexpectedly empty
7+
/// </summary>
58
public class BodyNotFoundException : Exception
69
{
710
public BodyNotFoundException(string message)

Titanium.Web.Proxy/Extensions/HttpWebRequestExtensions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44

55
namespace Titanium.Web.Proxy.Extensions
66
{
7+
/// <summary>
8+
/// Extensions on HttpWebSession object
9+
/// </summary>
710
public static class HttpWebRequestExtensions
811
{
12+
//Get encoding of the HTTP request
913
public static Encoding GetEncoding(this HttpWebSession request)
1014
{
1115
try
1216
{
17+
//return default if not specified
1318
if (request.Request.ContentType == null) return Encoding.GetEncoding("ISO-8859-1");
1419

20+
//extract the encoding by finding the charset
1521
var contentTypes = request.Request.ContentType.Split(';');
1622
foreach (var contentType in contentTypes)
1723
{
@@ -24,9 +30,11 @@ public static Encoding GetEncoding(this HttpWebSession request)
2430
}
2531
catch
2632
{
33+
//parsing errors
2734
// ignored
2835
}
2936

37+
//return default if not specified
3038
return Encoding.GetEncoding("ISO-8859-1");
3139
}
3240
}

Titanium.Web.Proxy/Helpers/Compression.cs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@
44

55
namespace Titanium.Web.Proxy.Helpers
66
{
7+
/// <summary>
8+
/// A helper to handle compression/decompression (gzip, zlib & deflate)
9+
/// </summary>
710
public class CompressionHelper
811
{
9-
private const int BufferSize = 8192;
10-
12+
/// <summary>
13+
/// compress the given bytes using zlib compression
14+
/// </summary>
15+
/// <param name="bytes"></param>
16+
/// <returns></returns>
1117
[SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
1218
public static byte[] CompressZlib(byte[] bytes)
1319
{
@@ -22,6 +28,11 @@ public static byte[] CompressZlib(byte[] bytes)
2228
}
2329
}
2430

31+
/// <summary>
32+
/// compress the given bytes using deflate compression
33+
/// </summary>
34+
/// <param name="bytes"></param>
35+
/// <returns></returns>
2536
[SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
2637
public static byte[] CompressDeflate(byte[] bytes)
2738
{
@@ -36,6 +47,11 @@ public static byte[] CompressDeflate(byte[] bytes)
3647
}
3748
}
3849

50+
/// <summary>
51+
/// compress the given bytes using gzip compression
52+
/// </summary>
53+
/// <param name="bytes"></param>
54+
/// <returns></returns>
3955
[SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
4056
public static byte[] CompressGzip(byte[] bytes)
4157
{
@@ -50,12 +66,17 @@ public static byte[] CompressGzip(byte[] bytes)
5066
}
5167
}
5268

69+
/// <summary>
70+
/// decompression the gzip compressed byte array
71+
/// </summary>
72+
/// <param name="gzip"></param>
73+
/// <returns></returns>
5374
//identify why passing stream instead of bytes returns empty result
5475
public static byte[] DecompressGzip(byte[] gzip)
5576
{
5677
using (var decompressor = new System.IO.Compression.GZipStream(new MemoryStream(gzip), System.IO.Compression.CompressionMode.Decompress))
5778
{
58-
var buffer = new byte[BufferSize];
79+
var buffer = new byte[ProxyServer.BUFFER_SIZE];
5980

6081
using (var output = new MemoryStream())
6182
{
@@ -69,11 +90,16 @@ public static byte[] DecompressGzip(byte[] gzip)
6990
}
7091
}
7192

93+
/// <summary>
94+
/// decompress the deflate byte stream
95+
/// </summary>
96+
/// <param name="input"></param>
97+
/// <returns></returns>
7298
public static byte[] DecompressDeflate(Stream input)
7399
{
74100
using (var decompressor = new DeflateStream(input, CompressionMode.Decompress))
75101
{
76-
var buffer = new byte[BufferSize];
102+
var buffer = new byte[ProxyServer.BUFFER_SIZE];
77103

78104
using (var output = new MemoryStream())
79105
{
@@ -87,11 +113,16 @@ public static byte[] DecompressDeflate(Stream input)
87113
}
88114
}
89115

116+
/// <summary>
117+
/// decompress the zlib byte stream
118+
/// </summary>
119+
/// <param name="input"></param>
120+
/// <returns></returns>
90121
public static byte[] DecompressZlib(Stream input)
91122
{
92123
using (var decompressor = new ZlibStream(input, CompressionMode.Decompress))
93124
{
94-
var buffer = new byte[BufferSize];
125+
var buffer = new byte[ProxyServer.BUFFER_SIZE];
95126

96127
using (var output = new MemoryStream())
97128
{

Titanium.Web.Proxy/Helpers/CustomBinaryReader.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,22 @@
55

66
namespace Titanium.Web.Proxy.Helpers
77
{
8+
/// <summary>
9+
/// A custom binary reader that would allo us to read string line by line
10+
/// using the specified encoding
11+
/// as well as to read bytes as required
12+
/// </summary>
813
public class CustomBinaryReader : BinaryReader
914
{
1015
internal CustomBinaryReader(Stream stream, Encoding encoding)
1116
: base(stream, encoding)
1217
{
1318
}
1419

20+
/// <summary>
21+
/// Read a line from the byte stream
22+
/// </summary>
23+
/// <returns></returns>
1524
internal string ReadLine()
1625
{
1726
var readBuffer = new StringBuilder();
@@ -43,6 +52,10 @@ internal string ReadLine()
4352
}
4453
}
4554

55+
/// <summary>
56+
/// Read until the last new line
57+
/// </summary>
58+
/// <returns></returns>
4659
internal List<string> ReadAllLines()
4760
{
4861
string tmpLine;

Titanium.Web.Proxy/Helpers/Firefox.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
namespace Titanium.Web.Proxy.Helpers
55
{
6+
/// <summary>
7+
/// A helper class to set proxy settings for firefox
8+
/// </summary>
69
public class FireFoxHelper
710
{
811
public static void AddFirefox()

Titanium.Web.Proxy/Models/HttpHeader.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Titanium.Web.Proxy.Models
44
{
5+
/// <summary>
6+
/// Http Header object used by proxy
7+
/// </summary>
58
public class HttpHeader
69
{
710
public HttpHeader(string name, string value)
@@ -15,6 +18,10 @@ public HttpHeader(string name, string value)
1518
public string Name { get; set; }
1619
public string Value { get; set; }
1720

21+
/// <summary>
22+
/// Returns header as a valid header string
23+
/// </summary>
24+
/// <returns></returns>
1825
public override string ToString()
1926
{
2027
return string.Format("{0}: {1}", Name, Value);

Titanium.Web.Proxy/Titanium.Web.Proxy.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
<Reference Include="System.Xml" />
8181
</ItemGroup>
8282
<ItemGroup>
83+
<Compile Include="EventArguments\ProxyClient.cs" />
8384
<Compile Include="Exceptions\BodyNotFoundException.cs" />
8485
<Compile Include="Extensions\HttpWebResponseExtensions.cs" />
8586
<Compile Include="Extensions\HttpWebRequestExtensions.cs" />

0 commit comments

Comments
 (0)