WriteEntireResponseFromString() public method

public WriteEntireResponseFromString ( int statusCode, String extraHeaders, String body, bool keepAlive ) : void
statusCode int
extraHeaders String
body String
keepAlive bool
return void
Example #1
0
        bool ProcessDirectoryRequest()
        {
            String dirPathTranslated = _pathTranslated;

            if (_pathInfo.Length > 0)
            {
                // directory path can never have pathInfo
                dirPathTranslated = MapPath(_path);
            }

            if (!Directory.Exists(dirPathTranslated))
            {
                return(false);
            }

            // have to redirect /foo to /foo/ to allow relative links to work
            if (!_path.EndsWith("/", StringComparison.Ordinal))
            {
                string newPath  = _path + "/";
                string location = "Location: " + UrlEncodeRedirect(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";

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

            // check for the default file
            foreach (string filename in defaultFileNames)
            {
                string defaultFilePath = dirPathTranslated + "\\" + 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
                }
            }

            return(false); // go through normal processing
        }
Example #2
0
        bool ProcessDirectoryListingRequest()
        {
            if (_verb != "GET")
            {
                return(false);
            }

            String dirPathTranslated = _pathTranslated;

            if (_pathInfo.Length > 0)
            {
                // directory path can never have pathInfo
                dirPathTranslated = MapPath(_path);
            }

            if (!Directory.Exists(dirPathTranslated))
            {
                return(false);
            }

            // have to redirect /foo to /foo/ to allow relative links to work
            if (!_path.EndsWith("/", StringComparison.Ordinal))
            {
                string newPath  = _path + "/";
                string location = "Location: " + UrlEncodeRedirect(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";

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

            // check for the default file
            foreach (string filename in defaultFileNames)
            {
                string defaultFilePath = dirPathTranslated + "\\" + 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(dirPathTranslated)).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;
                }
            }

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