@@ -68,10 +68,11 @@ Sample request and response event handlers
68
68
69
69
``` csharp
70
70
71
- // Test On Request, intercept requests
71
+ // Test On Request, intecept requests
72
+ // Read browser URL send back to proxy by the injection script in OnResponse event
72
73
public void OnRequest (object sender , SessionEventArgs e )
73
74
{
74
- Console .WriteLine (e .ProxySession .Request .RequestUrl );
75
+ Console .WriteLine (e .ProxySession .Request .Url );
75
76
76
77
// read request headers
77
78
var requestHeaders = e .ProxySession .Request .RequestHeaders ;
@@ -91,38 +92,38 @@ Sample request and response event handlers
91
92
// To cancel a request with a custom HTML content
92
93
// Filter URL
93
94
94
- if (e .ProxySession .Request .RequestUrl .Contains (" google.com" ))
95
+ if (e .ProxySession .Request .RequestUri . AbsoluteUri .Contains (" google.com" ))
95
96
{
96
- e .Ok (" <!DOCTYPE html><html><body><h1>Website Blocked</h1><p>Blocked by titanium web proxy.</p></body></html>" );
97
+ e .Ok (" <!DOCTYPE html>" +
98
+ " <html><body><h1>" +
99
+ " Website Blocked" +
100
+ " </h1>" +
101
+ " <p>Blocked by titanium web proxy.</p>" +
102
+ " </body>" +
103
+ " </html>" );
97
104
}
98
105
}
99
-
100
- public void OnResponse (object sender , SessionEventArgs e )
101
- {
102
- // //read response headers
106
+
107
+ // Test script injection
108
+ // Insert script to read the Browser URL and send it back to proxy
109
+ public void OnResponse (object sender , SessionEventArgs e )
110
+ {
111
+
112
+ // read response headers
103
113
var responseHeaders = e .ProxySession .Response .ResponseHeaders ;
104
114
105
115
106
- if (e .ResponseStatusCode == " 200 " )
116
+ if (e .RequestMethod == " GET " || e . RequestMethod == " POST " )
107
117
{
108
- if (e .ResponseContentType . Trim (). ToLower (). Contains ( " text/html " ) )
118
+ if (e .ProxySession . Response . ResponseStatusCode == " 200 " )
109
119
{
110
- // Get/Set response body bytes
111
- byte [] responseBodyBytes = e .GetResponseBody ();
112
- e .SetResponseBody (responseBodyBytes );
113
-
114
- // Get response body as string
115
- string responseBody = e .GetResponseBodyAsString ();
116
-
117
- // Modify e.ServerResponse
118
- Regex rex = new Regex (" </body>" , RegexOptions .RightToLeft | RegexOptions .IgnoreCase | RegexOptions .Multiline );
119
- string modified = rex .Replace (responseBody , " <script type =\" text/javascript\" >alert('Response was modified by this script!');</script></body>" , 1 );
120
-
121
- // Set modifed response Html Body
122
- e .SetResponseBodyString (modified );
120
+ if (e .ProxySession .Response .ContentType .Trim ().ToLower ().Contains (" text/html" ))
121
+ {
122
+ string body = e .GetResponseBodyAsString ();
123
+ }
123
124
}
124
125
}
125
- }
126
+ }
126
127
```
127
128
Future roadmap
128
129
============
0 commit comments