private void SniffSessionSink3(HTTPSession sender, HTTPMessage msg) { if (OnSniffPacket != null) OnSniffPacket((HTTPMessage)msg.Clone()); }
private void HandleHeaderRequest(HTTPMessage msg, HTTPSession WebSession) { DText parser = new DText(); HTTPMessage Response = new HTTPMessage(); String Method = msg.Directive; String MethodData = msg.DirectiveObj; VirtualDirectoryHandler H_cb = null; VirtualDirectoryHandler P_cb = null; String vd = ""; String vdobj = ""; // Check VirtualDirTable int vdi; try { vdi = MethodData.IndexOf("/", 1); if (vdi != -1) { vdobj = MethodData.Substring(vdi); vd = MethodData.Substring(0, vdi); if (VirtualDir_Header_Table.ContainsKey(vd)) { if (VirtualDir_Header_Table[vd] != null) H_cb = (VirtualDirectoryHandler)VirtualDir_Header_Table[vd]; } if (VirtualDir_Table.ContainsKey(vd)) { if (VirtualDir_Table[vd] != null) P_cb = (VirtualDirectoryHandler)VirtualDir_Table[vd]; } } } catch (Exception ex) { OpenSource.Utilities.EventLogger.Log(ex); } if ((H_cb != null) || (P_cb != null)) { HTTPMessage _msg = (HTTPMessage)msg.Clone(); _msg.DirectiveObj = vdobj; WebSession.InternalStateObject = new Object[3] { vd, vdobj, P_cb }; if (H_cb != null) H_cb(this, _msg, WebSession, vd); return; } }
private void HandleWebRequest(HTTPMessage msg, HTTPSession WebSession) { DText parser = new DText(); HTTPMessage Response = new HTTPMessage(); HTTPMessage Response2 = null; String Method = msg.Directive; String MethodData = msg.DirectiveObj; if (WebSession.InternalStateObject != null) { HTTPMessage _msg = (HTTPMessage)msg.Clone(); object[] state = (object[])WebSession.InternalStateObject; _msg.DirectiveObj = (string)state[1]; VirtualDirectoryHandler t = (VirtualDirectoryHandler)state[2]; WebSession.InternalStateObject = null; t(this, _msg, WebSession, (string)state[0]); return; } if ((Method != "GET") && (Method != "HEAD") && (Method != "POST") && (Method != "SUBSCRIBE") && (Method != "UNSUBSCRIBE") && (Method != "NOTIFY")) { Response.StatusCode = 405; Response.StatusData = Method + " not supported"; WebSession.Send(Response); return; // Other methods are unknown to us } // Process Headers if (Method == "GET" || Method == "HEAD") { try { Response = Get(MethodData, WebSession.Source); } catch (UPnPCustomException ce) { OpenSource.Utilities.EventLogger.Log(ce); Response.StatusCode = ce.ErrorCode; Response.StatusData = ce.ErrorDescription; WebSession.Send(Response); return; } catch (Exception e) { OpenSource.Utilities.EventLogger.Log(e); Response.StatusCode = 500; Response.StatusData = "Internal"; Response.StringBuffer = e.ToString(); } if (Method == "HEAD") { Response.BodyBuffer = null; } WebSession.Send(Response); } if (Method == "POST") { //InvokerInfo[Thread.CurrentThread.GetHashCode()] = WebSession; try { Response = Post(MethodData, msg.StringBuffer, msg.GetTag("SOAPACTION"), WebSession); } catch (DelayedResponseException ex) { OpenSource.Utilities.EventLogger.Log(ex); InvokerInfo.Remove(Thread.CurrentThread.GetHashCode()); WebSession.StopReading(); return; } catch (UPnPCustomException ce) { OpenSource.Utilities.EventLogger.Log(this, System.Diagnostics.EventLogEntryType.Error, "UPnP Error [" + ce.ErrorCode.ToString() + "] " + ce.ErrorDescription); Response.StatusCode = 500; Response.StatusData = "Internal"; Response.StringBuffer = BuildErrorBody(ce); WebSession.Send(Response); InvokerInfo.Remove(Thread.CurrentThread.GetHashCode()); return; } catch (UPnPInvokeException ie) { Response.StatusCode = 500; Response.StatusData = "Internal"; if (ie.UPNP != null) { OpenSource.Utilities.EventLogger.Log(this, System.Diagnostics.EventLogEntryType.Error, "UPnP Error [" + ie.UPNP.ErrorCode.ToString() + "] " + ie.UPNP.ErrorDescription); Response.StringBuffer = BuildErrorBody(ie.UPNP); } else { OpenSource.Utilities.EventLogger.Log(this, System.Diagnostics.EventLogEntryType.Error, "UPnP Invocation Error [" + ie.MethodName + "] " + ie.Message); Response.StringBuffer = BuildErrorBody(new UPnPCustomException(500, ie.Message)); } WebSession.Send(Response); InvokerInfo.Remove(Thread.CurrentThread.GetHashCode()); return; } catch (UPnPTypeMismatchException tme) { OpenSource.Utilities.EventLogger.Log(tme); Response.StatusCode = 500; Response.StatusData = "Internal"; Response.StringBuffer = BuildErrorBody(new UPnPCustomException(402, tme.Message)); WebSession.Send(Response); InvokerInfo.Remove(Thread.CurrentThread.GetHashCode()); return; } catch (UPnPStateVariable.OutOfRangeException oor) { OpenSource.Utilities.EventLogger.Log(oor); Response.StatusCode = 500; Response.StatusData = "Internal"; Response.StringBuffer = BuildErrorBody(new UPnPCustomException(402, oor.Message)); WebSession.Send(Response); InvokerInfo.Remove(Thread.CurrentThread.GetHashCode()); return; } catch (System.Reflection.TargetInvocationException tie) { Exception inner = tie.InnerException; OpenSource.Utilities.EventLogger.Log(tie); while (inner.InnerException != null && (typeof(UPnPCustomException).IsInstanceOfType(inner) == false)) { inner = inner.InnerException; } if (typeof(UPnPCustomException).IsInstanceOfType(inner)) { UPnPCustomException ce = (UPnPCustomException)inner; OpenSource.Utilities.EventLogger.Log(this, System.Diagnostics.EventLogEntryType.Error, "UPnP Error [" + ce.ErrorCode.ToString() + "] " + ce.ErrorDescription); Response.StatusCode = 500; Response.StatusData = "Internal"; Response.StringBuffer = BuildErrorBody(ce); WebSession.Send(Response); InvokerInfo.Remove(Thread.CurrentThread.GetHashCode()); return; } else { Response.StatusCode = 500; Response.StatusData = "Internal"; Response.StringBuffer = BuildErrorBody(new UPnPCustomException(500, inner.ToString())); WebSession.Send(Response); OpenSource.Utilities.EventLogger.Log(inner); return; } } catch (Exception e) { Response.StatusCode = 500; Response.StatusData = "Internal"; Response.StringBuffer = BuildErrorBody(new UPnPCustomException(500, e.ToString())); WebSession.Send(Response); OpenSource.Utilities.EventLogger.Log(e); return; } WebSession.Send(Response); InvokerInfo.Remove(Thread.CurrentThread.GetHashCode()); return; } if (Method == "SUBSCRIBE") { String SID = msg.GetTag("SID"); String NT = msg.GetTag("NT"); String Timeout = msg.GetTag("Timeout"); String CallbackURL = msg.GetTag("Callback"); if (Timeout == "") { Timeout = "7200"; // Default = 2 Hours } else { Timeout = Timeout.Substring(Timeout.IndexOf("-") + 1).Trim().ToUpper(); if (Timeout == "INFINITE") { Timeout = "0"; } } if (SID != "") { // Renew RenewEvents(MethodData.Substring(1), SID, Timeout); } else { // Subscribe try { Response2 = SubscribeEvents(ref SID, MethodData.Substring(1), CallbackURL, Timeout); } catch (Exception s_exception) { OpenSource.Utilities.EventLogger.Log(s_exception); HTTPMessage err = new HTTPMessage(); err.StatusCode = 500; err.StatusData = s_exception.Message; WebSession.Send(err); return; } } if (Timeout == "0") { Timeout = "Second-infinite"; } else { Timeout = "Second-" + Timeout; } Response.StatusCode = 200; Response.StatusData = "OK"; Response.AddTag("Server", "Windows NT/5.0, UPnP/1.0"); Response.AddTag("SID", SID); Response.AddTag("Timeout", Timeout); WebSession.Send(Response); if (Response2 != null) { Uri[] cbURL = ParseEventURL(CallbackURL); for (int x = 0; x < cbURL.Length; ++x) { Response2.DirectiveObj = HTTPMessage.UnEscapeString(cbURL[x].PathAndQuery); Response2.AddTag("Host", cbURL[x].Host + ":" + cbURL[x].Port.ToString()); IPEndPoint d = new IPEndPoint(IPAddress.Parse(cbURL[x].Host), cbURL[x].Port); HTTPRequest R = new HTTPRequest(); R.OnResponse += new HTTPRequest.RequestHandler(HandleInitialEvent); this.InitialEventTable[R] = R; R.PipelineRequest(d, Response2, null); } } } if (Method == "UNSUBSCRIBE") { CancelEvent(MethodData.Substring(1), msg.GetTag("SID")); Response.StatusCode = 200; Response.StatusData = "OK"; WebSession.Send(Response); } }
/// <summary> /// Sends a Packet to the computer connected to this session /// </summary> /// <param name="Packet">The Packet to Send</param> public void Send(HTTPMessage Packet) { OnSniffEvent.Fire(Packet.RawPacket, 0, Packet.RawPacket.Length); OnSniffPacketEvent.Fire(this,(HTTPMessage)Packet.Clone()); if (OpenSource.Utilities.EventLogger.Enabled) { OpenSource.Utilities.EventLogger.Log(this,System.Diagnostics.EventLogEntryType.Information,Packet.StringPacket); } if (Packet.Version=="1.0") { this.IsLegacy=true; } else { this.IsLegacy=false; } lock(SendQueue) { if (SendQueue.Count==0) { if (MainSocket!=null) { MainSocket.Send(Packet.RawPacket); if (Packet.StatusCode>=200) this.SET_REQUEST_ANSWERED(); } } else { SendQueue.Enqueue(Packet); } } /* if (Packet.StatusCode>=200) { // Send Complete Event this.SET_REQUEST_ANSWERED(); } */ }