public override void OnResponse(ref HTTPRequestStruct rq, ref HTTPResponseStruct rp) { string path = this.Folder + "\\" + rq.URL.PathAndQuery.ToString().Replace("/", "\\"); Media media = null; if (Directory.Exists(path)) { if (File.Exists(path + "default.htm")) path += "\\default.htm"; else { string[] dirs = Directory.GetDirectories(path); string[] files = Directory.GetFiles(path); rp.Headers["Content-Type"] = "text/html"; string bodyStr = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n"; bodyStr += "<HTML><HEAD>\n"; bodyStr += "<META http-equiv=Content-Type content=\"text/html; charset=windows-1252\">\n"; bodyStr += "</HEAD>\n"; bodyStr += "<BODY><p>Folder listing, to do not see this add a 'default.htm' document\n<p>\n"; for (int i = 0; i < dirs.Length; i++) bodyStr += "<br><a href = \"" + rq.URL + Path.GetFileName(dirs[i]) + "/\">[" + Path.GetFileName(dirs[i]) + "]</a>\n"; for (int i = 0; i < files.Length; i++) bodyStr += "<br><a href = \"" + rq.URL + Path.GetFileName(files[i]) + "\">" + Path.GetFileName(files[i]) + "</a>\n"; bodyStr += "</BODY></HTML>\n"; rp.BodyData = Encoding.ASCII.GetBytes(bodyStr); return; } } if ((media = MediaDB.Instance.HashToMedia(rq.URL.PathAndQuery.TrimStart(new char[] { '/' }))) != null) { System.Diagnostics.Debug.Assert(media.uri.Scheme == Uri.UriSchemeFile); path = media.uri.LocalPath; } if (rq.URL.PathAndQuery.ToString() == "/crossfade.xml") { P2PDB_MediaDB mdb = new P2PDB_MediaDB(); mdb.mediaList = MediaDB.Instance.mediaList; XmlSerializer s = new XmlSerializer(typeof(P2PDB_MediaDB)); Stream w = new MemoryStream(); s.Serialize(w, mdb); w.Seek(0, SeekOrigin.Begin); rp.fs = w; rp.Headers["Content-Type"] = "text/xml"; rp.Headers["Last-Modified"] = DateTime.Now.ToString("r"); rp.Headers["Accept-Ranges"] = "none"; rp.Headers["Content-Length"] = w.Length; rp.Headers["Connection"] = "close"; } else if (File.Exists(path)) { RegistryKey rk = Registry.ClassesRoot.OpenSubKey(Path.GetExtension(path), true); // Get the data from a specified item in the key. String s = (String)rk.GetValue("Content Type"); // Open the stream and read it back. rp.fs = File.Open(path, FileMode.Open); if (s != "") rp.Headers["Content-Type"] = s; System.IO.FileInfo fi = new System.IO.FileInfo(path); rp.Headers["Last-Modified"] = fi.LastWriteTime.ToString("r"); rp.Headers["Accept-Ranges"] = "none"; rp.Headers["Content-Length"] = fi.Length.ToString(); rp.Headers["Connection"] = "close"; } else { rp.status = (int)RespState.NOT_FOUND; rp.Headers["Accept-Ranges"] = "none"; rp.Headers["Connection"] = "close"; rp.Headers["Content-Type"] = "text/html"; string bodyStr = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n"; bodyStr += "<HTML><HEAD>\n"; bodyStr += "<META http-equiv=Content-Type content=\"text/html; charset=windows-1252\">\n"; bodyStr += "</HEAD>\n"; bodyStr += "<BODY>File not found!!</BODY></HTML>\n"; rp.BodyData = Encoding.ASCII.GetBytes(bodyStr); } }
public abstract void OnResponse(ref HTTPRequestStruct rq, ref HTTPResponseStruct rp);