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

Commit 8d7986e

Browse files
committed
issue 45
1 parent 7fd744b commit 8d7986e

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

+34-13
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,8 @@ public void SetResponseBodyString(string body)
384384
/// Before request is made to server
385385
/// Respond with the specified HTML string to client
386386
/// and ignore the request
387-
/// Marking as obsolete, need to comeup with a generic responder method in future
388387
/// </summary>
389388
/// <param name="html"></param>
390-
// [Obsolete]
391389
public void Ok(string html)
392390
{
393391
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)
397395

398396
var result = Encoding.Default.GetBytes(html);
399397

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";
407414

408-
//connectStreamWriter.WriteLine(ProxySession.Request.IsAlive ? "Connection: Keep-Alive" : "Connection: close");
415+
response.ResponseHeaders.Add(new HttpHeader("Timestamp", DateTime.Now.ToString()));
409416

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"));
412421

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);
414428

415429
ProxySession.Request.CancelRequest = true;
416430
}
431+
432+
/// a generic responder method
433+
public void Respond(Response response)
434+
{
435+
ProxySession.Response = response;
436+
ProxyServer.HandleHttpSessionResponse(this);
437+
}
417438
}
418439
}

Titanium.Web.Proxy/Network/HttpWebClient.cs

+3
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@ public void SendRequest()
312312

313313
public void ReceiveResponse()
314314
{
315+
//return if this is already read
316+
if (this.Response.ResponseStatusCode != null) return;
317+
315318
var httpResult = ProxyClient.ServerStreamReader.ReadLine().Split(new char[] { ' ' }, 3);
316319

317320
if (string.IsNullOrEmpty(httpResult[0]))

Titanium.Web.Proxy/ResponseHandler.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ namespace Titanium.Web.Proxy
1818
partial class ProxyServer
1919
{
2020
//Called asynchronously when a request was successfully and we received the response
21-
private static void HandleHttpSessionResponse(SessionEventArgs args)
21+
public static void HandleHttpSessionResponse(SessionEventArgs args)
2222
{
2323
args.ProxySession.ReceiveResponse();
2424

2525
try
2626
{
27+
if (!args.ProxySession.Response.ResponseBodyRead)
28+
args.ProxySession.Response.ResponseStream = args.ProxySession.ProxyClient.ServerStreamReader.BaseStream;
2729

28-
args.ProxySession.Response.ResponseStream = args.ProxySession.ProxyClient.ServerStreamReader.BaseStream;
2930

30-
31-
if (BeforeResponse != null)
31+
if (BeforeResponse != null && !args.ProxySession.Response.ResponseLocked)
3232
{
3333
BeforeResponse(null, args);
3434
}

0 commit comments

Comments
 (0)