Пример #1
0
        // ReSharper disable once ParameterHidesMember
        public void BeginSession(FtpConnectionData connectionData)
        {
            ftpChannel = ftpChannelFactory.CreateChannel();

            ftpChannel.Connect(connectionData.Host, connectionData.Port);
            ftpCommunicator.AttachToChannel(ftpChannel);

            FtpServerResponse response = ftpCommunicator.ReadResponse();

            if (response.ReturnCode != (int)FtpReturnCode.ServiceReadyForNewUser)
            {
                throw FtpException("Could not connect to the FTP server", response);
            }

            response = SendCommand("USER {0}", connectionData.Credentials.UserName);

            if (response.ReturnCode == (int)FtpReturnCode.UserNameOkNeedPassword)
            {
                response = SendCommand("PASS {0}", connectionData.Credentials.Password);
            }

            if (response.ReturnCode == (int)FtpReturnCode.UserLoggedIn)
            {
                SetBinaryMode();
                return;
            }

            throw FtpException("Could not log in to the FTP server", response);
        }
Пример #2
0
        private void SetBinaryMode()
        {
            FtpServerResponse response = SendCommand("TYPE I");

            if (response.ReturnCode != (int)FtpReturnCode.CommandOk)
            {
                throw FtpException("Could not execute command", response);
            }
        }
Пример #3
0
        public void ChangeDirectory(string directory)
        {
            FtpServerResponse response = SendCommand("CWD {0}", directory);

            if (response.ReturnCode != (int)FtpReturnCode.RequestedFileActionOkayCompleted)
            {
                throw FtpException("Could not change the current directory", response);
            }

            CurrentDirectory = directory;
        }
Пример #4
0
        private static FtpException FtpException(string errorMessage, FtpServerResponse response)
        {
            Contract.Requires(errorMessage != null);
            Contract.Requires(response != null);
            string message = string.Format(
                CultureInfo.InvariantCulture,
                "{0}: {1}",
                errorMessage,
                response);

            return(new FtpException(message, response.ReturnCode));
        }
Пример #5
0
        public void CreateDirectory(string directory, bool failIfExists)
        {
            FtpServerResponse response = SendCommand("MKD {0}", directory);

            if (response.ReturnCode != (int)FtpReturnCode.Created)
            {
                if (!failIfExists && response.ReturnCode == (int)FtpReturnCode.RequestedActionNotTakenFileUnavailable)
                {
                    return;
                }

                throw FtpException("Could not create the directory", response);
            }
        }
Пример #6
0
        public FtpServerResponse ReadResponse()
        {
            string responseText = ReadResponseLine();

            if (responseText == null)
            {
                throw new FtpException("responseText == null");
            }

            if (singlelineResponseRegex.IsMatch(responseText))
            {
                return(new FtpServerResponse(responseText));
            }

            if (!multilineResponseRegex.IsMatch(responseText))
            {
                throw new FtpException();
            }

            Contract.Assume(responseText.Length >= 4);

            FtpServerResponse firstResponseLine = new FtpServerResponse(responseText);

            while (true)
            {
                responseText = ReadResponseLine();

                if (responseText == null)
                {
                    throw new FtpException();
                }

                if (!singlelineResponseRegex.IsMatch(responseText))
                {
                    continue;
                }

                Contract.Assume(responseText.Length >= 4);

                FtpServerResponse response = new FtpServerResponse(responseText);
                if (response.ReturnCode == firstResponseLine.ReturnCode)
                {
                    return(firstResponseLine);
                }
            }
        }
Пример #7
0
        private IFtpChannel OpenDataChannel()
        {
            FtpServerResponse response = SendCommand("PASV");

            if (response.ReturnCode != (int)FtpReturnCode.EnteringPassiveMode)
            {
                throw FtpException("Could not upload file", response);
            }

            string responseMessage  = response.Message;
            int    openBracketIndex = responseMessage.IndexOf('(');

            if (openBracketIndex < 0)
            {
                throw new FtpException("Unexpected response message: '{0}'".Fmt(responseMessage));
            }

            int start             = openBracketIndex + 1;
            int closeBracketIndex = responseMessage.IndexOf(')', start);

            if (closeBracketIndex < 0)
            {
                throw new FtpException("Unexpected response message: '{0}'".Fmt(responseMessage));
            }

            int length = closeBracketIndex - start;

            string[] splits = responseMessage.Substring(start, length).Split(',');

            Contract.Assert(splits.Length >= 6);

            byte[] addressBytes = new byte[4];

            for (int i = 0; i < 4; i++)
            {
                addressBytes[i] = byte.Parse(splits[i], CultureInfo.InvariantCulture);
            }

            int dataChannelPort = int.Parse(splits[4], CultureInfo.InvariantCulture) * 256
                                  + int.Parse(splits[5], CultureInfo.InvariantCulture);
            IFtpChannel dataChannel = ftpChannelFactory.CreateChannel();

            dataChannel.Connect(addressBytes, dataChannelPort);
            return(dataChannel);
        }
Пример #8
0
        public IList <string> ListFiles(string directory)
        {
            OpenDataChannel();

            FtpServerResponse response = SendCommand("NLST {0}", directory);

            if (response.ReturnCode
                != (int)FtpReturnCode.DataConnectionAlreadyOpen &&
                response.ReturnCode != (int)FtpReturnCode.FileStatusOk)
            {
                throw FtpException("Could not list files", response);
            }

            throw new NotImplementedException();

            //using (MemoryStream stream = new MemoryStream())
            //{
            //    dataChannel.Receive(stream);
            //    string text = Encoding.ASCII.GetString(stream.ToArray());
            //    return text.SplitIntoLines();
            //}
        }