Commit 8d7986ec authored by titanium007's avatar titanium007

issue 45

parent 7fd744bb
......@@ -384,10 +384,8 @@ namespace Titanium.Web.Proxy.EventArguments
/// 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.");
......@@ -397,22 +395,45 @@ namespace Titanium.Web.Proxy.EventArguments
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);
}
}
}
\ No newline at end of file
......@@ -312,6 +312,9 @@ namespace Titanium.Web.Proxy.Network
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]))
......
......@@ -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);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment