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

Commit de33f72

Browse files
committed
Fix issues identified from review issue #27
1 parent 4ac7568 commit de33f72

File tree

7 files changed

+80
-111
lines changed

7 files changed

+80
-111
lines changed

Titanium.Web.Proxy.Test/ProxyTestController.cs

+14-20
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void Stop()
4444
//Read browser URL send back to proxy by the injection script in OnResponse event
4545
public void OnRequest(object sender, SessionEventArgs e)
4646
{
47-
Console.WriteLine(e.ProxySession.Request.RequestUrl);
47+
Console.WriteLine(e.ProxySession.Request.Url);
4848

4949
////read request headers
5050
//var requestHeaders = e.ProxySession.Request.RequestHeaders;
@@ -74,29 +74,23 @@ public void OnRequest(object sender, SessionEventArgs e)
7474
//Insert script to read the Browser URL and send it back to proxy
7575
public void OnResponse(object sender, SessionEventArgs e)
7676
{
77+
7778
////read response headers
7879
var responseHeaders = e.ProxySession.Response.ResponseHeaders;
7980

8081

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);
88-
89-
// //Get response body as string
90-
// string responseBody = e.GetResponseBodyAsString();
91-
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);
95-
96-
// //Set modifed response Html Body
97-
// e.SetResponseBodyString(modified);
98-
// }
99-
//}
82+
83+
//if (!e.ProxySession.Request.Hostname.Equals("medeczane.sgk.gov.tr")) return;
84+
if (e.RequestMethod == "GET" || e.RequestMethod == "POST")
85+
{
86+
if (e.ProxySession.Response.ResponseStatusCode == "200")
87+
{
88+
if (e.ProxySession.Response.ContentType.Trim().ToLower().Contains("text/html"))
89+
{
90+
string body = e.GetResponseBodyAsString(); //This line crashes
91+
}
92+
}
93+
}
10094
}
10195
}
10296
}

Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

+12-23
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,18 @@ internal SessionEventArgs(int bufferSize)
3535
ProxySession = new HttpWebSession();
3636
}
3737

38-
public Client Client { get; set; }
38+
internal Client Client { get; set; }
3939

4040
public bool IsHttps { get; internal set; }
4141

4242
public HttpWebSession ProxySession { get; set; }
4343

4444

45-
4645
public int RequestContentLength
4746
{
4847
get
4948
{
50-
if (ProxySession.Request.RequestHeaders.All(x => x.Name.ToLower() != "content-length")) return -1;
51-
int contentLen;
52-
int.TryParse(ProxySession.Request.RequestHeaders.First(x => x.Name.ToLower() == "content-length").Value, out contentLen);
53-
if (contentLen != 0)
54-
return contentLen;
55-
return -1;
49+
return ProxySession.Request.ContentLength;
5650
}
5751
}
5852

@@ -71,9 +65,7 @@ public string ResponseContentType
7165
{
7266
get
7367
{
74-
return ProxySession.Response.ResponseHeaders.Any(x => x.Name.ToLower() == "content-type")
75-
? ProxySession.Response.ResponseHeaders.First(x => x.Name.ToLower() == "content-type").Value
76-
: null;
68+
return ProxySession.Response.ContentType;
7769
}
7870
}
7971

@@ -201,7 +193,7 @@ private void ReadResponseBody()
201193
responseBodyStream.Write(buffer, 0, buffer.Length);
202194
}
203195

204-
switch (ProxySession.Response.ResponseContentEncoding)
196+
switch (ProxySession.Response.ContentEncoding)
205197
{
206198
case "gzip":
207199
ProxySession.Response.ResponseBody = CompressionHelper.DecompressGzip(responseBodyStream.ToArray());
@@ -223,12 +215,11 @@ private void ReadResponseBody()
223215
}
224216

225217

226-
227218
public Encoding GetRequestBodyEncoding()
228219
{
229220
if (ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function after request is made to server.");
230221

231-
return ProxySession.Request.RequestEncoding;
222+
return ProxySession.Request.Encoding;
232223
}
233224

234225
public byte[] GetRequestBody()
@@ -246,7 +237,7 @@ public string GetRequestBodyAsString()
246237

247238
ReadRequestBody();
248239

249-
return ProxySession.Request.RequestBodyString ?? (ProxySession.Request.RequestBodyString = ProxySession.Request.RequestEncoding.GetString(ProxySession.Request.RequestBody));
240+
return ProxySession.Request.RequestBodyString ?? (ProxySession.Request.RequestBodyString = ProxySession.Request.Encoding.GetString(ProxySession.Request.RequestBody));
250241
}
251242

