Exemple #1
0
        private void STOR(FtpCommand cmd)
        {
            CheckLogin();
            if (cmd.Parameters.Count == 0)
            {
                throw new SyntaxException();
            }

            CheckDataConnExist();

            restartPos = 0;

            try
            {
                Statue = FtpSessionStatue.Upload;
                bool   isFile;
                string localPath = GetLocalPath(cmd.Parameters[0], FtpOption.Upload, out isFile);
                dataConn.MaxLengthOfUpload = user.MaxUploadFileLength;
                dataConn.RecvFile          = new FileStream(localPath, FileMode.Create);
                Response("150 Opening BINARY mode data connection for file transfer.");

                if (dataConn.ReceiveFile(localPath, restartPos))
                {
                    Response("226 Transfer complete.");
                }
                else
                {
                    NetDebuger.PrintErrorMessage(this, dataConn.AsyncHelper.Exception.Message);
                    Response("426 Connection closed; transfer aborted.");
                }
            }
            catch (FtpException)
            {
                throw;
            }
            catch (Exception e)
            {
                NetDebuger.PrintErrorMessage(this, e.ToString());
                throw new InternalException("store file");
            }
            finally
            {
                CloseDataConn();
                Statue = FtpSessionStatue.Wait;
            }
        }
Exemple #2
0
        /// <summary>
        /// 分发FTP请求
        /// </summary>
        /// <param name="cmdText"></param>
        internal void SwitchFtpRequest(string cmdText)
        {
            NetDebuger.PrintErrorMessage(this, cmdText);
            if (string.IsNullOrEmpty(cmdText))
            {
                return;
            }

            FtpCommand cmd;

            int index = cmdText.IndexOf(' '); //把命令和参数部分分开

            if (index != -1)
            {
                //把命令转成大写
                cmd = new FtpCommand(cmdText.Substring(0, index).ToUpper());
                cmd.Parameters.Add(cmdText.Substring(index + 1));
            }
            else
            {
                //把命令转成大写
                cmd = new FtpCommand(cmdText.ToUpper());
            }

            //会话重新开始计时
            TimeCounter.Reset();

            try
            {
                if (!ftpHandlers.ContainsKey(cmd.Command))
                {
                    throw new CommandNotImplementedException();
                }

                //分发各个命令
                ftpHandlers[cmd.Command].Invoke(cmd);
            }
            catch (FtpException e)
            {
                Response(e.Message);
            }
            catch (Exception e)
            {
                NetDebuger.PrintErrorMessage(this, e.ToString());
            }
        }
Exemple #3
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;
            }
        }
Exemple #4
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));
        }
Exemple #5
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);
            }
        }
Exemple #6
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);
            }
        }
Exemple #7
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();
            }
        }
Exemple #8
0
 private void CWD(FtpCommand ftpCmd)
 {
     CheckLogin();
     ChangeDir(ftpCmd);
 }
Exemple #9
0
 private void LIST(FtpCommand ftpCmd)
 {
     CheckLogin();
     SendPathList(ftpCmd);
 }
Exemple #10
0
 private void RETR(FtpCommand cmd)
 {
     CheckLogin();
     SendFile(cmd);
 }
Exemple #11
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;
            }
        }
Exemple #12
0
 private void SYST(FtpCommand cmd)
 {
     CheckLogin();
     Response("215 UNIX Type: L8");
 }
Exemple #13
0
 private void NOOP(FtpCommand cmd)
 {
     CheckLogin();
     Response("200 NOOP Command Successful.");
 }
Exemple #14
0
 private void HELP(FtpCommand cmd)
 {
     CheckLogin();
     Response("500 No Help Available.");
 }
Exemple #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");
 }
Exemple #16
0
 private void PASV(FtpCommand ftpCmd)
 {
     CheckLogin();
     CreateDataConnection();
 }
Exemple #17
0
 private void PWD(FtpCommand ftpCmd)
 {
     CheckLogin();
     Response("257 \"" + CurrentDir + "\" is current directory.");
 }
Exemple #18
0
 private void QUIT(FtpCommand cmd)
 {
     CheckLogin();
     this.Statue = FtpSessionStatue.NotLogin;
     Response("220 " + ftpServer.ByeMessage);
 }