Esempio n. 1
0
        public List <string> GetListRelativeUrl()
        {
            List <string> listRelativeUrl = new List <string>();

            // Build Relative Path

            var folderRelativePath = FolderRelativePath;

            if (!string.IsNullOrWhiteSpace(AreaName))
            {
                folderRelativePath = Path.Combine(AreaName, folderRelativePath);
            }

            if (!string.IsNullOrWhiteSpace(AreaFolderName))
            {
                folderRelativePath = Path.Combine(AreaFolderName, folderRelativePath);
            }

            var folderAbsolutePath = PathHelper.GetFullPath(folderRelativePath);

            // Scan Files

            var folder = new DirectoryInfo(folderAbsolutePath);

            var listFile = folder.GetFiles("*", SearchOption.AllDirectories);

            // Build Urls

            var httpRequestUrl = HttpRequestPath.Trim('\\', '/');

            listRelativeUrl.AddRange(
                listFile.Select(file =>
                                httpRequestUrl + Path.DirectorySeparatorChar + file.FullName
                                .Replace(folderAbsolutePath, string.Empty)
                                .Trim(Path.DirectorySeparatorChar)
                                ));

            listRelativeUrl = listRelativeUrl.Select(x => x.Replace(Path.DirectorySeparatorChar, '/').Trim('/')).Distinct().ToList();

            return(listRelativeUrl);
        }
Esempio n. 2
0
        private void ConfigurePaths(TrivialWebServer server)
        {
            foreach (XmlElement pathEl in _configRoot.SelectNodes("path"))
            {
                string pattern = XmlUtil.ReadString(pathEl, "@value", null);
                if (pattern != null)
                {
                    pattern = "^" + Regex.Escape(pattern) + "$";
                }
                else
                {
                    pattern = XmlUtil.ReadString(pathEl, "@pattern", null);
                }

                if (pattern == null)
                {
                    throw new ConfigurationException("Path element requires either 'value' or 'pattern'");
                }

                HttpRequestPath path = new HttpRequestPath();
                path.Register(new Regex(pattern), server);

                bool    hasSeenHandler = false;
                XmlNode childNode      = pathEl.FirstChild;
                while (childNode != null)
                {
                    XmlElement childEl = childNode as XmlElement;
                    if (childEl == null)
                    {
                        childNode = childNode.NextSibling;
                        continue;
                    }

                    bool isFilter;
                    switch (childEl.Name)
                    {
                    case "filter":
                        isFilter = true;
                        if (hasSeenHandler)
                        {
                            throw new ConfigurationException("All filters for a given path must come before all the handlers in that path");
                        }
                        break;

                    case "handler":
                        isFilter       = false;
                        hasSeenHandler = true;
                        break;

                    default:
                        throw new ConfigurationException("Element '" + childEl.Name + "' was not expected here");
                    }

                    string          clazz       = XmlUtil.ReadString(childEl, "@className", null);
                    Type            filterType  = Type.GetType(clazz, true);
                    ConstructorInfo constructor = filterType.GetConstructor(new Type[] { typeof(XmlElement) });
                    if (constructor == null)
                    {
                        throw new ConfigurationException("Class " + filterType.FullName + " does not have a constructor that takes XmlElement");
                    }
                    object filterOrHandler = constructor.Invoke(new object[] { childEl });
                    if (isFilter)
                    {
                        if (!(filterOrHandler is HttpRequestFilter))
                        {
                            throw new ConfigurationException("Class " + filterType.FullName + " does not derive from HttpRequestFilter");
                        }
                        path.AddFilter((HttpRequestFilter)filterOrHandler);
                    }
                    else
                    {
                        if (!(filterOrHandler is HttpRequestHandler))
                        {
                            throw new ConfigurationException("Class " + filterType.FullName + " does not derive from HttpRequestHandler");
                        }
                        path.AddHandler((HttpRequestHandler)filterOrHandler);
                    }

                    childNode = childNode.NextSibling;
                }
            }
        }