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

Commit cb263c6

Browse files
justcoding121justcoding121
authored andcommitted
Update readme; new api; fix gzip decompression
1 parent 4bf6ee4 commit cb263c6

File tree

6 files changed

+120
-110
lines changed

6 files changed

+120
-110
lines changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ Sample request and response event handlers
5858
//Test On Request, intercept requests
5959
public void OnRequest(object sender, SessionEventArgs e)
6060
{
61-
Console.WriteLine(e.RequestURL);
62-
61+
Console.WriteLine(e.ProxySession.Request.RequestUrl);
62+
6363
//read request headers
64-
var requestHeaders = e.RequestHeaders;
64+
var requestHeaders = e.ProxySession.Request.RequestHeaders;
6565

66-
if ((e.RequestMethod.ToUpper() == "POST" || e.RequestMethod.ToUpper() == "PUT") && e.RequestContentLength > 0)
66+
if ((e.RequestMethod.ToUpper() == "POST" || e.RequestMethod.ToUpper() == "PUT"))
6767
{
6868
//Get/Set request body bytes
6969
byte[] bodyBytes = e.GetRequestBody();
@@ -78,18 +78,19 @@ Sample request and response event handlers
7878
//To cancel a request with a custom HTML content
7979
//Filter URL
8080
81-
if (e.RequestURL.Contains("google.com"))
81+
if (e.ProxySession.Request.RequestUrl.Contains("google.com"))
8282
{
8383
e.Ok("<!DOCTYPE html><html><body><h1>Website Blocked</h1><p>Blocked by titanium web proxy.</p></body></html>");
8484
}
8585
}
8686

8787
public void OnResponse(object sender, SessionEventArgs e)
8888
{
89-
//read response headers
90-
var responseHeaders = e.ResponseHeaders;
89+
////read response headers
90+
var responseHeaders = e.ProxySession.Response.ResponseHeaders;
91+
9192

92-
if (e.ResponseStatusCode == HttpStatusCode.OK)
93+
if (e.ResponseStatusCode == "200")
9394
{
9495
if (e.ResponseContentType.Trim().ToLower().Contains("text/html"))
9596
{

Titanium.Web.Proxy.Test/ProxyTestController.cs

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,28 @@ public void OnRequest(object sender, SessionEventArgs e)
4646
{
4747
Console.WriteLine(e.ProxySession.Request.RequestUrl);
4848

49-
////read request headers
50-
//var requestHeaders = e.ProxySession.Request.RequestHeaders;
49+
//read request headers
50+
var requestHeaders = e.ProxySession.Request.RequestHeaders;
5151

52-
//if ((e.RequestMethod.ToUpper() == "POST" || e.RequestMethod.ToUpper() == "PUT"))
53-
//{
54-
// //Get/Set request body bytes
55-
// byte[] bodyBytes = e.GetRequestBody();
56-
// e.SetRequestBody(bodyBytes);
52+
if ((e.RequestMethod.ToUpper() == "POST" || e.RequestMethod.ToUpper() == "PUT"))
53+
{
54+
//Get/Set request body bytes
55+
byte[] bodyBytes = e.GetRequestBody();
56+
e.SetRequestBody(bodyBytes);
5757

58-
// //Get/Set request body as string
59-
// string bodyString = e.GetRequestBodyAsString();
60-
// e.SetRequestBodyString(bodyString);
58+
//Get/Set request body as string
59+
string bodyString = e.GetRequestBodyAsString();
60+
e.SetRequestBodyString(bodyString);
6161

62-
//}
62+
}
6363

64-
////To cancel a request with a custom HTML content
65-
////Filter URL
64+
//To cancel a request with a custom HTML content
65+
//Filter URL
6666

67-
//if (e.ProxySession.Request.RequestUrl.Contains("google.com"))
68-
//{
69-
// e.Ok("<!DOCTYPE html><html><body><h1>Website Blocked</h1><p>Blocked by titanium web proxy.</p></body></html>");
70-
//}
67+
if (e.ProxySession.Request.RequestUrl.Contains("google.com"))
68+
{
69+
e.Ok("<!DOCTYPE html><html><body><h1>Website Blocked</h1><p>Blocked by titanium web proxy.</p></body></html>");
70+
}
7171
}
7272

7373
//Test script injection
@@ -78,25 +78,25 @@ public void OnResponse(object sender, SessionEventArgs e)
7878
var responseHeaders = e.ProxySession.Response.ResponseHeaders;
7979

8080

81-
//if (e.ResponseStatusCode == "200")
82-
//{
83-
// if (e.ResponseContentType.Trim().ToLower().Contains("text/html"))
84-
// {
85-
// //Get/Set response body bytes
86-
// byte[] responseBodyBytes = e.GetResponseBody();
87-
// e.SetResponseBody(responseBodyBytes);
81+
if (e.ResponseStatusCode == "200")
82+
{
83+
if (e.ResponseContentType.Trim().ToLower().Contains("text/html"))
84+
{
85+
//Get/Set response body bytes
86+
byte[] responseBodyBytes = e.GetResponseBody();
87+
e.SetResponseBody(responseBodyBytes);
8888

89-
// //Get response body as string
90-
// string responseBody = e.GetResponseBodyAsString();
89+
//Get response body as string
90+
string responseBody = e.GetResponseBodyAsString();
9191

92-
// //Modify e.ServerResponse
93-
// Regex rex = new Regex("</body>", RegexOptions.RightToLeft | RegexOptions.IgnoreCase | RegexOptions.Multiline);
94-
// string modified = rex.Replace(responseBody, "<script type =\"text/javascript\">alert('Response was modified by this script!');</script></body>", 1);
92+
//Modify e.ServerResponse
93+
Regex rex = new Regex("</body>", RegexOptions.RightToLeft | RegexOptions.IgnoreCase | RegexOptions.Multiline);
94+
string modified = rex.Replace(responseBody, "<script type =\"text/javascript\">alert('Response was modified by this script!');</script></body>", 1);
9595

96-
// //Set modifed response Html Body
97-
// e.SetResponseBodyString(modified);
98-
// }
99-
//}
96+
//Set modifed response Html Body
97+
e.SetResponseBodyString(modified);
98+
}
99+
}
100100
}
101101
}
102102
}

Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal SessionEventArgs(int bufferSize)
3232
{
3333
_bufferSize = bufferSize;
3434
Client = new Client();
35-
ProxySession = new HttpWebSession();
35+
ProxySession = new HttpWebSession();
3636
}
3737

3838
public Client Client { get; set; }
@@ -79,7 +79,7 @@ public string ResponseContentType
7979

8080
public void Dispose()
8181
{
82-
82+
8383
}
8484

8585
private void ReadRequestBody()
@@ -139,12 +139,13 @@ private void ReadRequestBody()
139139
}
140140
}
141141
}
142+
142143
try
143144
{
144145
switch (requestContentEncoding)
145146
{
146147
case "gzip":
147-
ProxySession.Request.RequestBody = CompressionHelper.DecompressGzip(requestBodyStream);
148+
ProxySession.Request.RequestBody = CompressionHelper.DecompressGzip(requestBodyStream.ToArray());
148149
break;
149150
case "deflate":
150151
ProxySession.Request.RequestBody = CompressionHelper.DecompressDeflate(requestBodyStream);
@@ -171,41 +172,57 @@ private void ReadResponseBody()
171172
{
172173
if (ProxySession.Response.ResponseBody == null)
173174
{
174-
switch (ProxySession.Response.ResponseContentEncoding)
175+
using (var responseBodyStream = new MemoryStream())
175176
{
176-
case "gzip":
177-
ProxySession.Response.ResponseBody = CompressionHelper.DecompressGzip(ProxySession.Response.ResponseStream);
178-
break;
179-
case "deflate":
180-
ProxySession.Response.ResponseBody = CompressionHelper.DecompressDeflate(ProxySession.Response.ResponseStream);
181-
break;
182-
case "zlib":
183-
ProxySession.Response.ResponseBody = CompressionHelper.DecompressZlib(ProxySession.Response.ResponseStream);
184-
break;
185-
default:
186-
ProxySession.Response.ResponseBody = DecodeData(ProxySession.Response.ResponseStream);
187-
break;
177+
if (ProxySession.Response.IsChunked)
178+
{
179+
while (true)
180+
{
181+
var chuchkHead = ProxySession.ProxyClient.ServerStreamReader.ReadLine();
182+
var chunkSize = int.Parse(chuchkHead, NumberStyles.HexNumber);
183+
184+
if (chunkSize != 0)
185+
{
186+
var buffer = ProxySession.ProxyClient.ServerStreamReader.ReadBytes(chunkSize);
187+
responseBodyStream.Write(buffer, 0, buffer.Length);
188+
//chunk trail
189+
ProxySession.ProxyClient.ServerStreamReader.ReadLine();
190+
}
191+
else
192+
{
193+
ProxySession.ProxyClient.ServerStreamReader.ReadLine();
194+
break;
195+
}
196+
}
197+
}
198+
else
199+
{
200+
var buffer = ProxySession.ProxyClient.ServerStreamReader.ReadBytes(ProxySession.Response.ContentLength);
201+
responseBodyStream.Write(buffer, 0, buffer.Length);
202+
}
203+
204+
switch (ProxySession.Response.ResponseContentEncoding)
205+
{
206+
case "gzip":
207+
ProxySession.Response.ResponseBody = CompressionHelper.DecompressGzip(responseBodyStream.ToArray());
208+
break;
209+
case "deflate":
210+
ProxySession.Response.ResponseBody = CompressionHelper.DecompressDeflate(responseBodyStream);
211+
break;
212+
case "zlib":
213+
ProxySession.Response.ResponseBody = CompressionHelper.DecompressZlib(responseBodyStream);
214+
break;
215+
default:
216+
ProxySession.Response.ResponseBody = responseBodyStream.ToArray();
217+
break;
218+
}
188219
}
189220

190221
ProxySession.Response.ResponseBodyRead = true;
191222
}
192223
}
193224

194225

195-
//stream reader not recomended for images
196-
private byte[] DecodeData(Stream responseStream)
197-
{
198-
var buffer = new byte[_bufferSize];
199-
using (var ms = new MemoryStream())
200-
{
201-
int read;
202-
while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0)
203-
{
204-
ms.Write(buffer, 0, read);
205-
}
206-
return ms.ToArray();
207-
}
208-
}
209226

210227
public Encoding GetRequestBodyEncoding()
211228
{

Titanium.Web.Proxy/Helpers/Compression.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,10 @@ public static byte[] CompressGzip(byte[] bytes)
5050
}
5151
}
5252