252243
public void SetRequestBody(byte[] body)
@@ -271,15 +262,15 @@ public void SetRequestBodyString(string body)
271262
ReadRequestBody();
272263
}
273264

274-
ProxySession.Request.RequestBody = ProxySession.Request.RequestEncoding.GetBytes(body);
265+
ProxySession.Request.RequestBody = ProxySession.Request.Encoding.GetBytes(body);
275266
ProxySession.Request.RequestBodyRead = true;
276267
}
277268

278269
public Encoding GetResponseBodyEncoding()
279270
{
280271
if (!ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function before request is made to server.");
281272

282-
return ProxySession.Response.ResponseEncoding;
273+
return ProxySession.Response.Encoding;
283274
}
284275

285276
public byte[] GetResponseBody()
@@ -296,7 +287,7 @@ public string GetResponseBodyAsString()
296287

297288
GetResponseBody();
298289

299-
return ProxySession.Response.ResponseBodyString ?? (ProxySession.Response.ResponseBodyString = ProxySession.Response.ResponseEncoding.GetString(ProxySession.Response.ResponseBody));
290+
return ProxySession.Response.ResponseBodyString ?? (ProxySession.Response.ResponseBodyString = ProxySession.Response.Encoding.GetString(ProxySession.Response.ResponseBody));
300291
}
301292

302293
public void SetResponseBody(byte[] body)
@@ -320,7 +311,7 @@ public void SetResponseBodyString(string body)
320311
GetResponseBody();
321312
}
322313

323-
var bodyBytes = ProxySession.Response.ResponseEncoding.GetBytes(body);
314+
var bodyBytes = ProxySession.Response.Encoding.GetBytes(body);
324315
SetResponseBody(bodyBytes);
325316
}
326317

@@ -335,22 +326,20 @@ public void Ok(string html)
335326
var result = Encoding.Default.GetBytes(html);
336327

337328
var connectStreamWriter = new StreamWriter(this.Client.ClientStream);
338-
var s = string.Format("HTTP/{0}.{1} {2} {3}", ProxySession.Request.RequestHttpVersion.Major, ProxySession.Request.RequestHttpVersion.Minor, 200, "Ok");
339-
connectStreamWriter.WriteLine(s);
329+
connectStreamWriter.WriteLine(string.Format("{0} {2} {3}", ProxySession.Request.HttpVersion, 200, "Ok"));
340330
connectStreamWriter.WriteLine("Timestamp: {0}", DateTime.Now);
341331
connectStreamWriter.WriteLine("content-length: " + result.Length);
342332
connectStreamWriter.WriteLine("Cache-Control: no-cache, no-store, must-revalidate");
343333
connectStreamWriter.WriteLine("Pragma: no-cache");
344334
connectStreamWriter.WriteLine("Expires: 0");
345335

346-
connectStreamWriter.WriteLine(ProxySession.Request.RequestIsAlive ? "Connection: Keep-Alive" : "Connection: close");
336+
connectStreamWriter.WriteLine(ProxySession.Request.IsAlive ? "Connection: Keep-Alive" : "Connection: close");
347337

348338
connectStreamWriter.WriteLine();
349339
connectStreamWriter.Flush();
350340

351341
this.Client.ClientStream.Write(result, 0, result.Length);
352342

353-
354343
ProxySession.Request.CancelRequest = true;
355344
}
356345
}

