Esempio n. 1
0
 /// <summary>
 /// Processes the list.
 /// </summary>
 /// <param name="session">The handle.</param>
 /// <param name="path">The path.</param>
 /// <returns></returns>
 private static IList <string> ProcessList(FtpSession session, FtpPath path)
 {
     // Open data channel
     using (var dataStream = OpenDataStream(session, FtpTransferMode.Binary, FtpStreamMode.Read))
     {
         // then command is sent
         var reply = session.Expect(session.SendCommand("LIST", session.Connection.Client.GetPlatform(session).EscapePath(path.ToString())), 125, 150, 425);
         if (!reply.Code.IsSuccess)
         {
             dataStream.Abort();
             session.ThrowException(reply);
         }
         using (var streamReader = new StreamReader(dataStream.Validated(), session.Connection.Encoding))
         {
             var list = new List <string>();
             for (;;)
             {
                 var line = streamReader.ReadLine();
                 if (line == null)
                 {
                     break;
                 }
                 list.Add(line);
             }
             return(list);
         }
     }
 }
Esempio n. 2
0
 /// <summary>
 /// Gets the platform.
 /// </summary>
 /// <param name="session">The FTP session.</param>
 /// <returns></returns>
 public FtpPlatform GetPlatform(FtpSession session)
 {
     if (_platform == null)
     {
         _platform = GetFtpPlatform(GetServerType(session), GetSystem(session));
     }
     return(_platform);
 }
Esempio n. 3
0
 /// <summary>
 /// Processes the specified action with a session.
 /// </summary>
 /// <typeparam name="TResult">The type of the ret.</typeparam>
 /// <param name="action">The action.</param>
 /// <param name="session">An existing session or null to create a new one.</param>
 /// <returns></returns>
 public TResult Process <TResult>(Func <FtpSession, TResult> action, FtpSession session = null)
 {
     if (session != null)
     {
         return(action(session));
     }
     using (var newSession = Session())
         return(action(newSession));
 }
Esempio n. 4
0
 /// <summary>
 /// Gets the system.
 /// </summary>
 /// <param name="session">The FTP session.</param>
 /// <returns></returns>
 public string GetSystem(FtpSession session)
 {
     if (_system == null)
     {
         var systemReply = Process(s => s.Expect(s.SendCommand("SYST"), 215), session);
         _system = systemReply.Lines[0];
     }
     return(_system);
 }
Esempio n. 5
0
 /// <summary>
 /// Processes the specified action with a session.
 /// </summary>
 /// <param name="action">The action.</param>
 /// <param name="session">An existing session or null to create a new one.</param>
 public void Process(Action <FtpSession> action, FtpSession session = null)
 {
     if (session != null)
     {
         action(session);
     }
     else
     {
         using (var newSession = Session())
             action(newSession);
     }
 }
Esempio n. 6
0
        /// <summary>
        /// Processes the stor.
        /// </summary>
        /// <param name="session">The handle.</param>
        /// <param name="path">The path.</param>
        /// <param name="mode">The mode.</param>
        /// <returns></returns>
        /// <exception cref="IOException"></exception>
        private static Stream ProcessStor(FtpSession session, FtpPath path, FtpTransferMode mode = FtpTransferMode.Binary)
        {
            var stream = OpenDataStream(session, mode, FtpStreamMode.Write);
            var reply  = session.Expect(session.SendCommand("STOR", path.ToString()), 125, 150, 425, 550);

            if (!reply.Code.IsSuccess)
            {
                stream.Abort();
                session.ThrowException(reply);
                return(null);
            }
            return(stream.Validated());
        }
Esempio n. 7
0
        /// <summary>
        /// Loads the features.
        /// </summary>
        /// <param name="session">The session.</param>
        /// <returns></returns>
        private static FtpServerFeatures LoadServerFeatures(FtpSession session)
        {
            var featuresReply = session.SendCommand("FEAT");

            if (featuresReply.Code == 211)
            {
                var featuresQuery = from line in featuresReply.Lines.Skip(1).Take(featuresReply.Lines.Length - 2)
                                    select line.Trim();

                return(new FtpServerFeatures(featuresQuery));
            }
            return(new FtpServerFeatures(new string[0]));
        }
Esempio n. 8
0
        private static FtpEntry ProcessGetEntry(FtpSession session, FtpPath path)
        {
            session.CheckProtection(FtpProtection.ControlChannel);
            var reply = session.SendCommand("STAT", session.Connection.Client.GetPlatform(session).EscapePath(path.ToString()));

            if ((reply.Code != 213 && reply.Code != 211) || reply.Lines.Length <= 2)
            {
                return(null);
            }
            // now get the type: the first entry is "." for folders or file itself for files/links
            var entry = EnumerateEntries(session.Connection.Client, path, reply.Lines.Skip(1), ignoreSpecialEntries: false).First();

            // actually, it's always good here
            return(new FtpEntry(path, entry.Size, entry.Type, entry.Date, entry.Target));
        }
Esempio n. 9
0
 /// <summary>
 /// Gets the features.
 /// </summary>
 /// <param name="session">The session.</param>
 /// <returns></returns>
 private FtpServerFeatures GetServerFeatures(FtpSession session)
 {
     if (_serverFeatures == null)
     {
         if (session != null)
         {
             _serverFeatures = LoadServerFeatures(session);
         }
         else
         {
             using (var newSession = Session())
                 _serverFeatures = LoadServerFeatures(newSession);
         }
     }
     return(_serverFeatures);
 }
Esempio n. 10
0
 /// <summary>
 /// Gets the type of the server.
 /// </summary>
 /// <param name="session">The FTP session.</param>
 /// <returns></returns>
 private FtpServerType GetServerType(FtpSession session)
 {
     if (!_serverType.HasValue)
     {
         var system = GetSystem(session);
         if (system.StartsWith("unix", StringComparison.InvariantCultureIgnoreCase))
         {
             _serverType = FtpServerType.Unix;
         }
         else if (system.StartsWith("windows", StringComparison.InvariantCultureIgnoreCase))
         {
             _serverType = FtpServerType.Windows;
         }
         else
         {
             _serverType = FtpServerType.Unknown;
         }
     }
     return(_serverType.Value);
 }
Esempio n. 11
0
        /// <summary>
        /// Opens a data stream.
        /// </summary>
        /// <param name="session">The session handle.</param>
        /// <param name="transferMode">The mode.</param>
        /// <param name="streamMode">The stream mode.</param>
        /// <returns></returns>
        public static FtpStream OpenDataStream(this FtpSession session, FtpTransferMode transferMode, FtpStreamMode streamMode)
        {
            var client = session.Connection.Client;

            return(session.OpenDataStream(client.Passive, client.ConnectTimeout, client.ReadWriteTimeout, transferMode, streamMode));
        }
Esempio n. 12
0
 /// <summary>
 /// Determines whether the specified feature has a requested feature.
 /// </summary>
 /// <param name="feature">The feature.</param>
 /// <param name="session">The session.</param>
 /// <returns>
 ///   <c>true</c> if the specified feature has feature; otherwise, <c>false</c>.
 /// </returns>
 internal bool HasServerFeature(string feature, FtpSession session)
 {
     return(GetServerFeatures(session).HasFeature(feature));
 }
Esempio n. 13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FtpSessionHandle"/> class.
 /// </summary>
 /// <param name="ftpSession">The FTP session.</param>
 internal FtpSessionHandle(FtpSession ftpSession)
 {
     Session = ftpSession;
     Session.AddReference();
 }