Esempio n. 1
0
        void ProcessRequest()
        {
            if (State == RequestState.Scheduled)
            {
                ftpResponse = new FtpWebResponse(requestUri, method, keepAlive);

                try {
                    ProcessMethod();
                    //State = RequestState.Finished;
                    //finalResponse = ftpResponse;
                    asyncResult.SetCompleted(false, ftpResponse);
                }
                catch (Exception e) {
                    State = RequestState.Error;
                    SetCompleteWithError(e);
                }
            }
            else
            {
                if (InProgress())
                {
                    FtpStatus status = GetResponseStatus();

                    ftpResponse.UpdateStatus(status);

                    if (ftpResponse.IsFinal())
                    {
                        State = RequestState.Finished;
                    }
                }

                asyncResult.SetCompleted(false, ftpResponse);
            }
        }
Esempio n. 2
0
        static string GetInitialPath(FtpStatus status)
        {
            int s = (int)status.StatusCode;

            if (s < 200 || s > 300 || status.StatusDescription.Length <= 4)
            {
                throw new WebException("Error getting current directory: " + status.StatusDescription, null,
                                       WebExceptionStatus.UnknownError, null);
            }

            string msg = status.StatusDescription.Substring(4);

            if (msg [0] == '"')
            {
                int next_quote = msg.IndexOf('\"', 1);
                if (next_quote == -1)
                {
                    throw new WebException("Error getting current directory: PWD -> " + status.StatusDescription, null,
                                           WebExceptionStatus.UnknownError, null);
                }

                msg = msg.Substring(1, next_quote - 1);
            }

            if (!msg.EndsWith("/"))
            {
                msg += "/";
            }
            return(msg);
        }
Esempio n. 3
0
        FtpStatus SendCommand(bool waitResponse, string command, params string [] parameters)
        {
            byte [] cmd;
            string  commandString = command;

            if (parameters.Length > 0)
            {
                commandString += " " + String.Join(" ", parameters);
            }

            commandString += EOL;
            cmd            = Encoding.ASCII.GetBytes(commandString);
            try {
                controlStream.Write(cmd, 0, cmd.Length);
            } catch (IOException) {
                //controlStream.Close ();
                return(new FtpStatus(FtpStatusCode.ServiceNotAvailable, "Write failed"));
            }

            if (!waitResponse)
            {
                return(null);
            }

            FtpStatus result = GetResponseStatus();

            if (ftpResponse != null)
            {
                ftpResponse.UpdateStatus(result);
            }
            return(result);
        }
Esempio n. 4
0
        Exception CreateExceptionFromResponse(FtpStatus status)
        {
            FtpWebResponse ftpResponse = new FtpWebResponse(requestUri, method, status);

            WebException exc = new WebException("Server returned an error: " + status.StatusDescription,
                                                null, WebExceptionStatus.ProtocolError, ftpResponse);

            return(exc);
        }
Esempio n. 5
0
        void OpenControlConnection()
        {
            Exception exception = null;
            Socket    sock      = null;

            foreach (IPAddress address in hostEntry.AddressList)
            {
                sock = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                IPEndPoint remote = new IPEndPoint(address, requestUri.Port);

                if (!ServicePoint.CallEndPointDelegate(sock, remote))
                {
                    sock.Close();
                    sock = null;
                }
                else
                {
                    try {
                        sock.Connect(remote);
                        localEndPoint = (IPEndPoint)sock.LocalEndPoint;
                        break;
                    } catch (SocketException exc) {
                        exception = exc;
                        sock.Close();
                        sock = null;
                    }
                }
            }

            // Couldn't connect to any address
            if (sock == null)
            {
                throw new WebException("Unable to connect to remote server", exception,
                                       WebExceptionStatus.UnknownError, ftpResponse);
            }

            controlStream = new NetworkStream(sock);
            controlReader = new StreamReader(controlStream, Encoding.ASCII);

            State = RequestState.Authenticating;

            Authenticate();
            FtpStatus status = SendCommand("OPTS", "utf8", "on");

            if ((int)status.StatusCode < 200 || (int)status.StatusCode > 300)
            {
                dataEncoding = Encoding.Default;
            }
            else
            {
                dataEncoding = Encoding.UTF8;
            }

            status       = SendCommand(WebRequestMethods.Ftp.PrintWorkingDirectory);
            initial_path = GetInitialPath(status);
        }
Esempio n. 6
0
 void SetType()
 {
     if (binary)
     {
         FtpStatus status = SendCommand(TypeCommand, DataType);
         if ((int)status.StatusCode < 200 || (int)status.StatusCode >= 300)
         {
             throw CreateExceptionFromResponse(status);
         }
     }
 }
Esempio n. 7
0
        private void InitiateSecureConnection(ref NetworkStream stream)
        {
            FtpStatus status = SendCommand(AuthCommand, "TLS");

            if (status.StatusCode != FtpStatusCode.ServerWantsSecureSession)
            {
                throw CreateExceptionFromResponse(status);
            }

            ChangeToSSLSocket(ref stream);
        }
Esempio n. 8
0
        // Here we could also get a server error, so be cautious
        internal void SetTransferCompleted()
        {
            if (InFinalState())
            {
                return;
            }

            State = RequestState.Finished;
            FtpStatus status = GetResponseStatus();

            ftpResponse.UpdateStatus(status);
            if (!keepAlive)
            {
                CloseConnection();
            }
        }
