public void Render(StreamWriter sw) { if (!isListing()) { sw.WriteLine(ErrorProvider.GetHtml(403)); LoggerModule.Log(ErrorProvider.GetHtml(403).StripHtml()); return; } LoggerModule.Log("Lisiting Directory"); string content = Resources.Listing; var dict = new Dictionary <string, object>(); dict.Add("Folder", _p.Request.Url.LocalPath); dict.Add("Path", _p.Request.Url.AbsoluteUri); content = Template.Render(content, dict); folderItem = Template.Render(folderItem, dict); fileItem = Template.Render(fileItem, dict); var sb = new StringBuilder(); var pdi = new DirectoryInfo(_fi.ToString()); parentItem = parentItem.Replace("{{ParentFolder}}", ".."); sb.AppendLine(parentItem); foreach (var folder in Directory.GetDirectories(_fi.ToString())) { var di = new DirectoryInfo(folder); if (folder != "Listing") { sb.AppendLine(folderItem.Replace("{{folder}}", di.Name).Replace("{{Size}}", "0")); } } foreach (var file in Directory.GetFiles(_fi.ToString())) { var fi = new FileInfo(file); if (isIgnored(fi.Name)) { continue; } sb.AppendLine(fileItem.Replace("{{File}}", fi.Name).Replace("{{Size}}", SizeFormatter.Format(fi.Length, 2))); } content = content.Replace("{{Items}}", sb.ToString()); sw.WriteLine(content); }
public void HandleRequest(HttpListenerContext p) { var uri = p.Request.Url; LoggerModule.Log("request " + uri.AbsolutePath); var sw = new StreamWriter(p.Response.OutputStream); var f = new FileInfo(_wc.DataDir + uri.AbsolutePath); if (Access.HasAccess(f.DirectoryName)) { var htaccess = new Furesoft.Web.Internal.HtAccess.Parser(); htaccess.AddContstant("HTTP_HOST", uri.Host); htaccess.AddContstant("HTTP_URI", uri.ToString()); htaccess.AddContstant("HTTP_PATH", uri.LocalPath); if (f.Exists) { htaccess.Parse(File.ReadAllText(f.DirectoryName + @"\.htaccess")); } else { htaccess.Parse(File.ReadAllText(_wc.DataDir + @"\.htaccess")); } ac = new Access(f.DirectoryName); //htaccess.PopulateCondition("*" + Path.GetExtension(f.Name), ac); htaccess.Populate(ac); AccessModule.Init(ac); if (ac.Redirect.Count > 0) { foreach (var r in ac.Redirect) { if (uri.AbsolutePath == "/" + r.Key) { uri = new Uri(_wc.DataDir + r.Key); string uris = HttpUtility.UrlDecode(uri.ToString()).Replace("file:///", ""); uri = new Uri(uris); if (uris.Contains("?")) { uris = uris.Substring(0, uris.IndexOf("?")); } f = new FileInfo(uris); p.Response.Redirect(r.Value); return; } } } if (ac.RewriteEngine) { var ff = ""; RewriteRule.Match(uri, ac, out ff); if (ff != "") { uri = new Uri(_wc.DataDir + ff); string uris = HttpUtility.UrlDecode(uri.ToString()).Replace("file:///", ""); uri = new Uri(uris); if (uris.Contains("?")) { uris = uris.Substring(0, uris.IndexOf("?")); } f = new FileInfo(uris); } } foreach (var er in ac.ErrorDocument) { if (!_wc.ErrorPages.ContainsKey(er.Key)) { _wc.ErrorPages.Add(er.Key, er.Value); } } if (htaccess.GetCommand <Directive>("AuthUserFile") != null) { ac.ReadUserFile(); } } if (AccessModule.IsBlocked(p.Request.LocalEndPoint.Address.ToString())) { sw.WriteLine(ErrorProvider.GetHtml(403)); LoggerModule.Log(ErrorProvider.GetHtml(403)); return; } if (ac.AuthType == "Basic") { if (!authed) { // ws.AuthenticationSchemes = AuthenticationSchemes.Basic; ws.AuthenticationSchemeSelectorDelegate = (s) => { try { if (p.User.Identity.IsAuthenticated) { LoggerModule.Log("Authentication requested"); foreach (var u in ac.Users) { var identity = (HttpListenerBasicIdentity)p.User.Identity; if (identity.Name == u.Username && identity.Password == u.Password) { // authed = true; return(AuthenticationSchemes.None); } else { p.Response.StatusCode = 403; p.Response.OutputStream.Seek(10, SeekOrigin.Current); sw.WriteLine("<p>Access Denied</p>"); LoggerModule.Log(ErrorProvider.GetHtml(403)); } } } } catch { } if (ac.AuthType == "Basic") { return(AuthenticationSchemes.Basic); } else { return(AuthenticationSchemes.None); } }; } if (File.Exists(f.ToString())) { HandleFile(f, uri, p, sw); } else if (!uri.IsFile && Directory.Exists(f.ToString())) { var index = "index.html"; if (ac != null) { if (ac.DirectoryIndex != null) { index = ac.DirectoryIndex; } } if (!File.Exists(f.ToString() + index)) { new DirectoryListing(ac, p, f).Render(sw); } else { HandleFile(new FileInfo(f.ToString() + index), uri, p, sw); } } else { sw.WriteLine(ErrorProvider.GetHtml(404)); LoggerModule.Log(ErrorProvider.GetHtml(404)); } p.Response.OutputStream.Flush(); sw.Flush(); p.Response.Close(); sw.Close(); } /* * public byte[] SendResponse(HttpListenerRequest request, HttpListenerResponse response) * { * var f = new FileInfo(_wc.DataDir + request.Url.AbsolutePath); * if (request.HttpMethod == "GET") * { * if(Access.HasAccess(f.DirectoryName)) * { * var htaccess = new Furesoft.Web.Internal.HtAccess.Parser(); * htaccess.Parse(File.ReadAllText(f.DirectoryName + @"\.htaccess")); * var ac = new Access(f.DirectoryName); * htaccess.PopulateCondition("*" + Path.GetExtension(f.Name), ac); * htaccess.Populate(ac); * if (htaccess.GetCommand<Directive>("AuthUserFile") != null) * { * ac.AuthUserFile = (string)htaccess.GetCommand<Directive>("AuthUserFile").Values[0]; * ac.ReadUserFile(); * } * if (htaccess.GetCommand<Directive>("AuthName") != null) * { * ac.AuthName = (string)htaccess.GetCommand<Directive>("AuthName").Values[0]; * } * if (htaccess.GetCommand<Directive>("AuthType") != null) * { * ac.AuthType = (string)htaccess.GetCommand<Directive>("AuthType").Values[0]; * ws._listener.AuthenticationSchemes = (AuthenticationSchemes)Enum.Parse(typeof(AuthenticationSchemes), ac.AuthType); * HttpListenerContext context = ws._listener.GetContext(); * HttpListenerBasicIdentity identity = (HttpListenerBasicIdentity)context.User.Identity; * if (identity != null) * { * foreach (var user in ac.Users) * { * if (user.Username == identity.Name) * { * if ((user.Password) == identity.Password) * { * if (File.Exists(f.ToString())) * { * // GetScriptByExtension(f.ToString()); * return File.ReadAllBytes(f.ToString()); * } * else if (!request.Url.IsFile) * { * return File.ReadAllBytes(_wc.DataDir + "index.html"); * } * else * { * return Encoding.ASCII.GetBytes(_wc.ErrorPages["403"]); * } * } * else * { * return Encoding.ASCII.GetBytes(_wc.ErrorPages["403"]); * } * } * } * } * else * { * return Encoding.ASCII.GetBytes(_wc.ErrorPages["404"]); * } * } * } * } * else * { * if (request.Headers.AllKeys.ToList().Contains("service")) * { * if (request.IsSecureConnection) * { * } * } * } * return null; * } */ }