53-
public static byte[] DecompressGzip(Stream input)
53+
//identify why passing stream instead of bytes returns empty result
54+
public static byte[] DecompressGzip(byte[] gzip)
5455
{
55-
using (
56-
var decompressor = new System.IO.Compression.GZipStream(input,
57-
System.IO.Compression.CompressionMode.Decompress))
56+
using (var decompressor = new System.IO.Compression.GZipStream(new MemoryStream(gzip), System.IO.Compression.CompressionMode.Decompress))
5857
{
5958
var buffer = new byte[BufferSize];
6059

Titanium.Web.Proxy/Network/HttpWebClient.cs

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ namespace Titanium.Web.Proxy.Network
1414
{
1515
public class Request
1616
{
17-
public string Method { get; set; }
18-
public Uri RequestUri { get; set; }
19-
public string Version { get; set; }
17+
public string Method { get; internal set; }
18+
public Uri RequestUri { get; internal set; }
19+
public string Version { get; internal set; }
2020

21-
public string RequestStatus { get; set; }
22-
public int RequestContentLength { get; set; }
23-
public bool RequestSendChunked { get; set; }
24-
public string RequestContentType { get; set; }
25-
public bool RequestKeepAlive { get; set; }
26-
public string RequestHost { get; set; }
21+
public string RequestStatus { get; internal set; }
22+
public int RequestContentLength { get; internal set; }
23+
public bool RequestSendChunked { get; internal set; }
24+
public string RequestContentType { get; internal set; }
25+
public bool RequestKeepAlive { get; internal set; }
26+
public string RequestHost { get; internal set; }
2727

2828
public string RequestUrl { get; internal set; }
2929

@@ -34,7 +34,7 @@ public class Request
3434
internal byte[] RequestBody { get; set; }
3535
internal string RequestBodyString { get; set; }
3636
internal bool RequestBodyRead { get; set; }
37-
public bool UpgradeToWebSocket { get; set; }
37+
internal bool UpgradeToWebSocket { get; set; }
3838
public List<HttpHeader> RequestHeaders { get; internal set; }
3939
internal bool RequestLocked { get; set; }
4040

@@ -55,25 +55,21 @@ public class Response
5555
internal bool ResponseBodyRead { get; set; }
5656
internal bool ResponseLocked { get; set; }
5757
public List<HttpHeader> ResponseHeaders { get; internal set; }
58-
public string ResponseCharacterSet { get; set; }
59-
public string ResponseContentEncoding { get; set; }
60-
public System.Version ResponseProtocolVersion { get; set; }
61-
public string ResponseStatusCode { get; set; }
62-
public string ResponseStatusDescription { get; set; }
63-
public bool ResponseKeepAlive { get; set; }
64-
public string ResponseContentType { get; set; }
65-
public int ContentLength { get; set; }
66-
public bool IsChunked { get; set; }
58+
internal string ResponseCharacterSet { get; set; }
59+
internal string ResponseContentEncoding { get; set; }
60+
internal System.Version ResponseProtocolVersion { get; set; }
61+
internal string ResponseStatusCode { get; set; }
62+
internal string ResponseStatusDescription { get; set; }
63+
internal bool ResponseKeepAlive { get; set; }
64+
internal string ResponseContentType { get; set; }
65+
internal int ContentLength { get; set; }
66+
internal bool IsChunked { get; set; }
6767

6868
public Response()
6969
{
7070
this.ResponseHeaders = new List<HttpHeader>();
7171
this.ResponseKeepAlive = true;
72-
}
73-
74-
75-
76-
72+
}
7773
}
7874

7975
public class HttpWebSession
@@ -90,7 +86,7 @@ public bool IsSecure
9086

9187
public Request Request { get; set; }
9288
public Response Response { get; set; }
93-
public TcpConnection ProxyClient { get; set; }
89+
internal TcpConnection ProxyClient { get; set; }
9490

9591
public void SetConnection(TcpConnection Connection)
9692
{
@@ -102,12 +98,8 @@ public HttpWebSession()
10298
{
10399
this.Request = new Request();
104100
this.Response = new Response();
105-
106-
107101
}
108102

109-
110-
111103
public void SendRequest()
112104
{
113105
Stream stream = ProxyClient.Stream;
@@ -169,8 +161,6 @@ public void ReceiveResponse()
169161
}
170162
}
171163

172-
173-
174164
}
175165

176166

0 commit comments

Comments
 (0)