@@ -384,10 +384,8 @@ public void SetResponseBodyString(string body)
384
384
/// Before request is made to server
385
385
/// Respond with the specified HTML string to client
386
386
/// and ignore the request
387
- /// Marking as obsolete, need to comeup with a generic responder method in future
388
387
/// </summary>
389
388
/// <param name="html"></param>
390
- // [Obsolete]
391
389
public void Ok ( string html )
392
390
{
393
391
if ( ProxySession . Request . RequestLocked ) throw new Exception ( "You cannot call this function after request is made to server." ) ;
@@ -397,22 +395,45 @@ public void Ok(string html)
397
395
398
396
var result = Encoding . Default . GetBytes ( html ) ;
399
397
400
- var connectStreamWriter = new StreamWriter ( this . Client . ClientStream ) ;
401
- connectStreamWriter . WriteLine ( string . Format ( "{0} {1} {2}" , ProxySession . Request . HttpVersion , 200 , "Ok" ) ) ;
402
- connectStreamWriter . WriteLine ( "Timestamp: {0}" , DateTime . Now ) ;
403
- connectStreamWriter . WriteLine ( "content-length: " + result . Length ) ;
404
- connectStreamWriter . WriteLine ( "Cache-Control: no-cache, no-store, must-revalidate" ) ;
405
- connectStreamWriter . WriteLine ( "Pragma: no-cache" ) ;
406
- connectStreamWriter . WriteLine ( "Expires: 0" ) ;
398
+ Ok ( result ) ;
399
+ }
400
+
401
+ /// <summary>
402
+ /// Before request is made to server
403
+ /// Respond with the specified byte[] to client
404
+ /// and ignore the request
405
+ /// </summary>
406
+ /// <param name="body"></param>
407
+ public void Ok ( byte [ ] result )
408
+ {
409
+ var response = new Response ( ) ;
410
+
411
+ response . HttpVersion = ProxySession . Request . HttpVersion ;
412
+ response . ResponseStatusCode = "200" ;
413
+ response . ResponseStatusDescription = "Ok" ;
407
414
408
- //connectStreamWriter.WriteLine(ProxySession.Request.IsAlive ? "Connection: Keep-Alive" : "Connection: close" );
415
+ response . ResponseHeaders . Add ( new HttpHeader ( "Timestamp" , DateTime . Now . ToString ( ) ) ) ;
409
416
410
- connectStreamWriter . WriteLine ( ) ;
411
- connectStreamWriter . Flush ( ) ;
417
+ response . ResponseHeaders . Add ( new HttpHeader ( "content-length" , DateTime . Now . ToString ( ) ) ) ;
418
+ response . ResponseHeaders . Add ( new HttpHeader ( "Cache-Control" , "no-cache, no-store, must-revalidate" ) ) ;
419
+ response . ResponseHeaders . Add ( new HttpHeader ( "Pragma" , "no-cache" ) ) ;
420
+ response . ResponseHeaders . Add ( new HttpHeader ( "Expires" , "0" ) ) ;
412
421
413
- this . Client . ClientStream . Write ( result , 0 , result . Length ) ;
422
+ ProxySession . Request . RequestLocked = true ;
423
+ response . ResponseBody = result ;
424
+ response . ResponseLocked = true ;
425
+ response . ResponseBodyRead = true ;
426
+
427
+ Respond ( response ) ;
414
428
415
429
ProxySession . Request . CancelRequest = true ;
416
430
}
431
+
432
+ /// a generic responder method
433
+ public void Respond ( Response response )
434
+ {
435
+ ProxySession . Response = response ;
436
+ ProxyServer . HandleHttpSessionResponse ( this ) ;
437
+ }
417
438
}
418
439
}
0 commit comments