Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1125 → Rev 1124

/TCPproxy/trunk/MainForm.cs
11,26 → 11,14
// FIXME:
// - add soap validation
// - icons for all items in the tree
// - update text view, not override whole text
// - use pool of threads?
// - do not store intermediate info, just row packets and the parsed fragments to display
// - make the text fragment store switchable
// - save/restore window layout
namespace TCPproxy
{
public class MainForm : System.Windows.Forms.Form
{
#region private fields
 
private TcpListener tcpListener = null;
private LogMessages logMessages = null;
private Hashtable treeNodes = new Hashtable();
 
private TcpShowMode tcpShowMode = TcpShowMode.ByDirection;
private bool autoExpand = true;
 
#endregion private fields
 
#region web forms fields
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button startButton;
72,6 → 60,20
private System.Windows.Forms.ImageList treeImageList;
private System.Windows.Forms.MenuItem wordWrapMenuItem;
private System.Windows.Forms.TextBox resendHostBox;
#region private fields
 
private TcpListener tcpListener = null;
private LogMessages logMessages = null;
private Hashtable treeNodes = new Hashtable();
 
private TcpShowMode tcpShowMode = TcpShowMode.ByDirection;
private bool autoExpand = true;
 
#endregion private fields
 
#region web forms fields
 
 
#endregion web forms fields
 
#region Windows Form Designer generated code
519,7 → 521,7
private void LoadFromRegistry()
{
Microsoft.Win32.RegistryKey subkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
@"Software\Anatoli Klassen\TCPproxy");
@"Software\Echelon\TCPproxy");
 
if(subkey != null)
{
533,11 → 535,6
object tcpShowModeStr = (object)subkey.GetValue("Tcp Show Mode", TcpShowMode.ByDirection);
tcpShowMode = (tcpShowModeStr as string) == "ByDirection"
? TcpShowMode.ByDirection : TcpShowMode.ByTime;
 
// this.Top = (int)subkey.GetValue("Window.Top", this.Top);
// this.Left = (int)subkey.GetValue("Window.Left", this.Left);
// this.Hight = (int)subkey.GetValue("Window.Hight", this.Hight);
// this.Width = (int)subkey.GetValue("Window.Width", this.Width);
}
}
 
544,7 → 541,7
private void SaveToRegistry()
{
Microsoft.Win32.RegistryKey subkey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(
@"Software\Anatoli Klassen\TCPproxy");
@"Software\Echelon\TCPproxy");
 
subkey.SetValue("Listen Port", listenPortBox.Text);
subkey.SetValue("Resend Host", resendHostBox.Text);
553,11 → 550,6
subkey.SetValue("Tcp Show Mode", tcpShowMode);
subkey.SetValue("Auto Expand", autoExpand ? 1 : 0);
subkey.SetValue("Word Wrap", messagesBox.WordWrap ? 1 : 0);
 
subkey.SetValue("Window.Top", this.Top);
subkey.SetValue("Window.Left", this.Left);
subkey.SetValue("Window.Hight", this.Height);
subkey.SetValue("Window.Width", this.Width);
}
 
private void startButton_Click(object sender, System.EventArgs e)
658,11 → 650,6
 
