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

Commit

Permalink
issue 45
Browse files Browse the repository at this point in the history
  • Loading branch information
justcoding121 committed Mar 21, 2016
1 parent 7fd744b commit 8d7986e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
47 changes: 34 additions & 13 deletions Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,8 @@ public void SetResponseBodyString(string body)
/// Before request is made to server
/// Respond with the specified HTML string to client
/// and ignore the request
/// Marking as obsolete, need to comeup with a generic responder method in future
/// </summary>
/// <param name="html"></param>
// [Obsolete]
public void Ok(string html)
{
if (ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function after request is made to server.");
Expand All @@ -397,22 +395,45 @@ public void Ok(string html)

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

var connectStreamWriter = new StreamWriter(this.Client.ClientStream);
connectStreamWriter.WriteLine(string.Format("{0} {1} {2}", ProxySession.Request.HttpVersion, 200, "Ok"));
connectStreamWriter.WriteLine("Timestamp: {0}", DateTime.Now);
connectStreamWriter.WriteLine("content-length: " + result.Length);
connectStreamWriter.WriteLine("Cache-Control: no-cache, no-store, must-revalidate");
connectStreamWriter.WriteLine("Pragma: no-cache");
connectStreamWriter.WriteLine("Expires: 0");
Ok(result);
}

/// <summary>
/// Before request is made to server
/// Respond with the specified byte[] to client
/// and ignore the request
/// </summary>
/// <param name="body"></param>
public void Ok(byte[] result)
{
var response = new Response();

response.HttpVersion = ProxySession.Request.HttpVersion;
response.ResponseStatusCode = "200";
response.ResponseStatusDescription = "Ok";

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

connectStreamWriter.WriteLine();
connectStreamWriter.Flush();
response.ResponseHeaders.Add(new HttpHeader("content-length", DateTime.Now.ToString()));
response.ResponseHeaders.Add(new HttpHeader("Cache-Control", "no-cache, no-store, must-revalidate"));
response.ResponseHeaders.Add(new HttpHeader("Pragma", "no-cache"));
response.ResponseHeaders.Add(new HttpHeader("Expires", "0"));

this.Client.ClientStream.Write(result, 0, result.Length);
ProxySession.Request.RequestLocked = true;
response.ResponseBody = result;
response.ResponseLocked = true;
response.ResponseBodyRead = true;

Respond(response);

ProxySession.Request.CancelRequest = true;
}

/// a generic responder method
public void Respond(Response response)
{
ProxySession.Response = response;
ProxyServer.HandleHttpSessionResponse(this);
}
}
}
3 changes: 3 additions & 0 deletions Titanium.Web.Proxy/Network/HttpWebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ public void SendRequest()

public void ReceiveResponse()
{
//return if this is already read
if (this.Response.ResponseStatusCode != null) return;

var httpResult = ProxyClient.ServerStreamReader.ReadLine().Split(new char[] { ' ' }, 3);

if (string.IsNullOrEmpty(httpResult[0]))
Expand Down
8 changes: 4 additions & 4 deletions Titanium.Web.Proxy/ResponseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ namespace Titanium.Web.Proxy
partial class ProxyServer
{
//Called asynchronously when a request was successfully and we received the response
private static void HandleHttpSessionResponse(SessionEventArgs args)
public static void HandleHttpSessionResponse(SessionEventArgs args)
{
args.ProxySession.ReceiveResponse();

try
{
if (!args.ProxySession.Response.ResponseBodyRead)
args.ProxySession.Response.ResponseStream = args.ProxySession.ProxyClient.ServerStreamReader.BaseStream;

args.ProxySession.Response.ResponseStream = args.ProxySession.ProxyClient.ServerStreamReader.BaseStream;


if (BeforeResponse != null)
if (BeforeResponse != null && !args.ProxySession.Response.ResponseLocked)
{
BeforeResponse(null, args);
}
Expand Down

0 comments on commit 8d7986e

Please sign in to comment.