Esempio n. 9
0
        void DownloadData()
        {
            State = RequestState.OpeningData;

            // Handle content offset
            if (offset > 0)
            {
                FtpStatus status = SendCommand(RestCommand, offset.ToString());

                if (status.StatusCode != FtpStatusCode.FileCommandPending)
                {
                    throw CreateExceptionFromResponse(status);
                }
            }

            OpenDataConnection();

            State = RequestState.TransferInProgress;
            ftpResponse.Stream = new FtpDataStream(this, dataSocket, true);
        }
Esempio n. 10
0
		internal void UpdateStatus (FtpStatus status) {
			statusCode = status.StatusCode;
			statusDescription = status.StatusDescription;
		}
Esempio n. 11
0
        void Authenticate()
        {
            string username = null;
            string password = null;
            string domain   = null;

            if (credentials != null)
            {
                username = credentials.UserName;
                password = credentials.Password;
                domain   = credentials.Domain;
            }

            if (username == null)
            {
                username = "******";
            }
            if (password == null)
            {
                password = "******";
            }
            if (!string.IsNullOrEmpty(domain))
            {
                username = domain + '\\' + username;
            }

            // Connect to server and get banner message
            FtpStatus status = GetResponseStatus();

            ftpResponse.BannerMessage = status.StatusDescription;

            if (EnableSsl)
            {
                InitiateSecureConnection(ref controlStream);
                controlReader = new StreamReader(controlStream, Encoding.ASCII);
            }

            if (status.StatusCode != FtpStatusCode.SendUserCommand)
            {
                throw CreateExceptionFromResponse(status);
            }

            status = SendCommand(UserCommand, username);

            switch (status.StatusCode)
            {
            case FtpStatusCode.SendPasswordCommand:
                status = SendCommand(PasswordCommand, password);
                if (status.StatusCode != FtpStatusCode.LoggedInProceed)
                {
                    throw CreateExceptionFromResponse(status);
                }
                break;

            case FtpStatusCode.LoggedInProceed:
                break;

            default:
                throw CreateExceptionFromResponse(status);
            }

            ftpResponse.WelcomeMessage = status.StatusDescription;
            ftpResponse.UpdateStatus(status);
        }
Esempio n. 12
0
 internal FtpWebResponse(FtpWebRequest request, Uri uri, string method, FtpStatus status) :
     this(request, uri, method, status.StatusCode, status.StatusDescription)
 {
 }
Esempio n. 13
0
 internal void UpdateStatus(FtpStatus status)
 {
     statusCode        = status.StatusCode;
     statusDescription = status.StatusDescription;
 }
Esempio n. 14
0
		internal FtpWebResponse (FtpWebRequest request, Uri uri, string method, FtpStatus status) :
			this (request, uri, method, status.StatusCode, status.StatusDescription)
		{
		}
Esempio n. 15
0
		Exception CreateExceptionFromResponse (FtpStatus status)
		{
			FtpWebResponse ftpResponse = new FtpWebResponse (this, requestUri, method, status);
			
			WebException exc = new WebException ("Server returned an error: " + status.StatusDescription, 
				null, WebExceptionStatus.ProtocolError, ftpResponse);
			return exc;
		}
Esempio n. 16
0
		static string GetInitialPath (FtpStatus status)
		{
			int s = (int) status.StatusCode;
			if (s < 200 || s > 300 || status.StatusDescription.Length <= 4)
				throw new WebException ("Error getting current directory: " + status.StatusDescription, null,
						WebExceptionStatus.UnknownError, null);

			string msg = status.StatusDescription.Substring (4);
			if (msg [0] == '"') {
				int next_quote = msg.IndexOf ('\"', 1);
				if (next_quote == -1)
					throw new WebException ("Error getting current directory: PWD -> " + status.StatusDescription, null,
								WebExceptionStatus.UnknownError, null);

				msg = msg.Substring (1, next_quote - 1);
			}

			if (!msg.EndsWith ("/"))
				msg += "/";
			return msg;
		}
Esempio n. 17
0
		void Authenticate ()
		{
			string username = null;
			string password = null;
			string domain = null;

			if (credentials != null) {
				username = credentials.UserName;
				password = credentials.Password;
				domain = credentials.Domain;
			}

			if (username == null)
				username = "******";
			if (password == null)
				password = "******";
			if (!string.IsNullOrEmpty (domain))
				username = domain + '\\' + username;

			// Connect to server and get banner message
			FtpStatus status = GetResponseStatus ();
			ftpResponse.BannerMessage = status.StatusDescription;

			if (EnableSsl) {
				InitiateSecureConnection (ref controlStream);
				controlReader = new StreamReader (controlStream, Encoding.ASCII);
				status = SendCommand ("PBSZ", "0");
				int st = (int) status.StatusCode;
				if (st < 200 || st >= 300)
					throw CreateExceptionFromResponse (status);
				// TODO: what if "PROT P" is denied by the server? What does MS do?
				status = SendCommand ("PROT", "P");
				st = (int) status.StatusCode;
				if (st < 200 || st >= 300)
					throw CreateExceptionFromResponse (status);

				status = new FtpStatus (FtpStatusCode.SendUserCommand, "");
			}
			
			if (status.StatusCode != FtpStatusCode.SendUserCommand)
				throw CreateExceptionFromResponse (status);

			status = SendCommand (UserCommand, username);

			switch (status.StatusCode) {
			case FtpStatusCode.SendPasswordCommand:
				status = SendCommand (PasswordCommand, password);
				if (status.StatusCode != FtpStatusCode.LoggedInProceed)
					throw CreateExceptionFromResponse (status);
				break;
			case FtpStatusCode.LoggedInProceed:
				break;
			default:
				throw CreateExceptionFromResponse (status);
			}

			ftpResponse.WelcomeMessage = status.StatusDescription;
			ftpResponse.UpdateStatus (status);
		}