private void clearButton_Click(object sender, System.EventArgs e)
{
// close all connetions
foreach(object tcp in treeNodes.Keys)
if(tcp is TcpConnection)
((TcpConnection)tcp).Cancel();
 
treeNodes.Clear();
messageView.Nodes.Clear();
messagesBox.Clear();
904,93 → 891,78
#region network events handlers
private void TcpConnectionLog(object sender, TcpLogEventArgs e)
{
lock(this)
TcpConnection tcp = sender as TcpConnection;
LogMessage message = new LogMessage(tcp, e.Level, e.Message, e.Exception);
 
try
{
TcpConnection tcp = sender as TcpConnection;
LogMessage message = new LogMessage(tcp, e.Level, e.Message, e.Exception);
 
try
{
this.BeginInvoke(new AddLogMessageHandler(AddLogMessageInternal), new object[] { message } );
}
catch(InvalidOperationException ex)
{
if(!this.Disposing && !this.IsDisposed)
Console.WriteLine(ex.Message + " (" + ex.GetType().Name + ")\n" + ex.StackTrace);
}
this.BeginInvoke(new AddLogMessageHandler(AddLogMessageInternal), new object[] { message } );
}
catch(InvalidOperationException ex)
{
if(!this.Disposing && !this.IsDisposed)
Console.WriteLine(ex.Message + " (" + ex.GetType().Name + ")\n" + ex.StackTrace);
}
}
 
private void AddTcpConnetion(object sender, TcpConnectionEventArgs e)
{
lock(this)
e.Tcp.Log += new TcpLogEventHandler(TcpConnectionLog);
e.Tcp.Update += new TcpEventHandler(UpdateTcpNode);
e.Tcp.Close += new TcpEventHandler(UpdateTcpNode);
e.Tcp.NewHttp += new TcpHttpEventHandler(AddHttpMessageNode);
 
try
{
e.Tcp.Log += new TcpLogEventHandler(TcpConnectionLog);
e.Tcp.Update += new TcpEventHandler(UpdateTcpNode);
e.Tcp.Close += new TcpEventHandler(UpdateTcpNode);
e.Tcp.NewHttp += new TcpHttpEventHandler(AddHttpMessageNode);
 
try
{
this.BeginInvoke(new AddTcpNodeHandler(AddTcpNodeInternal), new object[] { e.Tcp } );
}
catch(InvalidOperationException ex)
{
if(!this.Disposing && !this.IsDisposed)
Console.WriteLine(ex.Message + " (" + ex.GetType().Name + ")\n" + ex.StackTrace);
}
this.BeginInvoke(new AddTcpNodeHandler(AddTcpNodeInternal), new object[] { e.Tcp } );
}
catch(InvalidOperationException ex)
{
if(!this.Disposing && !this.IsDisposed)
Console.WriteLine(ex.Message + " (" + ex.GetType().Name + ")\n" + ex.StackTrace);
}
}
 
private void UpdateTcpNode(object sender, TcpEventArgs e)
{
lock(this)
try
{
try
{
this.BeginInvoke(new UpdateTcpNodeHandler(UpdateTcpNodeInternal), new object[] { (TcpConnection)sender } );
}
catch(InvalidOperationException ex)
{
if(!this.Disposing && !this.IsDisposed)
Console.WriteLine(ex.Message + " (" + ex.GetType().Name + ")\n" + ex.StackTrace);
}
this.BeginInvoke(new UpdateTcpNodeHandler(UpdateTcpNodeInternal), new object[] { (TcpConnection)sender } );
}
catch(InvalidOperationException ex)
{
if(!this.Disposing && !this.IsDisposed)
Console.WriteLine(ex.Message + " (" + ex.GetType().Name + ")\n" + ex.StackTrace);
}
}
 
private void AddHttpMessageNode(object sender, TcpHttpEventArgs e)
{
lock(this)
e.Http.Update += new TcpEventHandler(UpdateHttpNode);
 
try
{
e.Http.Update += new TcpEventHandler(UpdateHttpNode);
 
try
{
this.BeginInvoke(new AddHttpNodeHandler(AddHttpNodeInternal),
new object[] { (TcpConnection)sender, e.Http } );
}
catch(InvalidOperationException ex)
{
if(!this.Disposing && !this.IsDisposed)
Console.WriteLine(ex.Message + " (" + ex.GetType().Name + ")\n" + ex.StackTrace);
}
this.BeginInvoke(new AddHttpNodeHandler(AddHttpNodeInternal),
new object[] { (TcpConnection)sender, e.Http } );
}
catch(InvalidOperationException ex)
{
if(!this.Disposing && !this.IsDisposed)
Console.WriteLine(ex.Message + " (" + ex.GetType().Name + ")\n" + ex.StackTrace);
}
}
 
private void UpdateHttpNode(object sender, TcpEventArgs e)
{
lock(this)
try
{
try
{
this.BeginInvoke(new UpdateHttpNodeHandler(UpdateHttpNodeInternal), new object[] { (HttpMessage)sender } );
}
catch(InvalidOperationException ex)
{
if(!this.Disposing && !this.IsDisposed)
Console.WriteLine(ex.Message + " (" + ex.GetType().Name + ")\n" + ex.StackTrace);
}
this.BeginInvoke(new UpdateHttpNodeHandler(UpdateHttpNodeInternal), new object[] { (HttpMessage)sender } );
}
catch(InvalidOperationException ex)
{
if(!this.Disposing && !this.IsDisposed)
Console.WriteLine(ex.Message + " (" + ex.GetType().Name + ")\n" + ex.StackTrace);
}
}
 
#endregion network events handlers
1031,7 → 1003,7
private void UpdateTcpNodeInternal(TcpConnection tcp)
{
TcpNodeData data = treeNodes[tcp] as TcpNodeData;
if(data == null) return; // might be call by Cancel
if(data == null) throw new ArgumentException("Unknown TCP node");
 
string title = tcp.ToString();
if(title != data.Node.Text) data.Node.Text = title;
1067,7 → 1039,7
private void UpdateHttpNodeInternal(HttpMessage http)
{
HttpNodeData httpData = treeNodes[http] as HttpNodeData;
if(httpData == null) return; // might be call by Cancel
if(httpData == null) throw new ArgumentException("No node found for HTTP message");
 
string title = http.ToString();
if(httpData.Node.Text != title) httpData.Node.Text = title;
1406,7 → 1378,6
private bool responseXmlShown = false;
 
private object stateMarker = null;
private object requestStartMarker = null;
private object requestMethodMarker = null;
private object requestUriMarker = null;
private object requestVersionMarker = null;
1415,7 → 1386,6
private object requestContentTypeMarker = null;
private object requestCharsetMarker = null;
private object requestHeadersMarker = null;
private object responseStartMarker = null;
private object responseVersionMarker = null;
private object responseStatusMarker = null;
private object responseLengthMarker = null;
1481,15 → 1451,6
owner.messagesBox.AppendNewLine();
 
// request info
owner.messagesBox.AppendText("Request Start: ",
Color.DarkRed, Color.Transparent, false, false, 0, 0);
requestStartMarker = owner.messagesBox.BeginMark();
owner.messagesBox.AppendText(http.RequestStartTimestamp == DateTime.MinValue
? "<unknown>" : http.RequestStartTimestamp.ToString("HH:mm:ss.ffff"),
Color.DarkRed, Color.LightGray, false, false, 0, 27);
owner.messagesBox.EndMark(requestStartMarker);
owner.messagesBox.AppendNewLine();
 
owner.messagesBox.AppendText("Request Method: ",
Color.DarkRed, Color.Transparent, false, false, 0, 0);
requestMethodMarker = owner.messagesBox.BeginMark();
1559,15 → 1520,6
 
// response info
owner.messagesBox.AppendNewLine();
owner.messagesBox.AppendText("Response Start: ",
Color.DarkRed, Color.Transparent, false, false, 0, 0);
responseStartMarker = owner.messagesBox.BeginMark();
owner.messagesBox.AppendText(http.ResponseStartTimestamp == DateTime.MinValue
? "<unknown>" : http.ResponseStartTimestamp.ToString("HH:mm:ss.ffff"),
Color.DarkRed, Color.LightGray, false, false, 0, 27);
owner.messagesBox.EndMark(responseStartMarker);
owner.messagesBox.AppendNewLine();
 
owner.messagesBox.AppendText("Response Version: ",
Color.DarkRed, Color.Transparent, false, false, 0, 0);
responseVersionMarker = owner.messagesBox.BeginMark();
/TCPproxy/trunk/Network.cs
1104,15 → 1104,15
private static HttpCharType[] charTypes = null;
private static bool[] tokenChars = null;
private static char[] charValues = {
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
'`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', '\0'
};
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
'`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', '\0'
};
 
private static void InitTables()
{
1357,7 → 1357,6
 
messages.AddHttpMessage(http);
SkipEmptyLines(requestPos);
http.RequestStartTimestamp = requestPos.CurrentMessage.Timestamp;
 
ParseRequestLine(requestPos, http);
http.UpdateHttpMessage();
1425,7 → 1424,6
HttpMessage http = (HttpMessage)httpEnum.Current;
 
ParseResponseLine(responsePos, http);
http.ResponseStartTimestamp = responsePos.CurrentMessage.Timestamp;
http.UpdateHttpMessage();
 
ParseHeaders(responsePos, http, false);
1931,14 → 1929,14
 
public class HttpMessage
{
private bool requestComplete = false;
private bool requestComplete = false;
private HttpVersion requestVersion;
private string requestMethod;
private string requestUri;
private LinkedList requestHeaders = new LinkedList();
private Hashtable requestHeadersHash = new Hashtable();
private int requestLength = -1; // -1 == unknown
private HttpEncoding requestEncoding = HttpEncoding.Identify;
private LinkedList requestHeaders = new LinkedList();
private Hashtable requestHeadersHash = new Hashtable();
private int requestLength = -1; // -1 == unknown
private HttpEncoding requestEncoding = HttpEncoding.Identify;
private string requestContentType;
private string requestContentSubtype;
private string requestCharset;
1946,16 → 1944,15
private byte[] requestBody;
private string requestText;
private XmlMessage requestXml;
private DateTime requestStartTimestamp = DateTime.MinValue;
 
private bool responseComplete = false;
private bool responseComplete = false;
private HttpVersion responseVersion;
private int responseStatusCode;
private string responseStatusMessage;
private LinkedList responseHeaders = new LinkedList();
private Hashtable responseHeadersHash = new Hashtable();
private int responseLength = -1; // -1 == unknown
private HttpEncoding responseEncoding = HttpEncoding.Identify;
private LinkedList responseHeaders = new LinkedList();
private Hashtable responseHeadersHash = new Hashtable();
private int responseLength = -1; // -1 == unknown
private HttpEncoding responseEncoding = HttpEncoding.Identify;
private string responseContentType;
private string responseContentSubtype;
private string responseCharset;
1962,7 → 1959,6
private byte[] responseBody;
private string responseText;
private XmlMessage responseXml;
private DateTime responseStartTimestamp = DateTime.MinValue;
 
public bool RequestComplete
{
2052,12 → 2048,6
set { requestXml = value; }
}
 
public DateTime RequestStartTimestamp
{
get { return requestStartTimestamp; }
set { requestStartTimestamp = value; }
}
 
public bool ResponseComplete
{
get { return responseComplete; }
2140,12 → 2130,6
set { responseXml = value; }
}
 
public DateTime ResponseStartTimestamp
{
get { return responseStartTimestamp; }
set { responseStartTimestamp = value; }
}
 
public void AddRequestHeader(string name, string headerValue)
{
requestHeaders.Add(new HttpHeader(name, headerValue));