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

Commit 6601d02

Browse files
committed
add comments
1 parent 0246b70 commit 6601d02

File tree

2 files changed

+106
-35
lines changed

2 files changed

+106
-35
lines changed

Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

Lines changed: 106 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,48 @@
1313

1414
namespace Titanium.Web.Proxy.EventArguments
1515
{
16-
public class Client
17-
{
18-
internal TcpClient TcpClient { get; set; }
19-
internal Stream ClientStream { get; set; }
20-
internal CustomBinaryReader ClientStreamReader { get; set; }
21-
internal StreamWriter ClientStreamWriter { get; set; }
22-
23-
public int ClientPort { get; internal set; }
24-
public IPAddress ClientIpAddress { get; internal set; }
2516

26-
}
17+
/// <summary>
18+
/// Holds info related to a single proxy session (single request/response sequence)
19+
/// A proxy session is bounded to a single connection from client
20+
/// A proxy session ends when client terminates connection to proxy
21+
/// or when server terminates connection from proxy
22+
/// </summary>
2723
public class SessionEventArgs : EventArgs, IDisposable
2824
{
2925
readonly int _bufferSize;
3026

27+
/// <summary>
28+
/// Constructor to initialize the proxy
29+
/// </summary>
3130
internal SessionEventArgs(int bufferSize)
3231
{
3332
_bufferSize = bufferSize;
34-
Client = new Client();
33+
Client = new ProxyClient();
3534
ProxySession = new HttpWebSession();
3635
}
3736

38-
internal Client Client { get; set; }
37+
/// <summary>
38+
/// Holds a reference to server connection
39+
/// </summary>
40+
internal ProxyClient Client { get; set; }
3941

42+
43+
/// <summary>
44+
/// Does this session uses SSL
45+
/// </summary>
4046
public bool IsHttps { get; internal set; }
4147

48+
/// <summary>
49+
/// A web session corresponding to a single request/response sequence
50+
/// within a proxy connection
51+
/// </summary>
4252
public HttpWebSession ProxySession { get; set; }
4353

4454

55+
/// <summary>
56+
/// A shortcut to get the request content length
57+
/// </summary>
4558
public int RequestContentLength
4659
{
4760
get
@@ -50,17 +63,25 @@ public int RequestContentLength
5063
}
5164
}
5265

66+
/// <summary>
67+
/// A shortcut to get the request Method (GET/POST/PUT etc)
68+
/// </summary>
5369
public string RequestMethod
5470
{
5571
get { return ProxySession.Request.Method; }
5672
}
5773

58-
74+
/// <summary>
75+
/// A shortcut to get the response status code (200 OK, 404 etc)
76+
/// </summary>
5977
public string ResponseStatusCode
6078
{
6179
get { return ProxySession.Response.ResponseStatusCode; }
6280
}
6381

82+
/// <summary>
83+
/// A shortcut to get the response content type
84+
/// </summary>
6485
public string ResponseContentType
6586
{
6687
get
@@ -69,30 +90,39 @@ public string ResponseContentType
6990
}
7091
}
7192

93+
/// <summary>
94+
/// implement any cleanup here
95+
/// </summary>
7296
public void Dispose()
7397
{
7498

7599
}
76100

