Beispiel #1
0
        private bool ProcessDirectoryListingRequest()
        {
            if (_verb != "GET")
            {
                return(false);
            }

            // last element extension-less?
            int i1 = _pathTranslated.LastIndexOf('\\');
            int i2 = _pathTranslated.IndexOf('.', i1);

            if (i2 >= i1)
            {
                return(false);
            }

            // now check if directory
            if (!Directory.Exists(_pathTranslated))
            {
                return(false);
            }

            // have to redirect /foo to /foo/ to allow relative links to work
            if (!_path.EndsWith("/"))
            {
                String newPath  = _path + "/";
                String location = "Location: " + newPath + "\r\n";
                String body     = "<html><head><title>Object moved</title></head><body>\r\n" +
                                  "<h2>Object moved to <a href='" + newPath + "'>here</a>.</h2>\r\n" +
                                  "</body></html>\r\n";

                _conn.WriteEntireResponseFromString(302, location, body, false);
                return(true);
            }

            // check for the default file
            foreach (String filename in s_defaultFilenames)
            {
                String defaultFilePath = _pathTranslated + "\\" + filename;

                if (File.Exists(defaultFilePath))
                {
                    // pretend the request is for the default file path
                    _path          += filename;
                    _filePath       = _path;
                    _url            = (_queryString != null) ? (_path + "?" + _queryString) : _path;
                    _pathTranslated = defaultFilePath;
                    return(false); // go through normal processing
                }
            }

            // get all files and subdirs
            FileSystemInfo[] infos = null;
            try {
                infos = (new DirectoryInfo(_pathTranslated)).GetFileSystemInfos();
            }
            catch {
            }

            // determine if parent is appropriate
            String parentPath = null;

            if (_path.Length > 1)
            {
                int i = _path.LastIndexOf('/', _path.Length - 2);
                parentPath = (i > 0) ?_path.Substring(0, i) : "/";
                if (!_host.IsVirtualPathInApp(parentPath))
                {
                    parentPath = null;
                }
            }

            _conn.WriteEntireResponseFromString(200, "Content-type: text/html; charset=utf-8\r\n",
                                                Messages.FormatDirectoryListing(_path, parentPath, infos), false);
            return(true);
        }