Titanium.Web.Proxy/Extensions/HttpWebRequestExtensions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ public static Encoding GetEncoding(this HttpWebSession request)
1010
{
1111
try
1212
{
13-
if (request.Request.RequestContentType == null) return Encoding.GetEncoding("ISO-8859-1");
13+
if (request.Request.ContentType == null) return Encoding.GetEncoding("ISO-8859-1");
1414

15-
var contentTypes = request.Request.RequestContentType.Split(';');
15+
var contentTypes = request.Request.ContentType.Split(';');
1616
foreach (var contentType in contentTypes)
1717
{
1818
var encodingSplit = contentType.Split('=');

Titanium.Web.Proxy/Extensions/HttpWebResponseExtensions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ public static class HttpWebResponseExtensions
88
{
99
public static Encoding GetResponseEncoding(this HttpWebSession response)
1010
{
11-
if (string.IsNullOrEmpty(response.Response.ResponseCharacterSet)) return Encoding.GetEncoding("ISO-8859-1");
12-
return Encoding.GetEncoding(response.Response.ResponseCharacterSet.Replace(@"""", string.Empty));
11+
if (string.IsNullOrEmpty(response.Response.CharacterSet)) return Encoding.GetEncoding("ISO-8859-1");
12+
return Encoding.GetEncoding(response.Response.CharacterSet.Replace(@"""", string.Empty));
1313
}
1414
}
1515
}

Titanium.Web.Proxy/Network/HttpWebClient.cs

+19-31
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,19 @@ public class Request
1616
{
1717
public string Method { get; internal set; }
1818
public Uri RequestUri { get; internal set; }
19-
public string Version { get; internal set; }
19+
public string HttpVersion { get; internal set; }
2020

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; }
21+
public string Status { get; internal set; }
22+
public int ContentLength { get; internal set; }
23+
public bool SendChunked { get; internal set; }
24+
public string ContentType { get; internal set; }
25+
public bool KeepAlive { get; internal set; }
26+
public string Hostname { get; internal set; }
2727

28-
public string RequestUrl { get; internal set; }
28+
public string Url { get; internal set; }
2929

30-
internal Encoding RequestEncoding { get; set; }
31-
internal Version RequestHttpVersion { get; set; }
32-
internal bool RequestIsAlive { get; set; }
30+
internal Encoding Encoding { get; set; }
31+
internal bool IsAlive { get; set; }
3332
internal bool CancelRequest { get; set; }
3433
internal byte[] RequestBody { get; set; }
3534
internal string RequestBodyString { get; set; }
@@ -48,20 +47,20 @@ public Request()
4847
public class Response
4948
{
5049

51-
internal Encoding ResponseEncoding { get; set; }
50+
internal Encoding Encoding { get; set; }
5251
internal Stream ResponseStream { get; set; }
5352
internal byte[] ResponseBody { get; set; }
5453
internal string ResponseBodyString { get; set; }
5554
internal bool ResponseBodyRead { get; set; }
5655
internal bool ResponseLocked { get; set; }
5756
public List<HttpHeader> ResponseHeaders { get; internal 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; }
57+
internal string CharacterSet { get; set; }
58+
internal string ContentEncoding { get; set; }
59+
internal string HttpVersion { get; set; }
60+
public string ResponseStatusCode { get; internal set; }
61+
public string ResponseStatusDescription { get; internal set; }
6362
internal bool ResponseKeepAlive { get; set; }
64-
internal string ResponseContentType { get; set; }
63+
public string ContentType { get; internal set; }
6564
internal int ContentLength { get; set; }
6665
internal bool IsChunked { get; set; }
6766

@@ -110,7 +109,7 @@ public void SendRequest()
110109
{
111110
this.Request.Method,
112111
this.Request.RequestUri.PathAndQuery,
113-
this.Request.Version
112+
this.Request.HttpVersion
114113
}));
115114

116115
foreach (HttpHeader httpHeader in this.Request.RequestHeaders)
@@ -134,19 +133,8 @@ public void ReceiveResponse()
134133
{
135134
var s = ProxyClient.ServerStreamReader.ReadLine();
136135
}
137-
var httpVersion = httpResult[0];
138136

139-
Version version;
140-
if (httpVersion == "HTTP/1.1")
141-
{
142-
version = new Version(1, 1);
143-
}
144-
else
145-
{
146-
version = new Version(1, 0);
147-
}
148-
149-
this.Response.ResponseProtocolVersion = version;
137+
this.Response.HttpVersion = httpResult[0];
150138
this.Response.ResponseStatusCode = httpResult[1];
151139
string status = httpResult[2];
152140

0 commit comments

Comments
 (0)