Exemplo n.º 1
0
        private void RNTO(FtpCommand cmd)
        {
            CheckLogin();

            try
            {
                if (cmd.Parameters.Count == 0)
                {
                    throw new SyntaxException();
                }

                if (string.IsNullOrEmpty(renamePath))
                {
                    throw new BadSeqCommandsException();
                }

                bool   isFile;
                string destPath = GetLocalPath(cmd.Parameters[0], FtpOption.Create, out isFile);

                try
                {
                    if (Directory.Exists(renamePath))
                    {
                        Directory.Move(renamePath, destPath);
                    }
                    else
                    {
                        File.Move(renamePath, destPath);
                    }
                }
                catch (System.Exception)
                {
                    throw new InternalException("rename path");
                }

                Response("250 Rename successful.");
            }
            finally
            {
                renamePath = null;
            }
        }
Exemplo n.º 2
0
        private void REST(FtpCommand ftpCmd)
        {
            CheckLogin();

            if (ftpCmd.Parameters.Count == 0)
            {
                throw new SyntaxException();
            }

            long temp = long.Parse(ftpCmd.Parameters[0]);

            if (temp < 0)
            {
                throw new SyntaxException();
            }

            restartPos = temp;

            Response(string.Format("350 Restarting at {0}.", restartPos));
        }
Exemplo n.º 3
0
        private void SIZE(FtpCommand cmd)
        {
            CheckLogin();

            if (cmd.Parameters.Count == 0)
            {
                throw new SyntaxException();
            }

            string file = cmd.Parameters[0];

            if (string.IsNullOrEmpty(file))
            {
                throw new SyntaxException();
            }
            bool   isFile;
            string localPath = GetLocalPath(file, FtpOption.List, out isFile);
            long   length;

            try
            {
                if (isFile)
                {
                    FileInfo info = new FileInfo(localPath);
                    length = info.Length;
                    Response("213 " + info.Length.ToString());
                }
                else
                {
                    throw new FileNotFoundException(file);
                }
            }
            catch (FtpException)
            {
                throw;
            }
            catch (System.Exception e)
            {
                throw new InternalException(e.Message);
            }
        }
Exemplo n.º 4
0
        private void USER(FtpCommand cmd)
        {
            if (cmd.Parameters.Count == 0)
            {
                throw new SyntaxException();
            }

            //重新登陆,取消以前的状态
            Statue   = FtpSessionStatue.NotLogin;
            UserName = cmd.Parameters[0];

            if (ftpServer.ValidateUser(this))
            {
                Statue = FtpSessionStatue.Wait;
                Response("230 User successfully logged in.");
            }
            else
            {
                Response("331 Password required for " + UserName);
            }
        }
Exemplo n.º 5
0
        private void OPTS(FtpCommand ftpCmd)
        {
            CheckLogin();

            if (ftpCmd.Parameters.Count == 0)
            {
                throw new SyntaxException();
            }

            if (ftpCmd.Parameters[0] == "utf8 on")
            {
                Encoding = Encoding.UTF8;
                Response("200 UTF enabled mode.");
            }
            else if (ftpCmd.Parameters[0] == "utf8 off")
            {
                Encoding = Encoding.Default;
                Response("200 ASCII enabled mode.");
            }
            else
            {
                throw new SyntaxException();
            }
        }
Exemplo n.º 6
0
 private void PWD(FtpCommand ftpCmd)
 {
     CheckLogin();
     Response("257 \"" + CurrentDir + "\" is current directory.");
 }
Exemplo n.º 7
0
 private void PASV(FtpCommand ftpCmd)
 {
     CheckLogin();
     CreateDataConnection();
 }
Exemplo n.º 8
0
 private void CWD(FtpCommand ftpCmd)
 {
     CheckLogin();
     ChangeDir(ftpCmd);
 }
Exemplo n.º 9
0
 private void LIST(FtpCommand ftpCmd)
 {
     CheckLogin();
     SendPathList(ftpCmd);
 }
Exemplo n.º 10
0
 private void RETR(FtpCommand cmd)
 {
     CheckLogin();
     SendFile(cmd);
 }
Exemplo n.º 11
0
 private void QUIT(FtpCommand cmd)
 {
     CheckLogin();
     this.Statue = FtpSessionStatue.NotLogin;
     Response("220 " + ftpServer.ByeMessage);
 }
Exemplo n.º 12
0
 private void SYST(FtpCommand cmd)
 {
     CheckLogin();
     Response("215 UNIX Type: L8");
 }
Exemplo n.º 13
0
 private void NOOP(FtpCommand cmd)
 {
     CheckLogin();
     Response("200 NOOP Command Successful.");
 }
Exemplo n.º 14
0
 private void HELP(FtpCommand cmd)
 {
     CheckLogin();
     Response("500 No Help Available.");
 }
Exemplo n.º 15
0
 private void FEAT(FtpCommand cmd)
 {
     Response("211-Features:\r\n MDTM\r\n SIZE\r\n REST STREAM\r\n PASV\r\n211 End");
 }
Exemplo n.º 16
0
        /// <summary>
        /// 发送目录内容列表。(已经建立了数据连接的基础上)
        /// </summary>
        /// <param name="ftpCmd"></param>
        internal void SendPathList(FtpCommand ftpCmd)
        {
            CheckDataConnExist();

            try
            {
                Statue = FtpSessionStatue.List;

                if (ftpCmd.Parameters.Count == 0)
                {
                    ftpCmd.Parameters.Add("");
                }

                string virtualPath = ftpCmd.Parameters[0].TrimEnd('*');

                if (virtualPath.StartsWith("-"))
                {
                    //TODO:参数检查功能
                    virtualPath = "";
                }

                if (virtualPath == "")
                {
                    virtualPath = CurrentDir;
                }
                bool isFile;

                string localPath = GetLocalPath(virtualPath, FtpOption.List, out isFile);

                StringBuilder sb = new StringBuilder();

                //列举目录
                if (!isFile)
                {
                    Response("150 Opening ASCII mode data connection for directory list.");

                    DirectoryInfo localDir = new DirectoryInfo(localPath);
                    foreach (DirectoryInfo dir in localDir.GetDirectories())
                    {
                        WriteFileInfo(sb, dir.Name, 0, dir.LastWriteTime, false);
                    }

                    //列举文件
                    foreach (FileInfo file in localDir.GetFiles())
                    {
                        WriteFileInfo(sb, file.Name, file.Length, file.LastWriteTime, true);
                    }
                }
                else
                {
                    Response("150 Opening ASCII mode data connection for directory list.");
                    FileInfo file = new FileInfo(localPath);
                    WriteFileInfo(sb, file.Name, file.Length, file.LastWriteTime, true);
                }

                //NetDebuger.PrintDebugMessage(this, sb.ToString());
                dataConn.Send(new DataBlock(Encoding.GetBytes(sb.ToString())), opertionTimeout);
                Response("226 Transfer complete.");
            }
            catch (System.IO.IOException e)
            {
                throw new InternalException(e.Message);
            }
            finally
            {
                CloseDataConn();
                Statue = FtpSessionStatue.Wait;
            }
        }