예제 #1
0
        private void HandleFile(FileInfo f, Uri uri, HttpListenerContext p, StreamWriter sw)
        {
            using (var reader = new System.IO.StreamReader(f.ToString(), true))
            {
                var fs = reader.BaseStream.ToArray();

                p.Response.ContentType = Mime.GetMimeType(f.Extension);

                if (!ContainsLang(f))
                {
                    if (Mime.GetMimeType(f.Extension).StartsWith("image"))
                    {
                        p.Response.OutputStream.Write(fs, 0, fs.Length);
                        p.Response.OutputStream.Flush();
                    }
                    else
                    {
                        sw.WriteLine(reader.CurrentEncoding.GetString(fs));
                        sw.Flush();
                    }
                }
                else
                {
                    var lng = GetLanguageByExtension(f);
                    lng.Load();

                    lng.Execute(reader.CurrentEncoding.GetString(fs), uri, p, _wc, sw);

                    LoggerModule.Log(lng.Name + " Script executed");
                }
            }
        }
예제 #2
0
        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);
        }
예제 #3
0
        public static Assembly Init(CodeDomProvider bcp, string src, StreamWriter sw, IniFile ini)
        {
            var options = new CompilerParameters();

            options.GenerateExecutable = false;
            options.GenerateInMemory   = true;

            options.ReferencedAssemblies.AddRange(new[] { "System.dll", "System.Core.dll", "System.Drawing.dll", typeof(IScriptLanguage).Assembly.Location, typeof(Mono.Net.HttpListener).Assembly.Location });

            var sec = ini.GetSection("References");

            if (sec != null)
            {
                foreach (var refs in sec)
                {
                    options.ReferencedAssemblies.Add(refs.Value);
                }
            }

            var res = bcp.CompileAssemblyFromSource(options, src);

            if (res.Errors.HasErrors)
            {
                if (sw != null)
                {
                    foreach (CompilerError item in res.Errors)
                    {
                        LoggerModule.Log(item.Line + ": " + item.ErrorText);

                        throw new Exception(item.ErrorText);
                    }
                }
            }
            else
            {
                return(res.CompiledAssembly);
            }

            return(null);
        }
예제 #4
0
        public static string GetHtml(int errorcode)
        {
            if (_ac.ErrorDocument.ContainsKey(errorcode.ToString()))
            {
                var tmp = _ac.ErrorDocument[errorcode.ToString()];
                var fi  = new FileInfo(tmp.Replace("{data}", _wc.DataDir) + "\\");

                if (fi.Exists)
                {
                    var er = File.ReadAllText(tmp);

                    LoggerModule.Log(er);

                    return(er);
                }

                return(tmp);
            }
            else
            {
                return("<h1>Error " + errorcode + "</h1>");
            }
        }
예제 #5
0
        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;
             * }
             */
        }