22using  System . Collections . Generic ; 
33using  System . Net ; 
44using  System . Text . RegularExpressions ; 
5+ using  System . Threading . Tasks ; 
56using  Titanium . Web . Proxy . EventArguments ; 
67using  Titanium . Web . Proxy . Models ; 
78
@@ -13,7 +14,7 @@ public void StartProxy()
1314        { 
1415            ProxyServer . BeforeRequest  +=  OnRequest ; 
1516            ProxyServer . BeforeResponse  +=  OnResponse ; 
16-             ProxyServer . RemoteCertificateValidationCallback  +=  OnCertificateValidation ; 
17+             ProxyServer . ServerCertificateValidationCallback  +=  OnCertificateValidation ; 
1718
1819            //Exclude Https addresses you don't want to proxy 
1920            //Usefull for clients that use certificate pinning 
@@ -61,9 +62,8 @@ public void Stop()
6162            ProxyServer . Stop ( ) ; 
6263        } 
6364
64-         //Test On Request, intecept requests 
65-         //Read browser URL send back to proxy by the injection script in OnResponse event 
66-         public  void  OnRequest ( object  sender ,  SessionEventArgs  e ) 
65+         //intecept & cancel, redirect or update requests 
66+         public  async  Task  OnRequest ( object  sender ,  SessionEventArgs  e ) 
6767        { 
6868            Console . WriteLine ( e . WebSession . Request . Url ) ; 
6969
@@ -73,39 +73,37 @@ public void OnRequest(object sender, SessionEventArgs e)
7373            if  ( ( e . WebSession . Request . Method . ToUpper ( )  ==  "POST"  ||  e . WebSession . Request . Method . ToUpper ( )  ==  "PUT" ) ) 
7474            { 
7575                //Get/Set request body bytes 
76-                 byte [ ]  bodyBytes  =  e . GetRequestBody ( ) ; 
77-                 e . SetRequestBody ( bodyBytes ) ; 
76+                 byte [ ]  bodyBytes  =  await   e . GetRequestBody ( ) ; 
77+                 await   e . SetRequestBody ( bodyBytes ) ; 
7878
7979                //Get/Set request body as string 
80-                 string  bodyString  =  e . GetRequestBodyAsString ( ) ; 
81-                 e . SetRequestBodyString ( bodyString ) ; 
80+                 string  bodyString  =  await   e . GetRequestBodyAsString ( ) ; 
81+                 await   e . SetRequestBodyString ( bodyString ) ; 
8282
8383            } 
8484
8585            //To cancel a request with a custom HTML content 
8686            //Filter URL 
8787            if  ( e . WebSession . Request . RequestUri . AbsoluteUri . Contains ( "google.com" ) ) 
8888            { 
89-                 e . Ok ( "<!DOCTYPE html>"  + 
90-                      "<html><body><h1>"  + 
91-                      "Website Blocked"  + 
92-                      "</h1>"  + 
93-                      "<p>Blocked by titanium web proxy.</p>"  + 
94-                      "</body>"  + 
95-                      "</html>" ) ; 
89+                 await   e . Ok ( "<!DOCTYPE html>"  + 
90+                        "<html><body><h1>"  + 
91+                        "Website Blocked"  + 
92+                        "</h1>"  + 
93+                        "<p>Blocked by titanium web proxy.</p>"  + 
94+                        "</body>"  + 
95+                        "</html>" ) ; 
9696            } 
9797            //Redirect example 
9898            if  ( e . WebSession . Request . RequestUri . AbsoluteUri . Contains ( "wikipedia.org" ) ) 
9999            { 
100-                 e . Redirect ( "https://www.paypal.com" ) ; 
100+                 await   e . Redirect ( "https://www.paypal.com" ) ; 
101101            } 
102102        } 
103103
104-         //Test script injection 
105-         //Insert script to read the Browser URL and send it back to proxy 
106-         public  void  OnResponse ( object  sender ,  SessionEventArgs  e ) 
104+         //Modify response 
105+         public  async  Task  OnResponse ( object  sender ,  SessionEventArgs  e ) 
107106        { 
108- 
109107            //read response headers 
110108            var  responseHeaders  =  e . WebSession . Response . ResponseHeaders ; 
111109
@@ -116,11 +114,11 @@ public void OnResponse(object sender, SessionEventArgs e)
116114                { 
117115                    if  ( e . WebSession . Response . ContentType . Trim ( ) . ToLower ( ) . Contains ( "text/html" ) ) 
118116                    { 
119-                         byte [ ]  bodyBytes  =  e . GetResponseBody ( ) ; 
120-                         e . SetResponseBody ( bodyBytes ) ; 
117+                         byte [ ]  bodyBytes  =  await   e . GetResponseBody ( ) ; 
118+                         await   e . SetResponseBody ( bodyBytes ) ; 
121119
122-                         string  body  =  e . GetResponseBodyAsString ( ) ; 
123-                         e . SetResponseBodyString ( body ) ; 
120+                         string  body  =  await   e . GetResponseBodyAsString ( ) ; 
121+                         await   e . SetResponseBodyString ( body ) ; 
124122                    } 
125123                } 
126124            } 
@@ -131,13 +129,13 @@ public void OnResponse(object sender, SessionEventArgs e)
131129        /// </summary> 
132130        /// <param name="sender"></param> 
133131        /// <param name="e"></param> 
134-         public  void  OnCertificateValidation ( object  sender ,  CertificateValidationEventArgs  e ) 
132+         public  async   Task  OnCertificateValidation ( object  sender ,  CertificateValidationEventArgs  e ) 
135133        { 
136134            //set IsValid to true/false based on Certificate Errors 
137135            if  ( e . SslPolicyErrors  ==  System . Net . Security . SslPolicyErrors . None ) 
138136                e . IsValid  =  true ; 
139137            else 
140-                 e . Session . Ok ( "Cannot validate server certificate! Not safe to proceed." ) ; 
138+                 await   e . Session . Ok ( "Cannot validate server certificate! Not safe to proceed." ) ; 
141139        } 
142140    } 
143141} 
0 commit comments