Beispiel #1
0
        public static IPEndPoint ParseIPEndPoint(FtpReply ftpReply)
        {
            if (!ftpReply.Success)
            {
                return(null);
            }
            var matches = Regex.Match(ftpReply.ResponseMessage,
                                      "([0-9]+),([0-9]+),([0-9]+),([0-9]+),([0-9]+),([0-9]+)");

            if (!matches.Success)
            {
                return(null);
            }
            if (matches.Groups.Count != 7)
            {
                return(null);
            }

            var ipAddress = ParseIPAddress(from index in Enumerable.Range(1,
                                                                          4)
                                           let octet = matches.Groups[index].Value
                                                       select octet);
            var p1   = matches.Groups[5].Value;
            var p2   = matches.Groups[6].Value;
            var port = ParsePassivePort(p1,
                                        p2);
            var ipEndPoint = new IPEndPoint(ipAddress,
                                            port);

            return(ipEndPoint);
        }
Beispiel #2
0
        private bool TryCreateDirectoryInternal(string path,
                                                out FtpReply ftpReply)
        {
            Contract.Requires(!string.IsNullOrWhiteSpace(path));

            var controlComplexSocket = this._controlComplexSocket;
            {
                var success = controlComplexSocket.Send(string.Format("MKD {0}",
                                                                      path),
                                                        this.Encoding,
                                                        this.SendTimeout);
                if (!success)
                {
                    ftpReply = null;
                    return(false);
                }
            }
            {
                ftpReply = controlComplexSocket.Receive(this.Encoding,
                                                        this.ReceiveTimeout);
                var success = ftpReply.Success;

                return(success);
            }
        }
Beispiel #3
0
        public static FtpReply ParseFtpReply(string data)
        {
            if (string.IsNullOrEmpty(data))
            {
                return(FtpReply.Failed);
            }

            var ftpResponseType    = FtpResponseType.None;
            var messages           = new List <string>();
            var stringResponseCode = string.Empty;
            var responseCode       = 0;
            var responseMessage    = string.Empty;

            var lines = data.Split(Environment.NewLine.ToCharArray(),
                                   StringSplitOptions.RemoveEmptyEntries);

            foreach (var line in lines)
            {
                var match = Regex.Match(line,
                                        FtpReply.RegularExpressionForParsing);
                if (match.Success)
                {
                    if (match.Groups.Count > 1)
                    {
                        stringResponseCode = match.Groups[1].Value;
                    }
                    if (match.Groups.Count > 2)
                    {
                        responseMessage = match.Groups[2].Value;
                    }
                    if (!string.IsNullOrWhiteSpace(stringResponseCode))
                    {
                        var firstCharacter     = stringResponseCode.First();
                        var character          = firstCharacter.ToString(CultureInfo.InvariantCulture);
                        var intFtpResponseType = int.Parse(character);
                        ftpResponseType = (FtpResponseType)intFtpResponseType;
                        responseCode    = int.Parse(stringResponseCode);
                    }
                }
                messages.Add(line);
            }

            var ftpReply = new FtpReply(ftpResponseType,
                                        responseCode,
                                        responseMessage,
                                        messages,
                                        data);

            return(ftpReply);
        }
        internal FtpReply Receive(Encoding encoding,
                                  TimeSpan timeout)
        {
            Contract.Requires(encoding != null);

            using (var socketAsyncEventArgs = this.GetSocketAsyncEventArgs(timeout))
            {
                var responseBuffer = this.GetReceiveBuffer();
                socketAsyncEventArgs.SetBuffer(responseBuffer,
                                               0,
                                               responseBuffer.Length);
                var ftpResponseType    = FtpResponseType.None;
                var messages           = new List <string>();
                var stringResponseCode = string.Empty;
                var responseCode       = 0;
                var responseMessage    = string.Empty;

                while (this.ReceiveChunk(socketAsyncEventArgs))
                {
                    var data = socketAsyncEventArgs.GetData(encoding);
                    if (string.IsNullOrWhiteSpace(data))
                    {
                        break;
                    }
                    var lines = data.Split(Environment.NewLine.ToCharArray(),
                                           StringSplitOptions.RemoveEmptyEntries);
                    foreach (var line in lines)
                    {
                        var match = Regex.Match(line,
                                                @"^(\d{3})\s(.*)$");
                        if (match.Success)
                        {
                            if (match.Groups.Count > 1)
                            {
                                stringResponseCode = match.Groups[1].Value;
                            }
                            if (match.Groups.Count > 2)
                            {
                                responseMessage = match.Groups[2].Value;
                            }
                            if (!string.IsNullOrWhiteSpace(stringResponseCode))
                            {
                                var firstCharacter     = stringResponseCode.First();
                                var currentCulture     = Thread.CurrentThread.CurrentCulture;
                                var character          = firstCharacter.ToString(currentCulture);
                                var intFtpResponseType = Convert.ToInt32(character);
                                ftpResponseType = (FtpResponseType)intFtpResponseType;
                                responseCode    = Int32.Parse(stringResponseCode);
                            }
                        }
                        else
                        {
                            messages.Add(line);
                        }
                    }

                    var finished = ftpResponseType != FtpResponseType.None;
                    if (finished)
                    {
                        break;
                    }
                }

                var complexResult = new FtpReply(ftpResponseType,
                                                 responseCode,
                                                 responseMessage,
                                                 messages);

                return(complexResult);
            }
        }