2 // System.Net.FtpWebRequest.cs
5 // Carlos Alberto Cortez (calberto.cortez@gmail.com)
7 // (c) Copyright 2006 Novell, Inc. (http://www.novell.com)
13 using System.Net.Sockets;
15 using System.Threading;
16 using System.Net.Cache;
17 using System.Security.Cryptography.X509Certificates;
23 public sealed class FtpWebRequest : WebRequest
26 string file_name; // By now, used for upload
27 ServicePoint servicePoint;
29 NetworkStream controlStream;
30 StreamReader controlReader;
31 NetworkCredential credentials;
32 IPHostEntry hostEntry;
33 IPEndPoint localEndPoint;
36 int rwTimeout = 300000;
39 bool enableSsl = false;
40 bool usePassive = true;
41 bool keepAlive = false;
42 string method = WebRequestMethods.Ftp.DownloadFile;
44 object locker = new object ();
46 RequestState requestState = RequestState.Before;
47 FtpAsyncResult asyncResult;
48 FtpWebResponse ftpResponse;
52 const string ChangeDir = "CWD";
53 const string UserCommand = "USER";
54 const string PasswordCommand = "PASS";
55 const string TypeCommand = "TYPE";
56 const string PassiveCommand = "PASV";
57 const string PortCommand = "PORT";
58 const string AbortCommand = "ABOR";
59 const string AuthCommand = "AUTH";
60 const string RestCommand = "REST";
61 const string RenameFromCommand = "RNFR";
62 const string RenameToCommand = "RNTO";
63 const string QuitCommand = "QUIT";
64 const string EOL = "\r\n"; // Special end of line
80 static readonly string [] supportedCommands = new string [] {
81 WebRequestMethods.Ftp.AppendFile, // APPE
82 WebRequestMethods.Ftp.DeleteFile, // DELE
83 WebRequestMethods.Ftp.ListDirectoryDetails, // LIST
84 WebRequestMethods.Ftp.GetDateTimestamp, // MDTM
85 WebRequestMethods.Ftp.MakeDirectory, // MKD
86 WebRequestMethods.Ftp.ListDirectory, // NLST
87 WebRequestMethods.Ftp.PrintWorkingDirectory, // PWD
88 WebRequestMethods.Ftp.Rename, // RENAME
89 WebRequestMethods.Ftp.DownloadFile, // RETR
90 WebRequestMethods.Ftp.RemoveDirectory, // RMD
91 WebRequestMethods.Ftp.GetFileSize, // SIZE
92 WebRequestMethods.Ftp.UploadFile, // STOR
93 WebRequestMethods.Ftp.UploadFileWithUniqueName // STUR
96 internal FtpWebRequest (Uri uri)
98 this.requestUri = uri;
99 this.proxy = GlobalProxySelection.Select;
102 static Exception GetMustImplement ()
104 return new NotImplementedException ();
108 public X509CertificateCollection ClientCertificates
111 throw GetMustImplement ();
114 throw GetMustImplement ();
119 public override string ConnectionGroupName
122 throw GetMustImplement ();
125 throw GetMustImplement ();
129 public override string ContentType {
131 throw new NotSupportedException ();
134 throw new NotSupportedException ();
138 public override long ContentLength {
147 public long ContentOffset {
152 CheckRequestStarted ();
154 throw new ArgumentOutOfRangeException ();
160 public override ICredentials Credentials {
165 CheckRequestStarted ();
167 throw new ArgumentNullException ();
168 if (!(value is NetworkCredential))
169 throw new ArgumentException ();
171 credentials = value as NetworkCredential;
176 public static new RequestCachePolicy DefaultCachePolicy
179 throw GetMustImplement ();
182 throw GetMustImplement ();
186 public bool EnableSsl {
191 CheckRequestStarted ();
197 public override WebHeaderCollection Headers
200 throw GetMustImplement ();
203 throw GetMustImplement ();
207 public bool KeepAlive {
212 CheckRequestStarted ();
217 public override string Method {
222 CheckRequestStarted ();
224 throw new ArgumentNullException ("Method string cannot be null");
226 if (value.Length == 0 || Array.BinarySearch (supportedCommands, value) < 0)
227 throw new ArgumentException ("Method not supported", "value");
233 public override bool PreAuthenticate {
235 throw new NotSupportedException ();
238 throw new NotSupportedException ();
242 public override IWebProxy Proxy {
247 CheckRequestStarted ();
249 throw new ArgumentNullException ();
255 public int ReadWriteTimeout {
260 CheckRequestStarted ();
263 throw new ArgumentOutOfRangeException ();
269 public string RenameTo {
274 CheckRequestStarted ();
275 if (value == null || value.Length == 0)
276 throw new ArgumentException ("RenameTo value can't be null or empty", "RenameTo");
282 public override Uri RequestUri {
288 public ServicePoint ServicePoint {
290 return GetServicePoint ();
294 public bool UsePassive {
299 CheckRequestStarted ();
305 public override bool UseDefaultCredentials
308 throw GetMustImplement ();
311 throw GetMustImplement ();
315 public bool UseBinary {
319 CheckRequestStarted ();
324 public override int Timeout {
329 CheckRequestStarted ();
332 throw new ArgumentOutOfRangeException ();
340 return binary ? "I" : "A";
355 requestState = value;
360 public override void Abort () {
362 if (State == RequestState.TransferInProgress) {
363 /*FtpStatus status = */
364 SendCommand (false, AbortCommand);
367 if (!InFinalState ()) {
368 State = RequestState.Aborted;
369 ftpResponse = new FtpWebResponse (requestUri, method, FtpStatusCode.FileActionAborted, "Aborted by request");
374 public override IAsyncResult BeginGetResponse (AsyncCallback callback, object state) {
375 if (asyncResult != null && !asyncResult.IsCompleted) {
376 throw new InvalidOperationException ("Cannot re-call BeginGetRequestStream/BeginGetResponse while a previous call is still in progress");
381 asyncResult = new FtpAsyncResult (callback, state);
385 asyncResult.SetCompleted (true, ftpResponse);
387 if (State == RequestState.Before)
388 State = RequestState.Scheduled;
390 Thread thread = new Thread (ProcessRequest);
398 public override WebResponse EndGetResponse (IAsyncResult asyncResult) {
399 if (asyncResult == null)
400 throw new ArgumentNullException ("AsyncResult cannot be null!");
402 if (!(asyncResult is FtpAsyncResult) || asyncResult != this.asyncResult)
403 throw new ArgumentException ("AsyncResult is from another request!");
405 FtpAsyncResult asyncFtpResult = (FtpAsyncResult) asyncResult;
406 if (!asyncFtpResult.WaitUntilComplete (timeout, false)) {
408 throw new WebException ("Transfer timed out.", WebExceptionStatus.Timeout);
415 if (asyncFtpResult.GotException)
416 throw asyncFtpResult.Exception;
418 return asyncFtpResult.Response;
421 public override WebResponse GetResponse () {
422 IAsyncResult asyncResult = BeginGetResponse (null, null);
423 return EndGetResponse (asyncResult);
426 public override IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state) {
427 if (method != WebRequestMethods.Ftp.UploadFile && method != WebRequestMethods.Ftp.UploadFileWithUniqueName &&
428 method != WebRequestMethods.Ftp.AppendFile)
429 throw new ProtocolViolationException ();
434 if (State != RequestState.Before)
435 throw new InvalidOperationException ("Cannot re-call BeginGetRequestStream/BeginGetResponse while a previous call is still in progress");
437 State = RequestState.Scheduled;
440 asyncResult = new FtpAsyncResult (callback, state);
441 Thread thread = new Thread (ProcessRequest);
447 public override Stream EndGetRequestStream (IAsyncResult asyncResult) {
448 if (asyncResult == null)
449 throw new ArgumentNullException ("asyncResult");
451 if (!(asyncResult is FtpAsyncResult))
452 throw new ArgumentException ("asyncResult");
454 if (State == RequestState.Aborted) {
455 throw new WebException ("Request aborted", WebExceptionStatus.RequestCanceled);
458 if (asyncResult != this.asyncResult)
459 throw new ArgumentException ("AsyncResult is from another request!");
461 FtpAsyncResult res = (FtpAsyncResult) asyncResult;
463 if (!res.WaitUntilComplete (timeout, false)) {
465 throw new WebException ("Request timed out");
468 if (res.GotException)
474 public override Stream GetRequestStream () {
475 IAsyncResult asyncResult = BeginGetRequestStream (null, null);
476 return EndGetRequestStream (asyncResult);
479 ServicePoint GetServicePoint ()
481 if (servicePoint == null)
482 servicePoint = ServicePointManager.FindServicePoint (requestUri, proxy);
487 // Probably move some code of command connection here
491 hostEntry = GetServicePoint ().HostEntry;
493 if (hostEntry == null) {
494 ftpResponse.UpdateStatus (new FtpStatus(FtpStatusCode.ActionAbortedLocalProcessingError, "Cannot resolve server name"));
495 throw new WebException ("The remote server name could not be resolved: " + requestUri,
496 null, WebExceptionStatus.NameResolutionFailure, ftpResponse);
500 void ProcessRequest () {
502 if (State == RequestState.Scheduled) {
503 ftpResponse = new FtpWebResponse (requestUri, method, keepAlive);
507 //State = RequestState.Finished;
508 //finalResponse = ftpResponse;
509 asyncResult.SetCompleted (false, ftpResponse);
511 catch (Exception e) {
512 State = RequestState.Error;
513 SetCompleteWithError (e);
518 FtpStatus status = GetResponseStatus ();
520 ftpResponse.UpdateStatus (status);
522 if (ftpResponse.IsFinal ()) {
523 State = RequestState.Finished;
527 asyncResult.SetCompleted (false, ftpResponse);
534 FtpStatus status = SendCommand (TypeCommand, DataType);
535 if ((int) status.StatusCode < 200 || (int) status.StatusCode >= 300)
536 throw CreateExceptionFromResponse (status);
540 string GetRemoteFolderPath (Uri uri)
543 string local_path = Uri.UnescapeDataString (uri.LocalPath);
544 if (initial_path == null || initial_path == "/") {
547 if (local_path [0] == '/')
548 local_path = local_path.Substring (1);
550 Uri initial = new Uri ("ftp://dummy-host" + initial_path);
551 result = new Uri (initial, local_path).LocalPath;
554 int last = result.LastIndexOf ('/');
558 return result.Substring (0, last + 1);
561 void CWDAndSetFileName (Uri uri)
563 string remote_folder = GetRemoteFolderPath (uri);
565 if (remote_folder != null) {
566 status = SendCommand (ChangeDir, remote_folder);
567 if ((int) status.StatusCode < 200 || (int) status.StatusCode >= 300)
568 throw CreateExceptionFromResponse (status);
570 int last = uri.LocalPath.LastIndexOf ('/');
572 file_name = Uri.UnescapeDataString (uri.LocalPath.Substring (last + 1));
577 void ProcessMethod ()
579 State = RequestState.Connecting;
583 OpenControlConnection ();
584 CWDAndSetFileName (requestUri);
588 // Open data connection and receive data
589 case WebRequestMethods.Ftp.DownloadFile:
590 case WebRequestMethods.Ftp.ListDirectory:
591 case WebRequestMethods.Ftp.ListDirectoryDetails:
594 // Open data connection and send data
595 case WebRequestMethods.Ftp.AppendFile:
596 case WebRequestMethods.Ftp.UploadFile:
597 case WebRequestMethods.Ftp.UploadFileWithUniqueName:
600 // Get info from control connection
601 case WebRequestMethods.Ftp.GetFileSize:
602 case WebRequestMethods.Ftp.GetDateTimestamp:
603 case WebRequestMethods.Ftp.PrintWorkingDirectory:
604 case WebRequestMethods.Ftp.MakeDirectory:
605 case WebRequestMethods.Ftp.Rename:
606 case WebRequestMethods.Ftp.DeleteFile:
607 ProcessSimpleMethod ();
609 default: // What to do here?
610 throw new Exception (String.Format ("Support for command {0} not implemented yet", method));
616 private void CloseControlConnection () {
617 SendCommand (QuitCommand);
618 controlStream.Close ();
621 private void CloseDataConnection () {
622 if(dataSocket != null)
626 private void CloseConnection () {
627 CloseControlConnection ();
628 CloseDataConnection ();
631 void ProcessSimpleMethod ()
633 State = RequestState.TransferInProgress;
637 if (method == WebRequestMethods.Ftp.PrintWorkingDirectory)
640 if (method == WebRequestMethods.Ftp.Rename)
641 method = RenameFromCommand;
643 status = SendCommand (method, file_name);
645 ftpResponse.Stream = new EmptyStream ();
647 string desc = status.StatusDescription;
650 case WebRequestMethods.Ftp.GetFileSize: {
651 if (status.StatusCode != FtpStatusCode.FileStatus)
652 throw CreateExceptionFromResponse (status);
656 for (i = 4, len = 0; i < desc.Length && Char.IsDigit (desc [i]); i++, len++)
660 throw new WebException ("Bad format for server response in " + method);
662 if (!Int64.TryParse (desc.Substring (4, len), out size))
663 throw new WebException ("Bad format for server response in " + method);
665 ftpResponse.contentLength = size;
668 case WebRequestMethods.Ftp.GetDateTimestamp:
669 if (status.StatusCode != FtpStatusCode.FileStatus)
670 throw CreateExceptionFromResponse (status);
671 ftpResponse.LastModified = DateTime.ParseExact (desc.Substring (4), "yyyyMMddHHmmss", null);
673 case WebRequestMethods.Ftp.MakeDirectory:
674 if (status.StatusCode != FtpStatusCode.PathnameCreated)
675 throw CreateExceptionFromResponse (status);
678 method = WebRequestMethods.Ftp.PrintWorkingDirectory;
680 if (status.StatusCode != FtpStatusCode.FileActionOK)
681 throw CreateExceptionFromResponse (status);
683 status = SendCommand (method);
685 if (status.StatusCode != FtpStatusCode.PathnameCreated)
686 throw CreateExceptionFromResponse (status);
688 case RenameFromCommand:
689 method = WebRequestMethods.Ftp.Rename;
690 if (status.StatusCode != FtpStatusCode.FileCommandPending)
691 throw CreateExceptionFromResponse (status);
692 // Pass an empty string if RenameTo wasn't specified
693 status = SendCommand (RenameToCommand, renameTo != null ? renameTo : String.Empty);
694 if (status.StatusCode != FtpStatusCode.FileActionOK)
695 throw CreateExceptionFromResponse (status);
697 case WebRequestMethods.Ftp.DeleteFile:
698 if (status.StatusCode != FtpStatusCode.FileActionOK) {
699 throw CreateExceptionFromResponse (status);
704 State = RequestState.Finished;
709 State = RequestState.OpeningData;
711 OpenDataConnection ();
713 State = RequestState.TransferInProgress;
714 requestStream = new FtpDataStream (this, dataSocket, false);
715 asyncResult.Stream = requestStream;
720 State = RequestState.OpeningData;
722 OpenDataConnection ();
724 State = RequestState.TransferInProgress;
725 ftpResponse.Stream = new FtpDataStream (this, dataSocket, true);
728 void CheckRequestStarted ()
730 if (State != RequestState.Before)
731 throw new InvalidOperationException ("There is a request currently in progress");
734 void OpenControlConnection ()
736 Exception exception = null;
738 foreach (IPAddress address in hostEntry.AddressList) {
739 sock = new Socket (address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
741 IPEndPoint remote = new IPEndPoint (address, requestUri.Port);
743 if (!ServicePoint.CallEndPointDelegate (sock, remote)) {
748 sock.Connect (remote);
749 localEndPoint = (IPEndPoint) sock.LocalEndPoint;
751 } catch (SocketException exc) {
759 // Couldn't connect to any address
761 throw new WebException ("Unable to connect to remote server", exception,
762 WebExceptionStatus.UnknownError, ftpResponse);
764 controlStream = new NetworkStream (sock);
765 controlReader = new StreamReader (controlStream, Encoding.ASCII);
767 State = RequestState.Authenticating;
770 FtpStatus status = SendCommand ("OPTS", "utf8", "on");
771 // ignore status for OPTS
772 status = SendCommand (WebRequestMethods.Ftp.PrintWorkingDirectory);
773 initial_path = GetInitialPath (status);
776 static string GetInitialPath (FtpStatus status)
778 int s = (int) status.StatusCode;
779 if (s < 200 || s > 300 || status.StatusDescription.Length <= 4)
780 throw new WebException ("Error getting current directory: " + status.StatusDescription, null,
781 WebExceptionStatus.UnknownError, null);
783 string msg = status.StatusDescription.Substring (4);
784 if (msg [0] == '"') {
785 int next_quote = msg.IndexOf ('\"', 1);
786 if (next_quote == -1)
787 throw new WebException ("Error getting current directory: PWD -> " + status.StatusDescription, null,
788 WebExceptionStatus.UnknownError, null);
790 msg = msg.Substring (1, next_quote - 1);
793 if (!msg.EndsWith ("/"))
798 // Probably we could do better having here a regex
799 Socket SetupPassiveConnection (string statusDescription)
801 // Current response string
802 string response = statusDescription;
803 if (response.Length < 4)
804 throw new WebException ("Cannot open passive data connection");
806 // Look for first digit after code
808 for (i = 3; i < response.Length && !Char.IsDigit (response [i]); i++)
810 if (i >= response.Length)
811 throw new WebException ("Cannot open passive data connection");
814 string [] digits = response.Substring (i).Split (new char [] {','}, 6);
815 if (digits.Length != 6)
816 throw new WebException ("Cannot open passive data connection");
818 // Clean non-digits at the end of last element
820 for (j = digits [5].Length - 1; j >= 0 && !Char.IsDigit (digits [5][j]); j--)
823 throw new WebException ("Cannot open passive data connection");
825 digits [5] = digits [5].Substring (0, j + 1);
829 ip = IPAddress.Parse (String.Join (".", digits, 0, 4));
830 } catch (FormatException) {
831 throw new WebException ("Cannot open passive data connection");
836 if (!Int32.TryParse (digits [4], out p1) || !Int32.TryParse (digits [5], out p2))
837 throw new WebException ("Cannot open passive data connection");
839 port = (p1 << 8) + p2; // p1 * 256 + p2
840 //port = p1 * 256 + p2;
841 if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
842 throw new WebException ("Cannot open passive data connection");
844 IPEndPoint ep = new IPEndPoint (ip, port);
845 Socket sock = new Socket (ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
848 } catch (SocketException) {
850 throw new WebException ("Cannot open passive data connection");
856 Exception CreateExceptionFromResponse (FtpStatus status)
858 FtpWebResponse ftpResponse = new FtpWebResponse (requestUri, method, status);
860 WebException exc = new WebException ("Server returned an error: " + status.StatusDescription,
861 null, WebExceptionStatus.ProtocolError, ftpResponse);
865 // Here we could also get a server error, so be cautious
866 internal void SetTransferCompleted ()
871 State = RequestState.Finished;
872 FtpStatus status = GetResponseStatus ();
873 ftpResponse.UpdateStatus (status);
878 void SetCompleteWithError (Exception exc)
880 if (asyncResult != null) {
881 asyncResult.SetCompleted (false, exc);
885 Socket InitDataConnection ()
890 status = SendCommand (PassiveCommand);
891 if (status.StatusCode != FtpStatusCode.EnteringPassive) {
892 throw CreateExceptionFromResponse (status);
895 return SetupPassiveConnection (status.StatusDescription);
898 // Open a socket to listen the server's connection
899 Socket sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
901 sock.Bind (new IPEndPoint (localEndPoint.Address, 0));
902 sock.Listen (1); // We only expect a connection from server
904 } catch (SocketException e) {
907 throw new WebException ("Couldn't open listening socket on client", e);
910 IPEndPoint ep = (IPEndPoint) sock.LocalEndPoint;
911 string ipString = ep.Address.ToString ().Replace ('.', ',');
912 int h1 = ep.Port >> 8; // ep.Port / 256
913 int h2 = ep.Port % 256;
915 string portParam = ipString + "," + h1 + "," + h2;
916 status = SendCommand (PortCommand, portParam);
918 if (status.StatusCode != FtpStatusCode.CommandOK) {
920 throw (CreateExceptionFromResponse (status));
926 void OpenDataConnection ()
930 Socket s = InitDataConnection ();
932 // Handle content offset
934 status = SendCommand (RestCommand, offset.ToString ());
935 if (status.StatusCode != FtpStatusCode.FileCommandPending)
936 throw CreateExceptionFromResponse (status);
939 if (method != WebRequestMethods.Ftp.ListDirectory && method != WebRequestMethods.Ftp.ListDirectoryDetails &&
940 method != WebRequestMethods.Ftp.UploadFileWithUniqueName) {
941 status = SendCommand (method, file_name);
943 status = SendCommand (method);
946 if (status.StatusCode != FtpStatusCode.OpeningData && status.StatusCode != FtpStatusCode.DataAlreadyOpen)
947 throw CreateExceptionFromResponse (status);
954 // Active connection (use Socket.Blocking to true)
955 Socket incoming = null;
957 incoming = s.Accept ();
959 catch (SocketException) {
961 if (incoming != null)
964 throw new ProtocolViolationException ("Server commited a protocol violation.");
968 dataSocket = incoming;
972 InitiateSecureConnection (ref controlStream);
973 controlReader = new StreamReader (controlStream, Encoding.ASCII);
976 ftpResponse.UpdateStatus (status);
981 string username = null;
982 string password = null;
983 string domain = null;
985 if (credentials != null) {
986 username = credentials.UserName;
987 password = credentials.Password;
988 domain = credentials.Domain;
991 if (username == null)
992 username = "anonymous";
993 if (password == null)
994 password = "@anonymous";
995 if (!string.IsNullOrEmpty (domain))
996 username = domain + '\\' + username;
998 // Connect to server and get banner message
999 FtpStatus status = GetResponseStatus ();
1000 ftpResponse.BannerMessage = status.StatusDescription;
1003 InitiateSecureConnection (ref controlStream);
1004 controlReader = new StreamReader (controlStream, Encoding.ASCII);
1007 if (status.StatusCode != FtpStatusCode.SendUserCommand)
1008 throw CreateExceptionFromResponse (status);
1010 status = SendCommand (UserCommand, username);
1012 switch (status.StatusCode) {
1013 case FtpStatusCode.SendPasswordCommand:
1014 status = SendCommand (PasswordCommand, password);
1015 if (status.StatusCode != FtpStatusCode.LoggedInProceed)
1016 throw CreateExceptionFromResponse (status);
1018 case FtpStatusCode.LoggedInProceed:
1021 throw CreateExceptionFromResponse (status);
1024 ftpResponse.WelcomeMessage = status.StatusDescription;
1025 ftpResponse.UpdateStatus (status);
1028 FtpStatus SendCommand (string command, params string [] parameters) {
1029 return SendCommand (true, command, parameters);
1032 FtpStatus SendCommand (bool waitResponse, string command, params string [] parameters)
1035 string commandString = command;
1036 if (parameters.Length > 0)
1037 commandString += " " + String.Join (" ", parameters);
1039 commandString += EOL;
1040 cmd = Encoding.ASCII.GetBytes (commandString);
1042 controlStream.Write (cmd, 0, cmd.Length);
1043 } catch (IOException) {
1044 //controlStream.Close ();
1045 return new FtpStatus(FtpStatusCode.ServiceNotAvailable, "Write failed");
1051 FtpStatus result = GetResponseStatus ();
1052 if (ftpResponse != null)
1053 ftpResponse.UpdateStatus (result);
1057 internal static FtpStatus ServiceNotAvailable ()
1059 return new FtpStatus (FtpStatusCode.ServiceNotAvailable, Locale.GetText ("Invalid response from server"));
1062 internal FtpStatus GetResponseStatus ()
1065 string response = null;
1068 response = controlReader.ReadLine ();
1069 } catch (IOException) {
1072 if (response == null || response.Length < 3)
1073 return ServiceNotAvailable ();
1076 if (!Int32.TryParse (response.Substring (0, 3), out code))
1077 return ServiceNotAvailable ();
1079 if (response.Length > 3 && response [3] == '-'){
1081 string find = code.ToString() + ' ';
1084 line = controlReader.ReadLine();
1085 } catch (IOException) {
1088 return ServiceNotAvailable ();
1090 response += Environment.NewLine + line;
1092 if (line.StartsWith(find, StringComparison.Ordinal))
1096 return new FtpStatus ((FtpStatusCode) code, response);
1100 private void InitiateSecureConnection (ref NetworkStream stream) {
1101 FtpStatus status = SendCommand (AuthCommand, "TLS");
1103 if (status.StatusCode != FtpStatusCode.ServerWantsSecureSession) {
1104 throw CreateExceptionFromResponse (status);
1107 ChangeToSSLSocket (ref stream);
1110 internal static bool ChangeToSSLSocket (ref NetworkStream stream) {
1112 stream.ChangeToSSLSocket ();
1115 throw new NotImplementedException ();
1119 bool InFinalState () {
1120 return (State == RequestState.Aborted || State == RequestState.Error || State == RequestState.Finished);
1123 bool InProgress () {
1124 return (State != RequestState.Before && !InFinalState ());
1127 internal void CheckIfAborted () {
1128 if (State == RequestState.Aborted)
1129 throw new WebException ("Request aborted", WebExceptionStatus.RequestCanceled);
1132 void CheckFinalState () {
1133 if (InFinalState ())
1134 throw new InvalidOperationException ("Cannot change final state");
1137 class EmptyStream : MemoryStream
1139 internal EmptyStream ()
1140 : base (new byte [0], false) {