private void RequestAnsweredSink(HTTPSession ss) { lock (TagQueue) { if (!ReceivedFirstResponse) { ReceivedFirstResponse = true; IEnumerator en = TagQueue.GetEnumerator(); while (en.MoveNext()) { StateData sd = (StateData)en.Current; try { if (ProxySetting == null) { ss.Send(sd.Request); } else { HTTPMessage pr = (HTTPMessage)sd.Request.Clone(); pr.DirectiveObj = "http://" + sd.Dest.ToString() + pr.DirectiveObj; pr.Version = "1.0"; ss.Send(pr); } } catch (Exception ex) { OpenSource.Utilities.EventLogger.Log(ex); } } } } }
private void RequestAnsweredSink(HTTPSession ss) { lock (this.TagQueue) { if (!this.ReceivedFirstResponse) { this.ReceivedFirstResponse = true; foreach (StateData data in this.TagQueue) { try { if (this.ProxySetting == null) { ss.Send(data.Request); } else { HTTPMessage packet = (HTTPMessage)data.Request.Clone(); packet.DirectiveObj = "http://" + data.Dest.ToString() + packet.DirectiveObj; packet.Version = "1.0"; ss.Send(packet); } continue; } catch (Exception) { continue; } } } } }
private void SendErrorMessage(HTTPSession webSession, Stream stream) { try { if (stream != null) { webSession.CloseStreamObject(stream); stream.Dispose(); stream = null; } HTTPMessage error = new HTTPMessage(); error.StatusCode = 404; error.StatusData = "Error"; error.StringBuffer = "Error"; webSession.Send(error); } catch (Exception ex) { Utils.WriteException(ex); } finally { AllowSystemStandBy(); } }
private void CreateSink(HTTPSession ss) { lock (TagQueue) { ss.OnHeader += new HTTPSession.ReceiveHeaderHandler(HeaderSink); ss.OnReceive += new HTTPSession.ReceiveHandler(ReceiveSink); ss.OnClosed += new HTTPSession.SessionHandler(CloseSink); ss.OnStreamDone += new HTTPSession.StreamDoneHandler(StreamDoneSink); ss.OnRequestAnswered += new HTTPSession.SessionHandler(RequestAnsweredSink); if (this.OnSniff != null) { ss.OnSniff += new HTTPSession.SniffHandler(SniffSink); } if (this.OnSniffPacket != null) { ss.OnSniffPacket += new HTTPSession.ReceiveHandler(SniffPacketSink); } StateData sd = (StateData)TagQueue.Peek(); try { if (ProxySetting == null) { ss.Send(sd.Request); } else { HTTPMessage pr = (HTTPMessage)sd.Request.Clone(); pr.DirectiveObj = "http://" + sd.Dest.ToString() + pr.DirectiveObj; pr.Version = "1.0"; ss.Send(pr); } } catch (Exception exc) { OpenSource.Utilities.EventLogger.Log(exc); } } }
private void CreateSink(HTTPSession ss) { lock (TagQueue) { ss.OnHeader += HeaderSink; ss.OnReceive += ReceiveSink; ss.OnClosed += CloseSink; ss.OnStreamDone += StreamDoneSink; ss.OnRequestAnswered += RequestAnsweredSink; if (OnSniff != null) { ss.OnSniff += SniffSink; } if (OnSniffPacket != null) { ss.OnSniffPacket += SniffPacketSink; } StateData sd = (StateData)TagQueue.Peek(); try { if (ProxySetting == null) { ss.Send(sd.Request); } else { HTTPMessage pr = (HTTPMessage)sd.Request.Clone(); pr.DirectiveObj = "http://" + sd.Dest + pr.DirectiveObj; pr.Version = "1.0"; ss.Send(pr); } } catch (Exception exc) { EventLogger.Log(exc); } } }
private void CreateSink(HTTPSession ss) { lock (this.TagQueue) { ss.OnHeader += new HTTPSession.ReceiveHeaderHandler(this.HeaderSink); ss.OnReceive += new HTTPSession.ReceiveHandler(this.ReceiveSink); ss.OnClosed += new HTTPSession.SessionHandler(this.CloseSink); ss.OnStreamDone += new HTTPSession.StreamDoneHandler(this.StreamDoneSink); ss.OnRequestAnswered += new HTTPSession.SessionHandler(this.RequestAnsweredSink); if (this.OnSniff != null) { ss.OnSniff += new HTTPSession.SniffHandler(this.SniffSink); } if (this.OnSniffPacket != null) { ss.OnSniffPacket += new HTTPSession.ReceiveHandler(this.SniffPacketSink); } StateData data = (StateData)this.TagQueue.Peek(); try { if (this.ProxySetting == null) { ss.Send(data.Request); } else { HTTPMessage packet = (HTTPMessage)data.Request.Clone(); packet.DirectiveObj = "http://" + data.Dest.ToString() + packet.DirectiveObj; packet.Version = "1.0"; ss.Send(packet); } } catch (Exception exception) { EventLogger.Log(exception); } } }
private void CreateSink(HTTPSession CS) { CS.OnClosed += new HTTPSession.SessionHandler(CloseSink); SW = new HTTPSessionWatcher(CS); SW.OnSniff += new HTTPSessionWatcher.SniffHandler(SniffSink); CS.OnReceive += new HTTPSession.ReceiveHandler(ReceiveSink); if (CS.StateObject.GetType() == typeof(HTTPMessage)) { CS.Send((HTTPMessage)CS.StateObject); } else { Queue Q = (Queue)CS.StateObject; HTTPMessage M; M = (HTTPMessage)Q.Dequeue(); while (M != null && Q.Count > 0) { CS.Send(M); M = (HTTPMessage)Q.Dequeue(); } } }
public HTTPMessage HandleContent(string GetWhat, System.Net.IPEndPoint local, HTTPMessage msg, HTTPSession WebSession) { System.Diagnostics.Debug.WriteLine("HandleContent GetWhat = '" + GetWhat + "'"); HTTPMessage message = new HTTPMessage(); message.StatusCode = 200; message.StatusData = "OK"; string tagData = "text/xml"; message.BodyBuffer = new System.Text.ASCIIEncoding().GetBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?><blah>" + GetWhat + "</blah>"); message.AddTag("Content-Type", tagData); WebSession.Send(message); return(null); }
private void ContinueRequest(IPEndPoint dest, string PQ, object Tag, HTTPMessage MSG) { HTTPMessage r = null; if (MSG == null) { r = new HTTPMessage(); r.Directive = "GET"; r.DirectiveObj = PQ; if (dest.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { r.AddTag("Host", dest.ToString()); } if (dest.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) { r.AddTag("Host", "[" + RemoveIPv6Scope(dest.ToString()) + "]"); } } else { r = MSG; } lock (TagQueue) { this.IdleTimeout = false; KeepAliveTimer.Remove(this.GetHashCode()); LastMessage = r; if ((PIPELINE == false && _PIPELINE == false) || (_PIPELINE == false)) { HTTPRequest TR = new HTTPRequest(); TR.ProxySetting = ProxySetting; TR._PIPELINE = true; if (this.OnSniff != null) { TR.OnSniff += new HTTPRequest.SniffHandler(NonPipelinedSniffSink); } if (this.OnSniffPacket != null) { TR.OnSniffPacket += new HTTPRequest.RequestHandler(NonPipelinedSniffPacketSink); } TR.OnResponse += new HTTPRequest.RequestHandler(NonPipelinedResponseSink); this.NotPipelinedTable[TR] = TR; TR.PipelineRequest(dest, r, Tag); return; } bool NeedSend = (TagQueue.Count == 0); TagQueue.Enqueue(new StateData(r, dest, Tag, null)); IPAddress localif = IPAddress.Any; if (dest.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) { localif = IPAddress.IPv6Any; } if (s == null) { ReceivedFirstResponse = false; if (ProxySetting != null) { s = new HTTPSession(new IPEndPoint(localif, 0), ProxySetting, new HTTPSession.SessionHandler(CreateSink), new HTTPSession.SessionHandler(CreateFailedSink), null); } else { s = new HTTPSession(new IPEndPoint(localif, 0), dest, new HTTPSession.SessionHandler(CreateSink), new HTTPSession.SessionHandler(CreateFailedSink), null); } } else { if (s.IsConnected && this.ReceivedFirstResponse) { try { if (ProxySetting == null) { s.Send(r); } else { HTTPMessage pr = (HTTPMessage)r.Clone(); pr.DirectiveObj = "http://" + dest.ToString() + pr.DirectiveObj; pr.Version = "1.0"; s.Send(pr); } } catch (Exception ex) { OpenSource.Utilities.EventLogger.Log(ex); } } } } }
/// <summary> /// This method gets called when a new Request is received /// </summary> /// <param name="request"></param> /// <param name="WebSession"></param> protected void ReceiveSink(HTTPMessage request, HTTPSession WebSession) { HTTPMessage rsp = new HTTPMessage(); UTF8Encoding U = new UTF8Encoding(); if ((request.Directive == "HEAD") || (request.Directive == "GET")) { if (request.DirectiveObj == "/item.m3u") {/* * rsp.StatusCode = 200; * rsp.StatusData = "OK"; * rsp.ContentType = "audio/mpegurl"; * rsp.StringBuffer = M3UString; */ string r = request.GetTag("Range"); if (r == "") { rsp.StatusCode = 200; rsp.StatusData = "OK"; rsp.ContentType = "audio/mpegurl"; rsp.StringBuffer = M3UString; } else { rsp.StatusCode = 206; rsp.StatusData = "Partial Content"; rsp.ContentType = "audio/mpegurl"; DText rp0 = new DText(); rp0.ATTRMARK = "="; rp0[0] = r; DText rp = new DText(); rp.ATTRMARK = "-"; rp[0] = rp0[2].Trim(); if (rp[1] == "") { // LastBytes try { if (int.Parse(rp[2]) > M3UString.Length) { rsp.AddTag("Content-Range", "bytes 0-" + M3UString.Length.ToString() + "/" + M3UString.Length.ToString()); rsp.StringBuffer = M3UString; } else { rsp.AddTag("Content-Range", "bytes " + (M3UString.Length - int.Parse(rp[2])).ToString() + "-" + M3UString.Length.ToString() + "/" + M3UString.Length.ToString()); rsp.StringBuffer = M3UString.Substring(M3UString.Length - int.Parse(rp[2])); } } catch (Exception) { rsp = new HTTPMessage(); rsp.StatusCode = 400; rsp.StatusData = "Bad Request"; } } else if (rp[2] == "") { // Offset till end try { rsp.AddTag("Content-Range", "bytes " + rp[1] + "-" + M3UString.Length.ToString() + "/" + M3UString.Length.ToString()); rsp.StringBuffer = M3UString.Substring(int.Parse(rp[1])); } catch (Exception) { rsp = new HTTPMessage(); rsp.StatusCode = 400; rsp.StatusData = "Bad Request"; } } else { // Range try { if (int.Parse(rp[2]) > M3UString.Length + 1) { rsp.AddTag("Content-Range", "bytes " + rp[1] + "-" + (M3UString.Length - 1).ToString() + "/" + M3UString.Length.ToString()); rsp.StringBuffer = M3UString.Substring(int.Parse(rp[1])); } else { rsp.AddTag("Content-Range", "bytes " + rp[1] + "-" + rp[2] + "/" + M3UString.Length.ToString()); rsp.StringBuffer = M3UString.Substring(int.Parse(rp[1]), 1 + int.Parse(rp[2]) - int.Parse(rp[1])); } } catch (Exception) { rsp = new HTTPMessage(); rsp.StatusCode = 400; rsp.StatusData = "Bad Request"; } } } } else { try { int i = request.DirectiveObj.LastIndexOf("/"); string tmp = request.DirectiveObj.Substring(1, i - 1); bool hasRange = true; if (tmp.IndexOf("/") != -1) { tmp = tmp.Substring(0, tmp.IndexOf("/")); } if (FileInfoTable.ContainsKey(tmp)) { FileInfo f; if (FileInfoTable[tmp].GetType() == typeof(string)) { f = new FileInfo((string)FileInfoTable[tmp]); FileInfoTable[tmp] = f; } else { f = (FileInfo)FileInfoTable[tmp]; } ProtocolInfoString pis = ProtocolInfoString.CreateHttpGetProtocolInfoString(f); rsp.StatusCode = 200; rsp.StatusData = "OK"; rsp.ContentType = pis.MimeType; if (request.Directive == "HEAD") { rsp.AddTag("Content-Length", f.Length.ToString()); rsp.OverrideContentLength = true; } else { HTTPSession.Range[] RNG = null; if (request.GetTag("Range") != "") { long x, y; ArrayList RList = new ArrayList(); DText rp = new DText(); rp.ATTRMARK = "="; rp.MULTMARK = ","; rp.SUBVMARK = "-"; rp[0] = request.GetTag("Range"); for (int I = 1; I <= rp.DCOUNT(2); ++I) { if (rp[2, I, 1] == "") { // Final Bytes y = long.Parse(rp[2, I, 2].Trim()); x = f.Length - y; y = x + y; } else if (rp[2, I, 2] == "") { // Offset till end x = long.Parse(rp[2, I, 1].Trim()); y = f.Length - x; } else { // Full Range x = long.Parse(rp[2, I, 1].Trim()); y = long.Parse(rp[2, I, 2].Trim()); } RList.Add(new HTTPSession.Range(x, 1 + y - x)); } RNG = (HTTPSession.Range[])RList.ToArray(typeof(HTTPSession.Range)); } else { hasRange = false; } if (request.Version == "1.0") { WebSession.OnStreamDone += new HTTPSession.StreamDoneHandler(DoneSink); } // ((HTTPMessage)(new UPnPDebugObject(WebSession)).GetField("Headers")).Version = "1.0"; if (hasRange) { WebSession.SendStreamObject(f.OpenRead(), RNG, pis.MimeType); } else { FileStream fs = f.OpenRead(); long length = fs.Length; WebSession.SendStreamObject(fs, length, pis.MimeType); } return; } } else { rsp.StatusCode = 404; rsp.StatusData = "Not Found"; } } catch (Exception) { rsp.StatusCode = 404; rsp.StatusData = "Not Found"; } } WebSession.Send(rsp); return; } rsp.StatusCode = 500; rsp.StatusData = "Not implemented"; WebSession.Send(rsp); }
/// <summary> /// This method gets called when a new Request is received /// </summary> /// <param name="request"></param> /// <param name="WebSession"></param> protected void ReceiveSink(HTTPMessage request, HTTPSession WebSession) { HTTPMessage rsp = new HTTPMessage(); UTF8Encoding U = new UTF8Encoding(); if ((request.Directive == "HEAD") || (request.Directive == "GET")) { if (request.DirectiveObj == "/item.m3u") {/* rsp.StatusCode = 200; rsp.StatusData = "OK"; rsp.ContentType = "audio/mpegurl"; rsp.StringBuffer = M3UString; */ string r = request.GetTag("Range"); if (r == "") { rsp.StatusCode = 200; rsp.StatusData = "OK"; rsp.ContentType = "audio/mpegurl"; rsp.StringBuffer = M3UString; } else { rsp.StatusCode = 206; rsp.StatusData = "Partial Content"; rsp.ContentType = "audio/mpegurl"; DText rp0 = new DText(); rp0.ATTRMARK = "="; rp0[0] = r; DText rp = new DText(); rp.ATTRMARK = "-"; rp[0] = rp0[2].Trim(); if (rp[1] == "") { // LastBytes try { if (int.Parse(rp[2]) > M3UString.Length) { rsp.AddTag("Content-Range", "bytes 0-" + M3UString.Length.ToString() + "/" + M3UString.Length.ToString()); rsp.StringBuffer = M3UString; } else { rsp.AddTag("Content-Range", "bytes " + (M3UString.Length - int.Parse(rp[2])).ToString() + "-" + M3UString.Length.ToString() + "/" + M3UString.Length.ToString()); rsp.StringBuffer = M3UString.Substring(M3UString.Length - int.Parse(rp[2])); } } catch (Exception) { rsp = new HTTPMessage(); rsp.StatusCode = 400; rsp.StatusData = "Bad Request"; } } else if (rp[2] == "") { // Offset till end try { rsp.AddTag("Content-Range", "bytes " + rp[1] + "-" + M3UString.Length.ToString() + "/" + M3UString.Length.ToString()); rsp.StringBuffer = M3UString.Substring(int.Parse(rp[1])); } catch (Exception) { rsp = new HTTPMessage(); rsp.StatusCode = 400; rsp.StatusData = "Bad Request"; } } else { // Range try { if (int.Parse(rp[2]) > M3UString.Length + 1) { rsp.AddTag("Content-Range", "bytes " + rp[1] + "-" + (M3UString.Length - 1).ToString() + "/" + M3UString.Length.ToString()); rsp.StringBuffer = M3UString.Substring(int.Parse(rp[1])); } else { rsp.AddTag("Content-Range", "bytes " + rp[1] + "-" + rp[2] + "/" + M3UString.Length.ToString()); rsp.StringBuffer = M3UString.Substring(int.Parse(rp[1]), 1 + int.Parse(rp[2]) - int.Parse(rp[1])); } } catch (Exception) { rsp = new HTTPMessage(); rsp.StatusCode = 400; rsp.StatusData = "Bad Request"; } } } } else { try { int i = request.DirectiveObj.LastIndexOf("/"); string tmp = request.DirectiveObj.Substring(1, i - 1); bool hasRange = true; if (tmp.IndexOf("/") != -1) { tmp = tmp.Substring(0, tmp.IndexOf("/")); } if (FileInfoTable.ContainsKey(tmp)) { FileInfo f; if (FileInfoTable[tmp].GetType() == typeof(string)) { f = new FileInfo((string)FileInfoTable[tmp]); FileInfoTable[tmp] = f; } else { f = (FileInfo)FileInfoTable[tmp]; } ProtocolInfoString pis = ProtocolInfoString.CreateHttpGetProtocolInfoString(f); rsp.StatusCode = 200; rsp.StatusData = "OK"; rsp.ContentType = pis.MimeType; if (request.Directive == "HEAD") { rsp.AddTag("Content-Length", f.Length.ToString()); rsp.OverrideContentLength = true; } else { HTTPSession.Range[] RNG = null; if (request.GetTag("Range") != "") { long x, y; ArrayList RList = new ArrayList(); DText rp = new DText(); rp.ATTRMARK = "="; rp.MULTMARK = ","; rp.SUBVMARK = "-"; rp[0] = request.GetTag("Range"); for (int I = 1; I <= rp.DCOUNT(2); ++I) { if (rp[2, I, 1] == "") { // Final Bytes y = long.Parse(rp[2, I, 2].Trim()); x = f.Length - y; y = x + y; } else if (rp[2, I, 2] == "") { // Offset till end x = long.Parse(rp[2, I, 1].Trim()); y = f.Length - x; } else { // Full Range x = long.Parse(rp[2, I, 1].Trim()); y = long.Parse(rp[2, I, 2].Trim()); } RList.Add(new HTTPSession.Range(x, 1 + y - x)); } RNG = (HTTPSession.Range[])RList.ToArray(typeof(HTTPSession.Range)); } else { hasRange = false; } if (request.Version == "1.0") { WebSession.OnStreamDone += new HTTPSession.StreamDoneHandler(DoneSink); } // ((HTTPMessage)(new UPnPDebugObject(WebSession)).GetField("Headers")).Version = "1.0"; if (hasRange) { WebSession.SendStreamObject(f.OpenRead(), RNG, pis.MimeType); } else { FileStream fs = f.OpenRead(); long length = fs.Length; WebSession.SendStreamObject(fs, length, pis.MimeType); } return; } } else { rsp.StatusCode = 404; rsp.StatusData = "Not Found"; } } catch (Exception) { rsp.StatusCode = 404; rsp.StatusData = "Not Found"; } } WebSession.Send(rsp); return; } rsp.StatusCode = 500; rsp.StatusData = "Not implemented"; WebSession.Send(rsp); }
private void SendStreamObject(HTTPSession webSession, HTTPMessage msg, string fileName) { Stream stream = null; try { if (!fileName.Contains("/")) { fileName = HttpUtility.UrlDecode(fileName); } string str = (fileName[0] == '1' ? fileName.Substring(2, fileName.LastIndexOf('/') - 2) : fileName.Substring(2)); int idx = str.Length; string freeboxV5 = GetString(str, ref idx); string file = str.Substring(0, idx); string mimeType = null; long contentLength = -1; bool liveStream = MimeType.IsLiveStream(file); string rangeStr = (msg == null ? null : msg.GetTag("RANGE")); List <HTTPSession.Range> rangeSets = new List <HTTPSession.Range>(); if (!String.IsNullOrEmpty(rangeStr)) { rangeStr = rangeStr.Trim().ToLower(); } if (liveStream) { Web.CreateWebRequest(rangeSets, rangeStr, file, ref stream, ref contentLength, ref mimeType); if (!String.IsNullOrEmpty(mimeType) && mimeType.ToUpperInvariant() == "AUDIO/X-SCPLS") { file = new PLSPlaylist().GetMediaUrl(Web.GetStringAndCloseStream(stream, Encoding.UTF8)); Web.CreateWebRequest(rangeSets, rangeStr, file, ref stream, ref contentLength, ref mimeType); } } else { stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read); mimeType = MimeType.GetMimeTypeFromFileName(file); } if (stream == null) { SendErrorMessage(webSession, null); return; } bool canSeek = stream.CanSeek; if (canSeek) { contentLength = stream.Length; } PreventSystemStandBy(); if (msg != null && String.Compare(msg.Directive, "HEAD", true) == 0) { HTTPMessage head = new HTTPMessage(); head.StatusCode = 200; head.StatusData = "OK"; head.ContentType = mimeType; if (contentLength >= 0) { head.OverrideContentLength = true; if (String.IsNullOrEmpty(rangeStr)) { head.AddTag("Content-Length", contentLength.ToString()); head.AddTag("Accept-Ranges", "bytes"); } else { head.StatusCode = 206; rangeSets.AddRange(rangeStr.Trim().ToLower(), contentLength); if (rangeSets.Count == 1) { HTTPSession.Range range = rangeSets[0]; head.AddTag("Content-Range", "bytes " + range.Position.ToString() + "-" + ((long)(range.Position + range.Length - 1)).ToString() + "/" + contentLength.ToString()); head.AddTag("Content-Length", range.Length.ToString()); } } } else { // Can't calculate length => can't do range head.AddTag("Accept-Ranges", "none"); } webSession.Send(head); } else { if (canSeek && contentLength >= 0 && rangeSets.Count == 0) { rangeSets.AddRange(rangeStr, contentLength); } lock (webSession) { webSession.OnStreamDone += new HTTPSession.StreamDoneHandler(WebSession_OnStreamDone); if (freeboxV5 == "1" && mimeType == "audio/mpeg") { webSession.StateObject = file; } bool image = (MimeType.GetMediaKind(mimeType) == MediaKind.Image); if (freeboxV5 == "1") { if (!image) { webSession.BUFFER_SIZE = Settings.Default.V5BufferSize; } } else { webSession.BUFFER_SIZE = Settings.Default.V6BufferSize; } if (rangeSets.Count > 0) { webSession.SendStreamObject(stream, rangeSets.ToArray(), mimeType); } else { if (canSeek) { stream.Seek(0, SeekOrigin.Begin); } if (contentLength >= 0) { webSession.SendStreamObject(stream, contentLength, mimeType, image); } else { webSession.SendStreamObject(stream, mimeType); } } } } } catch (Exception ex) { Utils.WriteException(ex); SendErrorMessage(webSession, stream); } }