Commit 8d7986ec authored by titanium007's avatar titanium007

issue 45

parent 7fd744bb
...@@ -384,10 +384,8 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -384,10 +384,8 @@ namespace Titanium.Web.Proxy.EventArguments
/// Before request is made to server /// Before request is made to server
/// Respond with the specified HTML string to client /// Respond with the specified HTML string to client
/// and ignore the request /// and ignore the request
/// Marking as obsolete, need to comeup with a generic responder method in future
/// </summary> /// </summary>
/// <param name="html"></param> /// <param name="html"></param>
// [Obsolete]
public void Ok(string html) public void Ok(string html)
{ {
if (ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function after request is made to server."); 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 ...@@ -397,22 +395,45 @@ namespace Titanium.Web.Proxy.EventArguments
var result = Encoding.Default.GetBytes(html); var result = Encoding.Default.GetBytes(html);
var connectStreamWriter = new StreamWriter(this.Client.ClientStream); Ok(result);
connectStreamWriter.WriteLine(string.Format("{0} {1} {2}", ProxySession.Request.HttpVersion, 200, "Ok")); }
connectStreamWriter.WriteLine("Timestamp: {0}", DateTime.Now);
connectStreamWriter.WriteLine("content-length: " + result.Length); /// <summary>
connectStreamWriter.WriteLine("Cache-Control: no-cache, no-store, must-revalidate"); /// Before request is made to server
connectStreamWriter.WriteLine("Pragma: no-cache"); /// Respond with the specified byte[] to client
connectStreamWriter.WriteLine("Expires: 0"); /// 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(); response.ResponseHeaders.Add(new HttpHeader("content-length", DateTime.Now.ToString()));
connectStreamWriter.Flush(); 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; 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 ...@@ -312,6 +312,9 @@ namespace Titanium.Web.Proxy.Network
public void ReceiveResponse() public void ReceiveResponse()
{ {
//return if this is already read
if (this.Response.ResponseStatusCode != null) return;
var httpResult = ProxyClient.ServerStreamReader.ReadLine().Split(new char[] { ' ' }, 3); var httpResult = ProxyClient.ServerStreamReader.ReadLine().Split(new char[] { ' ' }, 3);
if (string.IsNullOrEmpty(httpResult[0])) if (string.IsNullOrEmpty(httpResult[0]))
......
...@@ -18,17 +18,17 @@ namespace Titanium.Web.Proxy ...@@ -18,17 +18,17 @@ namespace Titanium.Web.Proxy
partial class ProxyServer partial class ProxyServer
{ {
//Called asynchronously when a request was successfully and we received the response //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(); args.ProxySession.ReceiveResponse();
try 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 && !args.ProxySession.Response.ResponseLocked)
if (BeforeResponse != null)
{ {
BeforeResponse(null, args); 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