101+
/// <summary>
102+
/// Read request body content as bytes[] for current session
103+
/// </summary>
77104
private void ReadRequestBody()
78105
{
106+
//GET request don't have a request body to read
79107
if ((ProxySession.Request.Method.ToUpper() != "POST" && ProxySession.Request.Method.ToUpper() != "PUT"))
80108
{
81109
throw new BodyNotFoundException("Request don't have a body." +
82110
"Please verify that this request is a Http POST/PUT and request content length is greater than zero before accessing the body.");
83111
}
84112

113+
//Caching check
85114
if (ProxySession.Request.RequestBody == null)
86115
{
87116
var isChunked = false;
88117
string requestContentEncoding = null;
89118

90-
119+
//get compression method (gzip, zlib etc)
91120
if (ProxySession.Request.RequestHeaders.Any(x => x.Name.ToLower() == "content-encoding"))
92121
{
93122
requestContentEncoding = ProxySession.Request.RequestHeaders.First(x => x.Name.ToLower() == "content-encoding").Value;
94123
}
95124

125+
//check if the request have chunked body (body send chunck by chunck without a fixed length)
96126
if (ProxySession.Request.RequestHeaders.Any(x => x.Name.ToLower() == "transfer-encoding"))
97127
{
98128
var transferEncoding =
@@ -103,13 +133,14 @@ private void ReadRequestBody()
103133
}
104134
}
105135

106-
136+
//If not chunked then its easy just read the whole body with the content length mentioned in the request header
107137
if (requestContentEncoding == null && !isChunked)
108138
ProxySession.Request.RequestBody = this.Client.ClientStreamReader.ReadBytes(RequestContentLength);
109139
else
110140
{
111141
using (var requestBodyStream = new MemoryStream())
112142
{
143+
//For chunked request we need to read data as they arrive, until we reach a chunk end symbol
113144
if (isChunked)
114145
{
115146
while (true)
@@ -126,6 +157,7 @@ private void ReadRequestBody()
126157
}
127158
else
128159
{
160+
//chunk end
129161
this.Client.ClientStreamReader.ReadLine();
130162
break;
131163
}
@@ -134,6 +166,7 @@ private void ReadRequestBody()
134166

135167
try
136168
{
169+
//decompress
137170
switch (requestContentEncoding)
138171
{
139172
case "gzip":
@@ -152,20 +185,29 @@ private void ReadRequestBody()
152185
}
153186
catch
154187
{
188+
//if decompression fails, just assign the body stream as it it
189+
//Not a safe option
155190
ProxySession.Request.RequestBody = requestBodyStream.ToArray();
156191
}
157192
}
158193
}
159194
}
195+
//Now set the flag to true
196+
//So that next time we can deliver body from cache
160197
ProxySession.Request.RequestBodyRead = true;
161198
}
162199

200+
/// <summary>
201+
/// Read response body as byte[] for current response
202+
/// </summary>
163203
private void ReadResponseBody()
164204
{
205+
//If not already read (not cached yet)
165206
if (ProxySession.Response.ResponseBody == null)
166207
{
167208
using (var responseBodyStream = new MemoryStream())
168209
{
210+
//If chuncked the read chunk by chunk until we hit chunk end symbol
169211
if (ProxySession.Response.IsChunked)
170212
{
171213
while (true)
@@ -182,17 +224,19 @@ private void ReadResponseBody()
182224
}
183225
else
184226
{
227+
//chuck end
185228
ProxySession.ProxyClient.ServerStreamReader.ReadLine();
186229
break;
187230
}
188231
}
189232
}
190233
else
191234
{
235+
//If not chunked then its easy just read the amount of bytes mentioned in content length header of response
192236
var buffer = ProxySession.ProxyClient.ServerStreamReader.ReadBytes(ProxySession.Response.ContentLength);
193237
responseBodyStream.Write(buffer, 0, buffer.Length);
194238
}
195-
239+
//decompress
196240
switch (ProxySession.Response.ContentEncoding)
197241
{
198242
case "gzip":
@@ -209,41 +253,46 @@ private void ReadResponseBody()
209253
break;
210254
}
211255
}
212-
256+
//set this to true for caching
213257
ProxySession.Response.ResponseBodyRead = true;
214258
}
215259
}
216260

