System.Net.FtpControlStream.TryUpdateResponseUri C# (CSharp) Method

TryUpdateResponseUri() private method

Attempts to find the response Uri Typical string looks like this, need to get trailing filename "150 Opening BINARY mode data connection for FTP46.tmp."

private TryUpdateResponseUri ( string str, FtpWebRequest request ) : void
str string
request FtpWebRequest
return void
        private void TryUpdateResponseUri(string str, FtpWebRequest request)
        {
            Uri baseUri = request.RequestUri;
            //
            // Not sure what we are doing here but I guess the logic is IIS centric
            //
            int start = str.IndexOf("for ");
            if (start == -1)
                return;
            start += 4;
            int end = str.LastIndexOf('(');
            if (end == -1)
                end = str.Length;
            if (end <= start)
                return;

            string filename = str.Substring(start, end - start);
            filename = filename.TrimEnd(new char[] { ' ', '.', '\r', '\n' });
            // Do minimal escaping that we need to get a valid Uri
            // when combined with the baseUri
            string escapedFilename;
            escapedFilename = filename.Replace("%", "%25");
            escapedFilename = escapedFilename.Replace("#", "%23");

            // help us out if the user forgot to add a slash to the directory name
            string orginalPath = baseUri.AbsolutePath;
            if (orginalPath.Length > 0 && orginalPath[orginalPath.Length - 1] != '/')
            {
                UriBuilder uriBuilder = new UriBuilder(baseUri);
                uriBuilder.Path = orginalPath + "/";
                baseUri = uriBuilder.Uri;
            }

            Uri newUri;
            if (!Uri.TryCreate(baseUri, escapedFilename, out newUri))
            {
                throw new FormatException(SR.Format(SR.net_ftp_invalid_response_filename, filename));
            }
            else
            {
                if (!baseUri.IsBaseOf(newUri) ||
                     baseUri.Segments.Length != newUri.Segments.Length - 1)
                {
                    throw new FormatException(SR.Format(SR.net_ftp_invalid_response_filename, filename));
                }
                else
                {
                    _responseUri = newUri;
                }
            }
        }