Exemple #1
0
        }         // proc OnEndReadConfiguration

        public override bool Request(IDEContext r)
        {
            // create the full file name
            var fileName = Path.GetFullPath(Path.Combine(directoryBase, ProcsDE.GetLocalPath(r.RelativeSubPath)));

            // Check for a directory escape
            if (!fileName.StartsWith(directoryBase, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            // is the filename a directory, add index.html
            if (Directory.Exists(fileName))
            {
                fileName = Path.Combine(fileName, "Index.html");
            }

            if (File.Exists(fileName))
            {
                // security
                DemandFile(r, fileName);
                // Send the file
                r.WriteFile(fileName, GetFileContentType(fileName));
                return(true);
            }
            else
            {
                return(false);
            }
        }         // func Request
Exemple #2
0
        }         // proc OnBeginReadConfiguration

        public override bool Request(IDEContext r)
        {
            if (assembly == null || namespaceRoot == null)
            {
                return(false);
            }

            // create the resource name
            var resourceName = namespaceRoot + r.RelativeSubPath.Replace('/', '.');

            var src = (Stream)null;

            try
            {
                DateTime stamp;
                // try to open the resource stream
                var forceAlternativeCheck = nonePresentAlternativeExtensions != null && nonePresentAlternativeExtensions.FirstOrDefault(c => resourceName.EndsWith(c, StringComparison.OrdinalIgnoreCase)) != null;
                src = assembly.GetManifestResourceStream(resourceName);
                if (src == null && !forceAlternativeCheck)                 // nothing...
                {
                    return(false);
                }

                // check if there is a newer file
                if (alternativeRoots != null)
                {
                    var relativeFileName = ProcsDE.GetLocalPath(r.RelativeSubPath);
                    var alternativeFile  = (from c in alternativeRoots
                                            let fi = new FileInfo(Path.Combine(c, relativeFileName))
                                                     where fi.Exists && (forceAlternativeCheck || fi.LastWriteTimeUtc > assemblyStamp)
                                                     orderby fi.LastWriteTimeUtc descending
                                                     select fi).FirstOrDefault();

                    if (alternativeFile != null)
                    {
                        src?.Close();
                        src   = alternativeFile.OpenRead();
                        stamp = alternativeFile.LastWriteTimeUtc;
                    }
                    else
                    {
                        stamp = assemblyStamp;
                        if (forceAlternativeCheck && src == null)
                        {
                            return(false);
                        }
                    }
                }
                else
                {
                    stamp = assemblyStamp;
                    if (forceAlternativeCheck && src == null)
                    {
                        return(false);
                    }
                }

                // security
                DemandFile(r, resourceName);
                // send the file
                r.SetLastModified(stamp)
                .WriteStream(src, GetFileContentType(resourceName) ?? r.Server.GetContentType(Path.GetExtension(resourceName)));
                return(true);
            }
            finally
            {
                Procs.FreeAndNil(ref src);
            }
        }         // func Request
Exemple #3
0
        /// <summary></summary>
        /// <param name="r"></param>
        /// <returns></returns>
        public override async Task <bool> RequestAsync(IDEWebRequestScope r)
        {
            if (String.IsNullOrEmpty(filterPattern))
            {
                return(false);
            }

            // create the full file name
            var useIndex            = false;
            var fullDirectoryName   = this.directoryBase.FullName;
            var fileName            = Path.GetFullPath(Path.Combine(fullDirectoryName, ProcsDE.GetLocalPath(r.RelativeSubPath)));
            var directoryBaseOffset = fullDirectoryName[fullDirectoryName.Length - 1] == Path.DirectorySeparatorChar ? fullDirectoryName.Length - 1 : fullDirectoryName.Length;

            // Check for a directory escape
            if (!fileName.StartsWith(directoryBase.FullName, StringComparison.OrdinalIgnoreCase) ||
                (fileName.Length > directoryBaseOffset && fileName[directoryBaseOffset] != Path.DirectorySeparatorChar))
            {
                return(false);
            }

            // is the filename a directory, add index.html
            if (Directory.Exists(fileName))
            {
                if (!String.IsNullOrEmpty(r.AbsolutePath) && r.AbsolutePath[r.AbsolutePath.Length - 1] != '/')
                {
                    r.Redirect(r.AbsolutePath + '/');
                    return(true);
                }
                else
                {
                    fileName = Path.Combine(fileName, ConfigNode.GetAttribute <string>("indexPage"));
                    useIndex = true;
                }
            }
            else if (filterPattern != "*")             // check for filter pattern
            {
                if (!Procs.IsFilterEqual(Path.GetFileName(fileName), filterPattern))
                {
                    return(false);                    // pattern does not fit
                }
            }

            if (File.Exists(fileName))
            {
                // security
                DemandFile(r, fileName);
                // Send the file
                await Task.Run(() => r.WriteFile(fileName, GetFileContentType(fileName), defaultReadEncoding));

                return(true);
            }
            else if (useIndex && allowListing)             // write index table
            {
                await Task.Run(() => r.WriteResource(typeof(HttpWorker), "Resources.Listing.html", "text/html"));

                return(true);
            }
            else
            {
                return(false);
            }
        }         // func Request
Exemple #4
0
        public IEnumerable <FileInfo> ListFiles(string relativePath = null)
        {
            var directory = new DirectoryInfo(Path.GetFullPath(Path.Combine(directoryBase.FullName, ProcsDE.GetLocalPath(relativePath))));

            if (directory.Exists)
            {
                return(directory.EnumerateFiles(filterPattern, SearchOption.TopDirectoryOnly));
            }
            else
            {
                return(Array.Empty <FileInfo>());
            }
        }         // func ListFiles