217-
218-
public Encoding GetRequestBodyEncoding()
219-
{
220-
if (ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function after request is made to server.");
221-
222-
return ProxySession.Request.Encoding;
223-
}
224-
261+
/// <summary>
262+
/// Gets the request body as bytes
263+
/// </summary>
264+
/// <returns></returns>
225265
public byte[] GetRequestBody()
226266
{
227267
if (ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function after request is made to server.");
228268

229269
ReadRequestBody();
230270
return ProxySession.Request.RequestBody;
231271
}
232-
272+
/// <summary>
273+
/// Gets the request body as string
274+
/// </summary>
275+
/// <returns></returns>
233276
public string GetRequestBodyAsString()
234277
{
235278
if (ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function after request is made to server.");
236279

237280

238281
ReadRequestBody();
239282

283+
//Use the encoding specified in request to decode the byte[] data to string
240284
return ProxySession.Request.RequestBodyString ?? (ProxySession.Request.RequestBodyString = ProxySession.Request.Encoding.GetString(ProxySession.Request.RequestBody));
241285
}
242286

287+
/// <summary>
288+
/// Sets the request body
289+
/// </summary>
290+
/// <param name="body"></param>
243291
public void SetRequestBody(byte[] body)
244292
{
245293
if (ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function after request is made to server.");
246294

295+
//syphon out the request body from client before setting the new body
247296
if (!ProxySession.Request.RequestBodyRead)
248297
{
249298
ReadRequestBody();
@@ -253,10 +302,15 @@ public void SetRequestBody(byte[] body)
253302
ProxySession.Request.RequestBodyRead = true;
254303
}
255304

305+
/// <summary>
306+
/// Sets the body with the specified string
307+
/// </summary>
308+
/// <param name="body"></param>
256309
public void SetRequestBodyString(string body)
257310
{
258311
if (ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function after request is made to server.");
259312

313+
//syphon out the request body from client before setting the new body
260314
if (!ProxySession.Request.RequestBodyRead)
261315
{
262316
ReadRequestBody();
@@ -266,13 +320,10 @@ public void SetRequestBodyString(string body)
266320
ProxySession.Request.RequestBodyRead = true;
267321
}
268322

269-
public Encoding GetResponseBodyEncoding()
270-
{
271-
if (!ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function before request is made to server.");
272-
273-
return ProxySession.Response.Encoding;
274-
}
275-
323+
/// <summary>
324+
/// Gets the response body as byte array
325+
/// </summary>
326+
/// <returns></returns>
276327
public byte[] GetResponseBody()
277328
{
278329
if (!ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function before request is made to server.");
@@ -281,6 +332,10 @@ public byte[] GetResponseBody()
281332
return ProxySession.Response.ResponseBody;
282333
}
283334

335+
/// <summary>
336+
/// Gets the response body as string
337+
/// </summary>
338+
/// <returns></returns>
284339
public string GetResponseBodyAsString()
285340
{
286341
if (!ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function before request is made to server.");
@@ -290,10 +345,15 @@ public string GetResponseBodyAsString()
290345
return ProxySession.Response.ResponseBodyString ?? (ProxySession.Response.ResponseBodyString = ProxySession.Response.Encoding.GetString(ProxySession.Response.ResponseBody));
291346
}
292347

348+
/// <summary>
349+
/// Set the response body bytes
350+
/// </summary>
351+
/// <param name="body"></param>
293352
public void SetResponseBody(byte[] body)
294353
{
295354
if (!ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function before request is made to server.");
296355

356+
//syphon out the response body from server before setting the new body
297357
if (ProxySession.Response.ResponseBody == null)
298358
{
299359
GetResponseBody();
@@ -302,10 +362,15 @@ public void SetResponseBody(byte[] body)
302362
ProxySession.Response.ResponseBody = body;
303363
}
304364

365+
/// <summary>
366+
/// Replace the response body with the specified string
367+
/// </summary>
368+
/// <param name="body"></param>
305369
public void SetResponseBodyString(string body)
306370
{
307371
if (!ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function before request is made to server.");
308372

373+
//syphon out the response body from server before setting the new body
309374
if (ProxySession.Response.ResponseBody == null)
310375
{
311376
GetResponseBody();
@@ -316,6 +381,14 @@ public void SetResponseBodyString(string body)
316381
}
317382

318383

384+
/// <summary>
385+
/// Before request is made to server
386+
/// Respond with the specified HTML string to client
387+
/// and ignore the request
388+
/// Marking as obsolete, need to comeup with a generic responder method in future
389+
/// </summary>
390+
/// <param name="html"></param>
391+
[Obsolete]
319392
public void Ok(string html)
320393
{
321394
if (ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function after request is made to server.");

Titanium.Web.Proxy/RequestHandler.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,6 @@ private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, S
239239
args.Client.ClientStreamWriter = clientStreamWriter;
240240
args.ProxySession.Request.Hostname = args.ProxySession.Request.RequestUri.Host;
241241
args.ProxySession.Request.Url = args.ProxySession.Request.RequestUri.OriginalString;
242-
args.Client.ClientPort = ((IPEndPoint)client.Client.RemoteEndPoint).Port;
243-
args.Client.ClientIpAddress = ((IPEndPoint)client.Client.RemoteEndPoint).Address;
244242

245243

246244
//If requested interception

0 commit comments

Comments
 (0)