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); } }
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); }
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); }
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); }
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); }
void SetType() { if (binary) { FtpStatus status = SendCommand(TypeCommand, DataType); if ((int)status.StatusCode < 200 || (int)status.StatusCode >= 300) { throw CreateExceptionFromResponse(status); } } }
private void InitiateSecureConnection(ref NetworkStream stream) { FtpStatus status = SendCommand(AuthCommand, "TLS"); if (status.StatusCode != FtpStatusCode.ServerWantsSecureSession) { throw CreateExceptionFromResponse(status); } ChangeToSSLSocket(ref stream); }
// 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(); } }
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); }
internal void UpdateStatus (FtpStatus status) { statusCode = status.StatusCode; statusDescription = status.StatusDescription; }
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); }
internal FtpWebResponse(FtpWebRequest request, Uri uri, string method, FtpStatus status) : this(request, uri, method, status.StatusCode, status.StatusDescription) { }
internal void UpdateStatus(FtpStatus status) { statusCode = status.StatusCode; statusDescription = status.StatusDescription; }
internal FtpWebResponse (FtpWebRequest request, Uri uri, string method, FtpStatus status) : this (request, uri, method, status.StatusCode, status.StatusDescription) { }
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; }
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; }
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); }