private void InsertHeadersOutbound(SoapMessage message, bool isclient) { MessageProcessor mp = MessageProcessor.Instance; if (MessageProcessor.GetConfig.DependencyInjectionEnabled) { string thread = ""; string thismessageid = ""; if (localhashcode != -1) { MessageCorrelator mc = AgentMessageTable.GetRequestbyHash(localhashcode); if (mc != null) { mc.threadid = MessageProcessor.GetTransactionThreadId(Thread.CurrentContext.ContextID.ToString() + Thread.CurrentThread.ManagedThreadId.ToString() + ":" + Thread.GetDomainID().ToString() + Thread.CurrentThread.Name); if (String.IsNullOrEmpty(mc.threadid)) { mc.threadid = Guid.NewGuid().ToString(); } thread = mc.threadid; thismessageid = mc.MessageID; message.Headers.Add(new FGSMSSoapHeaderRelatedMessageASPNET(thismessageid)); message.Headers.Add(new FGSMSSoapHeaderTransactionThreadIdASPNET(thread)); } } } }
private void GrabClientResponseMessageBeforeDeserialization(SoapMessage message) { AgentMessageTable ctx2 = AgentMessageTable.Instance; AgentMessageTable hashtable = AgentMessageTable.Instance; if (localhashcode == -1) { return; } MessageCorrelator mc = AgentMessageTable.GetRequestbyHash(localhashcode); if (mc != null) { try { mc.ResponseMessage = StreamtoString(message.Stream); if (mc.ResponseHeaders == null) { mc.ResponseHeaders = new NameValueCollection(); } mc.ResponseHeaders.Add("Content-Encoding", message.ContentEncoding); mc.ResponseHeaders.Add("Content-Type", message.ContentType); } catch (Exception ex) { Logger.debug(ex); } } }
public static MessageCorrelator GetRequestbyHash(int hash) { MessageCorrelator ret = (MessageCorrelator)messageTable[hash]; if (ret == null) { return(null); } return(ret); }
public static void AddRequest(int hash, DateTime time, string message, string url, NameValueCollection headers) { MessageCorrelator x = new MessageCorrelator(); x.RequestMessage = message; x.RecievedAt = time; x.originalUrl = x.URL = url; x.MessageID = Guid.NewGuid().ToString(); x.identity = new List <string>(); messageTable.Add(hash, x); }
/* * public static string GetRequestMessage(int hash) * { * MessageCorrelator ret = (MessageCorrelator)messageTable[hash]; * return ret.; * } * public static string GetRequestURL(int hash) * { * MessageTimeKeeper ret = (MessageTimeKeeper)messageTable[hash]; * return ret.url; * }*/ public static MessageCorrelator RemoveMessage(int hash) { try { MessageCorrelator c = (MessageCorrelator)messageTable[hash]; messageTable.Remove(hash); return(c); } catch { return(null); } }
public void WhenMessageIsAdded_TryLookupReturnsTrue() { MessageCorrelator correlator = new MessageCorrelator(); var telemetry = new DependencyTelemetry(); var id = new UniqueId(); correlator.Add(id, telemetry, TimeSpan.FromMilliseconds(100)); DependencyTelemetry result; Assert.IsTrue(correlator.TryLookup(id, out result)); Assert.AreSame(telemetry, result); }
public void WhenAddIsCalledWithNullTelemetry_ArgumentNullExceptionIsThrown() { MessageCorrelator correlator = new MessageCorrelator(); bool failed = false; try { correlator.Add(new UniqueId(), null, TimeSpan.FromMilliseconds(100)); } catch (ArgumentNullException) { failed = true; } Assert.IsTrue(failed, "ArgumentNullException was not thrown"); }
public void WhenDisposed_AddThrowsException() { MessageCorrelator correlator = new MessageCorrelator(); correlator.Dispose(); var id = new UniqueId(); bool failed = false; try { correlator.Add(id, new DependencyTelemetry(), TimeSpan.FromMilliseconds(100)); } catch (ObjectDisposedException) { failed = true; } Assert.IsTrue(failed, "Add did not throw ObjectDisposedException"); }
} // end Instance public static List <int> GetOldMessages(DateTime cutoff) { //if now is 01:10pm, cutoff is 01:00pm List <int> l = new List <int>(); lock (messageTable) { IDictionaryEnumerator it = messageTable.GetEnumerator(); while (it.MoveNext()) { MessageCorrelator ret = (MessageCorrelator)it.Entry.Value; if (ret.RecievedAt < cutoff) { l.Add((int)it.Key); } } } return(l); }
private void PurgeOldMessages() { AgentMessageTable x = AgentMessageTable.Instance; //Timeout threshold DateTime cutoff = DateTime.Now.Subtract(new TimeSpan(0, 0, 0, 0, (int)MessageProcessor.GetConfig.DeadMessageDuration)); List <int> l = AgentMessageTable.GetOldMessages(cutoff); if (l.Count > 0) { Logger.warn("ASP.NET Agent, Purging " + l.Count + " stale records from the message table. These were most likely time outs and will be logged as faults."); } for (int i = 0; i < l.Count; i++) { MessageCorrelator mc = AgentMessageTable.RemoveMessage(localhashcode); mc.CompletedAt = DateTime.Now; if (String.IsNullOrEmpty(mc.soapAction)) { mc.soapAction = "urn:undeterminable"; } mc.ResponseMessage = "Request Timeout"; mc.IsFault = true; mc.agenttype = "org.miloss.fgsms.agents.AgentSoapExtension.DeadMessageQueue"; MessageProcessor.ProcessMessage(mc); /* * MessageProcessor.ProcessMessage((AgentMessageTable.GetRequestURL(l[i])), * AgentMessageTable.GetRequestHash(l[i]), * DateTime.Now, * "urn:undeterminable", * AgentMessageTable.GetRequestMessage(l[i]), * "Request Timeout", * localhashcode.ToString(), true, * HttpContext.Current, * "", "org.miloss.fgsms.agents.AgentSoapExtension.DeadMessageQueue", * "" * );*/ } }
public void WhenMessageIsAdded_AndNotRemoved_TimeoutCallbackIsFired() { UniqueId timeoutId = null; DependencyTelemetry timeoutTelemetry = null; ManualResetEvent timeoutEvent = new ManualResetEvent(false); MessageCorrelator correlator = new MessageCorrelator( (messageId, dependencyObj) => { timeoutId = messageId; timeoutTelemetry = dependencyObj; timeoutEvent.Set(); }); var telemetry = new DependencyTelemetry(); var id = new UniqueId(); correlator.Add(id, telemetry, TimeSpan.FromMilliseconds(100)); Assert.IsTrue(timeoutEvent.WaitOne(200)); Assert.AreEqual(id, timeoutId); Assert.AreEqual(telemetry, timeoutTelemetry); }
public void WhenMessageIsAdded_AndRemoved_TimeoutCallbackIsNotFired() { ManualResetEvent timeoutEvent = new ManualResetEvent(false); MessageCorrelator correlator = new MessageCorrelator( (messageId, dependencyObj) => { timeoutEvent.Set(); }); var telemetry = new DependencyTelemetry(); // add and remove right away var id = new UniqueId(); correlator.Add(id, telemetry, TimeSpan.FromMilliseconds(100)); DependencyTelemetry result; Assert.IsTrue(correlator.TryLookup(id, out result)); // should timeout Assert.IsFalse(timeoutEvent.WaitOne(200)); }
public static void AddRequest(MessageCorrelator mc, int hash) { messageTable.Add(hash, mc); }
public override void Write(byte[] buffer, int offset, int count) { if (HttpContext.Current == null) { Logger.warn(this.GetType().FullName + " SniiferFilter, http context is null, calling assembly must be outside of IIS"); } else { string strBuffer = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count); responseHtml.Append(strBuffer); string response = responseHtml.ToString(); try { int k = System.Web.HttpContext.Current.Request.GetHashCode(); MessageCorrelator mc = AgentMessageTable.GetRequestbyHash(k); //DateTime start = AgentMessageTable.GetRequestHash(k); //if (start == null || start == DateTime.MinValue) if (mc == null) { Logger.warn(this.GetType().FullName + " SniiferFilter unable to find corresponding request message to this response, it will be ignored."); } else { if (String.IsNullOrEmpty(mc.soapAction)) { string action = "urn:undeterminable"; action = System.Web.HttpContext.Current.Request.Headers["SOAPAction"]; if (!String.IsNullOrEmpty(action)) { action = action.Replace("\"", ""); action = action.Replace("'", ""); if (String.IsNullOrEmpty(action)) { action = "urn:undeterminable"; } } if (action == "urn:undeterminable") { action = System.Web.HttpContext.Current.Request.HttpMethod; action = action.Replace("\"", ""); action = action.Replace("'", ""); if (String.IsNullOrEmpty(action)) { action = "urn:undeterminable"; } } } //string user = null; if (mc.identity == null) { mc.identity = new List <string>(); } if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated) { mc.identity.Add(System.Web.HttpContext.Current.User.Identity.Name); } mc.CompletedAt = DateTime.Now; mc.ResponseMessage = response; mc.responsesize = mc.ResponseMessage.Length; mc.identity.Add(mc.ipaddress); mc.agenttype = "org.miloss.fgsms.agents.AgentSoapExtension"; mc.IsFault = AgentSoapExtension.IsFault(response); try { //mc.ResponseHeaders = HttpContext.Current.Response. try { mc.ResponseHeaders = HttpContext.Current.Response.Headers; } catch { mc.ResponseHeaders = new NameValueCollection(); mc.ResponseHeaders.Add("charset", HttpContext.Current.Response.Charset); mc.ResponseHeaders.Add("ContentEncoding", HttpContext.Current.Response.ContentEncoding.WebName); mc.ResponseHeaders.Add("ContentType", HttpContext.Current.Response.ContentType); mc.ResponseHeaders.Add("soapAction", mc.soapAction); } mc.ipaddress = HttpContext.Current.Request.UserHostAddress; if (HttpContext.Current.Request.ClientCertificate != null) { mc.ClientCertificate = new X509Certificate2(HttpContext.Current.Request.ClientCertificate.Certificate); } } catch (Exception ex) { Logger.warn(ex, this.GetType().FullName + " SniiferFilter configuration error for " + mc.URL + " you'll have to enable IIS Intergrated Pipeline Mode when running IIS 7+. Items such as headers, remote requestor ips and certificate info will not be recorded"); } MessageProcessor.ProcessMessage(mc); /* * MessageProcessor.ProcessMessage(System.Web.HttpContext.Current.Request.Url.ToString(), * start, * DateTime.Now, * action, * AgentMessageTable.GetRequestMessage(System.Web.HttpContext.Current.Request.GetHashCode()), * response, * System.Web.HttpContext.Current.Request.GetHashCode().ToString(), * AgentSoapExtension.IsFault(response), * System.Web.HttpContext.Current, * System.Web.HttpContext.Current.Request.UserHostAddress, "org.miloss.fgsms.agents.AgentSoapExtension", * user * );*/ AgentMessageTable.RemoveMessage(System.Web.HttpContext.Current.Request.GetHashCode()); Logger.debug(this.GetType().FullName + "SniiferFilter success to " + System.Web.HttpContext.Current.Request.Url.ToString()); } } catch (Exception ex) { Logger.error(ex, "Error aquiring data from asp.net sniffer filter agent. please report."); AgentMessageTable.RemoveMessage(System.Web.HttpContext.Current.Request.GetHashCode()); } //byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(response); } responseStream.Write(buffer, offset, count); }
private void ProcessResponse(SoapMessage message, Boolean isclient) { MessageProcessor ctx = MessageProcessor.Instance; // if (!MessageProcessor.Enabled) // return; if (Trace.CorrelationManager.ActivityId == Guid.Empty) { Guid newGuid = Guid.NewGuid(); Trace.CorrelationManager.ActivityId = newGuid; } try { AgentMessageTable ctx2 = AgentMessageTable.Instance; AgentMessageTable hashtable = AgentMessageTable.Instance; if (localhashcode == -1) { Logger.warn(this.GetType().FullName + " ProcessResponse unable to local hash reference coresponding request message"); return; } if (isclient && HttpContext.Current == null) //if this is a standalone client processing a response, this is common { MessageCorrelator mc = AgentMessageTable.RemoveMessage(localhashcode); if (mc == null) { Logger.warn( this.GetType().FullName + " ProcessResponse unable to reference coresponding request message"); return; } if (isclient && MessageProcessor.GetConfig.DependencyInjectionEnabled) { IEnumerator it = message.Headers.GetEnumerator(); while (it.MoveNext()) { SoapUnknownHeader e = it.Current as SoapUnknownHeader; if (e != null) { //if (e.Element.Name.Equals(FGSMSSoapHeaderTransactionThreadIdWCF.Name2) && e.Element.NamespaceURI.Equals(FGSMSSoapHeaderTransactionThreadIdWCF.Namespace2)) //{ // mc.threadid = e.Element.InnerText; //} if (e.Element.Name.Equals(FGSMSSoapHeaderRelatedMessageIdWCF.Name2) && e.Element.NamespaceURI.Equals(FGSMSSoapHeaderRelatedMessageIdWCF.Namespace2)) { mc.relatedtransactionid = e.Element.InnerText; } } } } if (String.IsNullOrEmpty(mc.threadid)) { mc.threadid = MessageProcessor.GetTransactionThreadId(Thread.CurrentContext.ContextID.ToString() + Thread.CurrentThread.ManagedThreadId.ToString() + ":" + Thread.GetDomainID().ToString() + Thread.CurrentThread.Name); if (String.IsNullOrEmpty(mc.threadid)) { mc.threadid = Guid.NewGuid().ToString(); } } if (!isclient && MessageProcessor.GetConfig.DependencyInjectionEnabled) { message.Headers.Add(new FGSMSSoapHeaderRelatedMessageASPNET(mc.MessageID)); message.Headers.Add(new FGSMSSoapHeaderTransactionThreadIdASPNET(mc.threadid)); } try { if (String.IsNullOrEmpty(mc.soapAction)) { string action = "urn:undeterminable"; if (action == "urn:undeterminable") { action = message.Action; action = action.Replace("\"", ""); action = action.Replace("'", ""); if (String.IsNullOrEmpty(action)) { action = "urn:undeterminable"; } } if (action == "urn:undeterminable") { action = message.MethodInfo.Name;; action = action.Replace("\"", ""); action = action.Replace("'", ""); if (String.IsNullOrEmpty(action)) { action = "urn:undeterminable"; } } } string ip = ""; try { string myHost = System.Net.Dns.GetHostName(); System.Net.IPAddress[] list = System.Net.Dns.GetHostEntry(myHost).AddressList; for (int i = 0; i < list.Length; i++) { if (!IPAddress.IsLoopback(System.Net.Dns.GetHostEntry(myHost).AddressList[i])) { ip = System.Net.Dns.GetHostEntry(myHost).AddressList[i].ToString(); break; } } } catch { } string user = System.Environment.UserName; mc.identity = new List <string>(); mc.identity.Add(user); // mc.identity.Add(ip); mc.ipaddress = ip; mc.CompletedAt = DateTime.Now; mc.IsFault = AgentSoapExtension.IsFault(message); mc.agenttype = this.GetType().FullName + ".client"; mc.memo = "MsgMap=" + AgentMessageTable.GetSize(); if (mc.ResponseHeaders == null) { mc.ResponseHeaders = new NameValueCollection(); } if (HttpContext.Current != null && HttpContext.Current.Response != null && HttpContext.Current.Response.Headers != null) { mc.ResponseHeaders.Add(HttpContext.Current.Response.Headers); } else { mc.ResponseHeaders.Add("SOAPAction", message.Action); } if (message.OneWay) { mc.ResponseMessage = ""; mc.responsesize = 0; MessageProcessor.ProcessMessage(mc); /* * mc.ipaddress = ip; * MessageProcessor.ProcessMessage(( * url), * start, * DateTime.Now, * action, * req, * "", * localhashcode.ToString(), * AgentSoapExtension.IsFault(message), * null, * ip, this.GetType().FullName + ".client", * user * );*/ } else { mc.responsesize = mc.ResponseMessage.Length; MessageProcessor.ProcessMessage(mc); /* * MessageProcessor.ProcessMessage((url), * start, * DateTime.Now, * action, * req, * //InputMessageToString(message), * StreamtoString(message.Stream), * localhashcode.ToString(), * AgentSoapExtension.IsFault(message), * null, * ip, this.GetType().FullName + ".client", * user * );*/ } } catch (Exception ex) { try { AgentMessageTable.RemoveMessage(localhashcode); } catch { } Logger.error(ex, this.GetType().FullName + " Error caught process response for clients"); } } else { //this is a response from a service, either directly from the service, or a service chaining event and the caller is processing the response try { if (HttpContext.Current == null) { Logger.warn(this.GetType().FullName + " SniiferFilter failure, http context is null, this shouldn't happen please report if you see this message"); } else { // HttpContext.Current.Response.Filter = new SniiferFilter(HttpContext.Current.Response.Filter); HttpContext.Current.Response.Filter = new SniiferFilter(HttpContext.Current.Response.Filter); } // if (FGSMSConstants.log) EventLog.WriteEntry(this.GetType().FullName, "SniiferFilter success to " + System.Web.HttpContext.Current.Request.Url.ToString(), EventLogEntryType.Information); } catch { } } //this may be a good hook for dependency checking //HttpContext.Current.Items.Add( //need to make a container, add it to the context somehow //on each outbound message, grab the context container, add dependency if detected. //on the first inbound connection, setup the context //on an outbound connection, check if it's a request or response //if response, we are done, send dependency tree to the message processor //if request, get the context, add the dependency try { //if (FGSMSConstants.log) EventLog.WriteEntry(this.GetType().FullName, "ProcessOutbound success", EventLogEntryType.Information); } catch { } } catch (Exception ex) { Logger.error(ex, "Process outbound"); } if (!isclient) { try { MessageProcessor.ClearTransactionThreadId(Thread.CurrentContext.ContextID.ToString() + Thread.CurrentThread.ManagedThreadId.ToString() + ":" + Thread.GetDomainID().ToString() + Thread.CurrentThread.Name); } catch { } } else { try { MessageProcessor.ClearTransactionThreadId(Thread.CurrentContext.ContextID.ToString() + Thread.CurrentThread.ManagedThreadId.ToString() + ":" + Thread.GetDomainID().ToString() + Thread.CurrentThread.Name); } catch { } } //PurgeOldMessages(); }
private void ProcessRequest(SoapMessage message, Boolean isclient) { if (Trace.CorrelationManager.ActivityId == Guid.Empty) { Guid newGuid = Guid.NewGuid(); Trace.CorrelationManager.ActivityId = newGuid; } MessageProcessor ctx = MessageProcessor.Instance; MessageCorrelator mc = new MessageCorrelator(); mc.MessageID = Guid.NewGuid().ToString(); if (isclient) { mc.threadid = MessageProcessor.GetTransactionThreadId(Thread.CurrentContext.ContextID.ToString() + Thread.CurrentThread.ManagedThreadId.ToString() + ":" + Thread.GetDomainID().ToString() + Thread.CurrentThread.Name); if (String.IsNullOrEmpty(mc.threadid)) { mc.threadid = Guid.NewGuid().ToString(); } if (MessageProcessor.GetConfig.DependencyInjectionEnabled) { message.Headers.Add(new FGSMSSoapHeaderRelatedMessageASPNET(mc.MessageID)); message.Headers.Add(new FGSMSSoapHeaderTransactionThreadIdASPNET(mc.threadid)); } } else //service processing a request { IEnumerator it = message.Headers.GetEnumerator(); while (it.MoveNext()) { SoapUnknownHeader e = it.Current as SoapUnknownHeader; if (e != null) { //if (e.Element.Name.Equals(FGSMSSoapHeaderTransactionThreadIdWCF.Name2) && e.Element.NamespaceURI.Equals(FGSMSSoapHeaderTransactionThreadIdWCF.Namespace2)) //{ // mc.threadid = e.Element.InnerText; //} if (e.Element.Name.Equals(FGSMSSoapHeaderRelatedMessageIdWCF.Name2) && e.Element.NamespaceURI.Equals(FGSMSSoapHeaderRelatedMessageIdWCF.Namespace2)) { mc.relatedtransactionid = e.Element.InnerText; } if (e.Element.Name.Equals(FGSMSSoapHeaderTransactionThreadIdWCF.Name2) && e.Element.NamespaceURI.Equals(FGSMSSoapHeaderTransactionThreadIdWCF.Namespace2)) { mc.threadid = e.Element.InnerText; } } } } if (String.IsNullOrEmpty(mc.threadid)) { mc.threadid = Guid.NewGuid().ToString(); } MessageProcessor.SetTransactionThreadId(Thread.CurrentContext.ContextID.ToString() + Thread.CurrentThread.ManagedThreadId.ToString() + ":" + Thread.GetDomainID().ToString() + Thread.CurrentThread.Name, mc.threadid); try { Uri url = new Uri("urn:undeterminable"); if (!isclient && System.Web.HttpContext.Current != null) { url = System.Web.HttpContext.Current.Request.Url; } else { url = new Uri(message.Url); } AgentMessageTable hashtable = AgentMessageTable.Instance; if (isclient) { localhashcode = message.GetHashCode(); } else if (HttpContext.Current != null) { localhashcode = HttpContext.Current.Request.GetHashCode(); } else { //uh oh, http context is null and this is a server side request, unexpected. } if (HttpContext.Current == null) { mc.RecievedAt = DateTime.Now; mc.RequestMessage = StreamtoString(message.Stream); mc.requestsize = mc.RequestMessage.Length; mc.originalUrl = mc.URL = url.ToString(); mc.RequestHeaders = new System.Collections.Specialized.NameValueCollection(); // mc.RequestHeaders.Add("Content-Type", message.ContentType); // mc.RequestHeaders.Add("Content-Encoding", message.ContentEncoding); mc.RequestHeaders.Add("SOAPAction", message.Action); mc.soapAction = message.Action; AgentMessageTable.AddRequest(mc, localhashcode); /* AgentMessageTable.AddRequest(localhashcode, * DateTime.Now, * StreamtoString(message.Stream), * //InputMessageToString(message), * url.ToString() * // message.ToString() * );*/ } else { mc.RecievedAt = DateTime.Now; mc.RequestMessage = StreamtoString(message.Stream); mc.requestsize = mc.RequestMessage.Length; mc.originalUrl = mc.URL = url.ToString(); mc.RequestHeaders = new System.Collections.Specialized.NameValueCollection(); mc.RequestHeaders = HttpContext.Current.Request.Headers; mc.userp = HttpContext.Current.User; try { mc.soapAction = message.Action; } catch { } if (String.IsNullOrEmpty(mc.soapAction)) { mc.soapAction = mc.RequestHeaders["SOAPAction"]; } if (HttpContext.Current.Request.ClientCertificate != null) { mc.ClientCertificate = new X509Certificate2(HttpContext.Current.Request.ClientCertificate.Certificate); } AgentMessageTable.AddRequest(mc, localhashcode); /* * AgentMessageTable.AddRequest(localhashcode, * DateTime.Now, * StreamtoString(message.Stream), * //OutputMessageToString(message), * url.ToString()); * // message.ToString()*/ } } catch (Exception ex) { Logger.error(ex, this.GetType().FullName + " error caught processing a request "); } // PurgeOldMessages(); }