| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148 |
- //
- // System.Net.FtpWebRequest.cs
- //
- // Authors:
- // Carlos Alberto Cortez ([email protected])
- //
- // (c) Copyright 2006 Novell, Inc. (http://www.novell.com)
- //
- #if NET_2_0
- using System;
- using System.IO;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Net.Cache;
- using System.Security.Cryptography.X509Certificates;
- using System.Net;
- namespace System.Net
- {
- public sealed class FtpWebRequest : WebRequest
- {
- Uri requestUri;
- string file_name; // By now, used for upload
- ServicePoint servicePoint;
- Socket dataSocket;
- NetworkStream controlStream;
- StreamReader controlReader;
- NetworkCredential credentials;
- IPHostEntry hostEntry;
- IPEndPoint localEndPoint;
- IWebProxy proxy;
- int timeout = 100000;
- int rwTimeout = 300000;
- long offset = 0;
- bool binary = true;
- bool enableSsl = false;
- bool usePassive = true;
- bool keepAlive = false;
- string method = WebRequestMethods.Ftp.DownloadFile;
- string renameTo;
- object locker = new object ();
-
- RequestState requestState = RequestState.Before;
- FtpAsyncResult asyncResult;
- FtpWebResponse ftpResponse;
- Stream requestStream;
- string initial_path;
- const string ChangeDir = "CWD";
- const string UserCommand = "USER";
- const string PasswordCommand = "PASS";
- const string TypeCommand = "TYPE";
- const string PassiveCommand = "PASV";
- const string PortCommand = "PORT";
- const string AbortCommand = "ABOR";
- const string AuthCommand = "AUTH";
- const string RestCommand = "REST";
- const string RenameFromCommand = "RNFR";
- const string RenameToCommand = "RNTO";
- const string QuitCommand = "QUIT";
- const string EOL = "\r\n"; // Special end of line
- enum RequestState
- {
- Before,
- Scheduled,
- Connecting,
- Authenticating,
- OpeningData,
- TransferInProgress,
- Finished,
- Aborted,
- Error
- }
- // sorted commands
- static readonly string [] supportedCommands = new string [] {
- WebRequestMethods.Ftp.AppendFile, // APPE
- WebRequestMethods.Ftp.DeleteFile, // DELE
- WebRequestMethods.Ftp.ListDirectoryDetails, // LIST
- WebRequestMethods.Ftp.GetDateTimestamp, // MDTM
- WebRequestMethods.Ftp.MakeDirectory, // MKD
- WebRequestMethods.Ftp.ListDirectory, // NLST
- WebRequestMethods.Ftp.PrintWorkingDirectory, // PWD
- WebRequestMethods.Ftp.Rename, // RENAME
- WebRequestMethods.Ftp.DownloadFile, // RETR
- WebRequestMethods.Ftp.RemoveDirectory, // RMD
- WebRequestMethods.Ftp.GetFileSize, // SIZE
- WebRequestMethods.Ftp.UploadFile, // STOR
- WebRequestMethods.Ftp.UploadFileWithUniqueName // STUR
- };
- internal FtpWebRequest (Uri uri)
- {
- this.requestUri = uri;
- this.proxy = GlobalProxySelection.Select;
- }
- static Exception GetMustImplement ()
- {
- return new NotImplementedException ();
- }
-
- [MonoTODO]
- public X509CertificateCollection ClientCertificates
- {
- get {
- throw GetMustImplement ();
- }
- set {
- throw GetMustImplement ();
- }
- }
-
- [MonoTODO]
- public override string ConnectionGroupName
- {
- get {
- throw GetMustImplement ();
- }
- set {
- throw GetMustImplement ();
- }
- }
- public override string ContentType {
- get {
- throw new NotSupportedException ();
- }
- set {
- throw new NotSupportedException ();
- }
- }
- public override long ContentLength {
- get {
- return 0;
- }
- set {
- // DO nothing
- }
- }
- public long ContentOffset {
- get {
- return offset;
- }
- set {
- CheckRequestStarted ();
- if (value < 0)
- throw new ArgumentOutOfRangeException ();
- offset = value;
- }
- }
- public override ICredentials Credentials {
- get {
- return credentials;
- }
- set {
- CheckRequestStarted ();
- if (value == null)
- throw new ArgumentNullException ();
- if (!(value is NetworkCredential))
- throw new ArgumentException ();
- credentials = value as NetworkCredential;
- }
- }
- [MonoTODO]
- public static new RequestCachePolicy DefaultCachePolicy
- {
- get {
- throw GetMustImplement ();
- }
- set {
- throw GetMustImplement ();
- }
- }
- public bool EnableSsl {
- get {
- return enableSsl;
- }
- set {
- CheckRequestStarted ();
- enableSsl = value;
- }
- }
- [MonoTODO]
- public override WebHeaderCollection Headers
- {
- get {
- throw GetMustImplement ();
- }
- set {
- throw GetMustImplement ();
- }
- }
- public bool KeepAlive {
- get {
- return keepAlive;
- }
- set {
- CheckRequestStarted ();
- keepAlive = value;
- }
- }
- public override string Method {
- get {
- return method;
- }
- set {
- CheckRequestStarted ();
- if (value == null)
- throw new ArgumentNullException ("Method string cannot be null");
- if (value.Length == 0 || Array.BinarySearch (supportedCommands, value) < 0)
- throw new ArgumentException ("Method not supported", "value");
-
- method = value;
- }
- }
- public override bool PreAuthenticate {
- get {
- throw new NotSupportedException ();
- }
- set {
- throw new NotSupportedException ();
- }
- }
- public override IWebProxy Proxy {
- get {
- return proxy;
- }
- set {
- CheckRequestStarted ();
- if (value == null)
- throw new ArgumentNullException ();
- proxy = value;
- }
- }
- public int ReadWriteTimeout {
- get {
- return rwTimeout;
- }
- set {
- CheckRequestStarted ();
- if (value < - 1)
- throw new ArgumentOutOfRangeException ();
- else
- rwTimeout = value;
- }
- }
- public string RenameTo {
- get {
- return renameTo;
- }
- set {
- CheckRequestStarted ();
- if (value == null || value.Length == 0)
- throw new ArgumentException ("RenameTo value can't be null or empty", "RenameTo");
- renameTo = value;
- }
- }
- public override Uri RequestUri {
- get {
- return requestUri;
- }
- }
- public ServicePoint ServicePoint {
- get {
- return GetServicePoint ();
- }
- }
- public bool UsePassive {
- get {
- return usePassive;
- }
- set {
- CheckRequestStarted ();
- usePassive = value;
- }
- }
- [MonoTODO]
- public override bool UseDefaultCredentials
- {
- get {
- throw GetMustImplement ();
- }
- set {
- throw GetMustImplement ();
- }
- }
-
- public bool UseBinary {
- get {
- return binary;
- } set {
- CheckRequestStarted ();
- binary = value;
- }
- }
- public override int Timeout {
- get {
- return timeout;
- }
- set {
- CheckRequestStarted ();
- if (value < -1)
- throw new ArgumentOutOfRangeException ();
- else
- timeout = value;
- }
- }
- string DataType {
- get {
- return binary ? "I" : "A";
- }
- }
- RequestState State {
- get {
- lock (locker) {
- return requestState;
- }
- }
- set {
- lock (locker) {
- CheckIfAborted ();
- CheckFinalState ();
- requestState = value;
- }
- }
- }
- public override void Abort () {
- lock (locker) {
- if (State == RequestState.TransferInProgress) {
- /*FtpStatus status = */
- SendCommand (false, AbortCommand);
- }
- if (!InFinalState ()) {
- State = RequestState.Aborted;
- ftpResponse = new FtpWebResponse (requestUri, method, FtpStatusCode.FileActionAborted, "Aborted by request");
- }
- }
- }
- public override IAsyncResult BeginGetResponse (AsyncCallback callback, object state) {
- if (asyncResult != null && !asyncResult.IsCompleted) {
- throw new InvalidOperationException ("Cannot re-call BeginGetRequestStream/BeginGetResponse while a previous call is still in progress");
- }
- CheckIfAborted ();
-
- asyncResult = new FtpAsyncResult (callback, state);
- lock (locker) {
- if (InFinalState ())
- asyncResult.SetCompleted (true, ftpResponse);
- else {
- if (State == RequestState.Before)
- State = RequestState.Scheduled;
- Thread thread = new Thread (ProcessRequest);
- thread.Start ();
- }
- }
- return asyncResult;
- }
- public override WebResponse EndGetResponse (IAsyncResult asyncResult) {
- if (asyncResult == null)
- throw new ArgumentNullException ("AsyncResult cannot be null!");
- if (!(asyncResult is FtpAsyncResult) || asyncResult != this.asyncResult)
- throw new ArgumentException ("AsyncResult is from another request!");
- FtpAsyncResult asyncFtpResult = (FtpAsyncResult) asyncResult;
- if (!asyncFtpResult.WaitUntilComplete (timeout, false)) {
- Abort ();
- throw new WebException ("Transfer timed out.", WebExceptionStatus.Timeout);
- }
- CheckIfAborted ();
- asyncResult = null;
- if (asyncFtpResult.GotException)
- throw asyncFtpResult.Exception;
- return asyncFtpResult.Response;
- }
- public override WebResponse GetResponse () {
- IAsyncResult asyncResult = BeginGetResponse (null, null);
- return EndGetResponse (asyncResult);
- }
- public override IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state) {
- if (method != WebRequestMethods.Ftp.UploadFile && method != WebRequestMethods.Ftp.UploadFileWithUniqueName &&
- method != WebRequestMethods.Ftp.AppendFile)
- throw new ProtocolViolationException ();
- lock (locker) {
- CheckIfAborted ();
- if (State != RequestState.Before)
- throw new InvalidOperationException ("Cannot re-call BeginGetRequestStream/BeginGetResponse while a previous call is still in progress");
- State = RequestState.Scheduled;
- }
- asyncResult = new FtpAsyncResult (callback, state);
- Thread thread = new Thread (ProcessRequest);
- thread.Start ();
- return asyncResult;
- }
- public override Stream EndGetRequestStream (IAsyncResult asyncResult) {
- if (asyncResult == null)
- throw new ArgumentNullException ("asyncResult");
- if (!(asyncResult is FtpAsyncResult))
- throw new ArgumentException ("asyncResult");
- if (State == RequestState.Aborted) {
- throw new WebException ("Request aborted", WebExceptionStatus.RequestCanceled);
- }
-
- if (asyncResult != this.asyncResult)
- throw new ArgumentException ("AsyncResult is from another request!");
- FtpAsyncResult res = (FtpAsyncResult) asyncResult;
- if (!res.WaitUntilComplete (timeout, false)) {
- Abort ();
- throw new WebException ("Request timed out");
- }
- if (res.GotException)
- throw res.Exception;
- return res.Stream;
- }
- public override Stream GetRequestStream () {
- IAsyncResult asyncResult = BeginGetRequestStream (null, null);
- return EndGetRequestStream (asyncResult);
- }
-
- ServicePoint GetServicePoint ()
- {
- if (servicePoint == null)
- servicePoint = ServicePointManager.FindServicePoint (requestUri, proxy);
- return servicePoint;
- }
- // Probably move some code of command connection here
- void ResolveHost ()
- {
- CheckIfAborted ();
- hostEntry = GetServicePoint ().HostEntry;
- if (hostEntry == null) {
- ftpResponse.UpdateStatus (new FtpStatus(FtpStatusCode.ActionAbortedLocalProcessingError, "Cannot resolve server name"));
- throw new WebException ("The remote server name could not be resolved: " + requestUri,
- null, WebExceptionStatus.NameResolutionFailure, ftpResponse);
- }
- }
- 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);
- }
- }
- void SetType ()
- {
- if (binary) {
- FtpStatus status = SendCommand (TypeCommand, DataType);
- if ((int) status.StatusCode < 200 || (int) status.StatusCode >= 300)
- throw CreateExceptionFromResponse (status);
- }
- }
- string GetRemoteFolderPath (Uri uri)
- {
- string result;
- string local_path = Uri.UnescapeDataString (uri.LocalPath);
- if (initial_path == null || initial_path == "/") {
- result = local_path;
- } else {
- if (local_path [0] == '/')
- local_path = local_path.Substring (1);
- Uri initial = new Uri ("ftp://dummy-host" + initial_path);
- result = new Uri (initial, local_path).LocalPath;
- }
- int last = result.LastIndexOf ('/');
- if (last == -1)
- return null;
- return result.Substring (0, last + 1);
- }
- void CWDAndSetFileName (Uri uri)
- {
- string remote_folder = GetRemoteFolderPath (uri);
- FtpStatus status;
- if (remote_folder != null) {
- status = SendCommand (ChangeDir, remote_folder);
- if ((int) status.StatusCode < 200 || (int) status.StatusCode >= 300)
- throw CreateExceptionFromResponse (status);
- int last = uri.LocalPath.LastIndexOf ('/');
- if (last >= 0) {
- file_name = Uri.UnescapeDataString (uri.LocalPath.Substring (last + 1));
- }
- }
- }
- void ProcessMethod ()
- {
- State = RequestState.Connecting;
- ResolveHost ();
- OpenControlConnection ();
- CWDAndSetFileName (requestUri);
- SetType ();
- switch (method) {
- // Open data connection and receive data
- case WebRequestMethods.Ftp.DownloadFile:
- case WebRequestMethods.Ftp.ListDirectory:
- case WebRequestMethods.Ftp.ListDirectoryDetails:
- DownloadData ();
- break;
- // Open data connection and send data
- case WebRequestMethods.Ftp.AppendFile:
- case WebRequestMethods.Ftp.UploadFile:
- case WebRequestMethods.Ftp.UploadFileWithUniqueName:
- UploadData ();
- break;
- // Get info from control connection
- case WebRequestMethods.Ftp.GetFileSize:
- case WebRequestMethods.Ftp.GetDateTimestamp:
- case WebRequestMethods.Ftp.PrintWorkingDirectory:
- case WebRequestMethods.Ftp.MakeDirectory:
- case WebRequestMethods.Ftp.Rename:
- case WebRequestMethods.Ftp.DeleteFile:
- ProcessSimpleMethod ();
- break;
- default: // What to do here?
- throw new Exception (String.Format ("Support for command {0} not implemented yet", method));
- }
- CheckIfAborted ();
- }
- private void CloseControlConnection () {
- SendCommand (QuitCommand);
- controlStream.Close ();
- }
- private void CloseDataConnection () {
- if(dataSocket != null)
- dataSocket.Close ();
- }
- private void CloseConnection () {
- CloseControlConnection ();
- CloseDataConnection ();
- }
-
- void ProcessSimpleMethod ()
- {
- State = RequestState.TransferInProgress;
-
- FtpStatus status;
-
- if (method == WebRequestMethods.Ftp.PrintWorkingDirectory)
- method = ChangeDir;
- if (method == WebRequestMethods.Ftp.Rename)
- method = RenameFromCommand;
-
- status = SendCommand (method, file_name);
- ftpResponse.Stream = new EmptyStream ();
-
- string desc = status.StatusDescription;
- switch (method) {
- case WebRequestMethods.Ftp.GetFileSize: {
- if (status.StatusCode != FtpStatusCode.FileStatus)
- throw CreateExceptionFromResponse (status);
- int i, len;
- long size;
- for (i = 4, len = 0; i < desc.Length && Char.IsDigit (desc [i]); i++, len++)
- ;
- if (len == 0)
- throw new WebException ("Bad format for server response in " + method);
- if (!Int64.TryParse (desc.Substring (4, len), out size))
- throw new WebException ("Bad format for server response in " + method);
- ftpResponse.contentLength = size;
- }
- break;
- case WebRequestMethods.Ftp.GetDateTimestamp:
- if (status.StatusCode != FtpStatusCode.FileStatus)
- throw CreateExceptionFromResponse (status);
- ftpResponse.LastModified = DateTime.ParseExact (desc.Substring (4), "yyyyMMddHHmmss", null);
- break;
- case WebRequestMethods.Ftp.MakeDirectory:
- if (status.StatusCode != FtpStatusCode.PathnameCreated)
- throw CreateExceptionFromResponse (status);
- break;
- case ChangeDir:
- method = WebRequestMethods.Ftp.PrintWorkingDirectory;
- if (status.StatusCode != FtpStatusCode.FileActionOK)
- throw CreateExceptionFromResponse (status);
- status = SendCommand (method);
- if (status.StatusCode != FtpStatusCode.PathnameCreated)
- throw CreateExceptionFromResponse (status);
- break;
- case RenameFromCommand:
- method = WebRequestMethods.Ftp.Rename;
- if (status.StatusCode != FtpStatusCode.FileCommandPending)
- throw CreateExceptionFromResponse (status);
- // Pass an empty string if RenameTo wasn't specified
- status = SendCommand (RenameToCommand, renameTo != null ? renameTo : String.Empty);
- if (status.StatusCode != FtpStatusCode.FileActionOK)
- throw CreateExceptionFromResponse (status);
- break;
- case WebRequestMethods.Ftp.DeleteFile:
- if (status.StatusCode != FtpStatusCode.FileActionOK) {
- throw CreateExceptionFromResponse (status);
- }
- break;
- }
- State = RequestState.Finished;
- }
- void UploadData ()
- {
- State = RequestState.OpeningData;
- OpenDataConnection ();
- State = RequestState.TransferInProgress;
- requestStream = new FtpDataStream (this, dataSocket, false);
- asyncResult.Stream = requestStream;
- }
- 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);
- }
- void CheckRequestStarted ()
- {
- if (State != RequestState.Before)
- throw new InvalidOperationException ("There is a request currently in progress");
- }
- 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");
- // ignore status for OPTS
- status = SendCommand (WebRequestMethods.Ftp.PrintWorkingDirectory);
- initial_path = GetInitialPath (status);
- }
- 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;
- }
- // Probably we could do better having here a regex
- Socket SetupPassiveConnection (string statusDescription)
- {
- // Current response string
- string response = statusDescription;
- if (response.Length < 4)
- throw new WebException ("Cannot open passive data connection");
-
- // Look for first digit after code
- int i;
- for (i = 3; i < response.Length && !Char.IsDigit (response [i]); i++)
- ;
- if (i >= response.Length)
- throw new WebException ("Cannot open passive data connection");
- // Get six elements
- string [] digits = response.Substring (i).Split (new char [] {','}, 6);
- if (digits.Length != 6)
- throw new WebException ("Cannot open passive data connection");
- // Clean non-digits at the end of last element
- int j;
- for (j = digits [5].Length - 1; j >= 0 && !Char.IsDigit (digits [5][j]); j--)
- ;
- if (j < 0)
- throw new WebException ("Cannot open passive data connection");
-
- digits [5] = digits [5].Substring (0, j + 1);
- IPAddress ip;
- try {
- ip = IPAddress.Parse (String.Join (".", digits, 0, 4));
- } catch (FormatException) {
- throw new WebException ("Cannot open passive data connection");
- }
- // Get the port
- int p1, p2, port;
- if (!Int32.TryParse (digits [4], out p1) || !Int32.TryParse (digits [5], out p2))
- throw new WebException ("Cannot open passive data connection");
- port = (p1 << 8) + p2; // p1 * 256 + p2
- //port = p1 * 256 + p2;
- if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
- throw new WebException ("Cannot open passive data connection");
- IPEndPoint ep = new IPEndPoint (ip, port);
- Socket sock = new Socket (ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
- try {
- sock.Connect (ep);
- } catch (SocketException) {
- sock.Close ();
- throw new WebException ("Cannot open passive data connection");
- }
- return sock;
- }
- 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;
- }
-
- // 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 SetCompleteWithError (Exception exc)
- {
- if (asyncResult != null) {
- asyncResult.SetCompleted (false, exc);
- }
- }
- Socket InitDataConnection ()
- {
- FtpStatus status;
-
- if (usePassive) {
- status = SendCommand (PassiveCommand);
- if (status.StatusCode != FtpStatusCode.EnteringPassive) {
- throw CreateExceptionFromResponse (status);
- }
-
- return SetupPassiveConnection (status.StatusDescription);
- }
- // Open a socket to listen the server's connection
- Socket sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- try {
- sock.Bind (new IPEndPoint (localEndPoint.Address, 0));
- sock.Listen (1); // We only expect a connection from server
- } catch (SocketException e) {
- sock.Close ();
- throw new WebException ("Couldn't open listening socket on client", e);
- }
- IPEndPoint ep = (IPEndPoint) sock.LocalEndPoint;
- string ipString = ep.Address.ToString ().Replace ('.', ',');
- int h1 = ep.Port >> 8; // ep.Port / 256
- int h2 = ep.Port % 256;
- string portParam = ipString + "," + h1 + "," + h2;
- status = SendCommand (PortCommand, portParam);
-
- if (status.StatusCode != FtpStatusCode.CommandOK) {
- sock.Close ();
- throw (CreateExceptionFromResponse (status));
- }
- return sock;
- }
- void OpenDataConnection ()
- {
- FtpStatus status;
-
- Socket s = InitDataConnection ();
- if (method != WebRequestMethods.Ftp.ListDirectory && method != WebRequestMethods.Ftp.ListDirectoryDetails &&
- method != WebRequestMethods.Ftp.UploadFileWithUniqueName) {
- status = SendCommand (method, file_name);
- } else {
- status = SendCommand (method);
- }
- if (status.StatusCode != FtpStatusCode.OpeningData && status.StatusCode != FtpStatusCode.DataAlreadyOpen)
- throw CreateExceptionFromResponse (status);
- if (usePassive) {
- dataSocket = s;
- }
- else {
- // Active connection (use Socket.Blocking to true)
- Socket incoming = null;
- try {
- incoming = s.Accept ();
- }
- catch (SocketException) {
- s.Close ();
- if (incoming != null)
- incoming.Close ();
- throw new ProtocolViolationException ("Server commited a protocol violation.");
- }
- s.Close ();
- dataSocket = incoming;
- }
- if (EnableSsl) {
- InitiateSecureConnection (ref controlStream);
- controlReader = new StreamReader (controlStream, Encoding.ASCII);
- }
- ftpResponse.UpdateStatus (status);
- }
- 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 = "anonymous";
- if (password == null)
- password = "@anonymous";
- 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);
- }
- FtpStatus SendCommand (string command, params string [] parameters) {
- return SendCommand (true, command, parameters);
- }
- 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;
- }
- internal static FtpStatus ServiceNotAvailable ()
- {
- return new FtpStatus (FtpStatusCode.ServiceNotAvailable, Locale.GetText ("Invalid response from server"));
- }
-
- internal FtpStatus GetResponseStatus ()
- {
- while (true) {
- string response = null;
- try {
- response = controlReader.ReadLine ();
- } catch (IOException) {
- }
- if (response == null || response.Length < 3)
- return ServiceNotAvailable ();
- int code;
- if (!Int32.TryParse (response.Substring (0, 3), out code))
- return ServiceNotAvailable ();
- if (response [3] == '-'){
- string line = null;
- string find = code.ToString() + ' ';
- while (true){
- try {
- line = controlReader.ReadLine();
- } catch (IOException) {
- }
- if (line == null)
- return ServiceNotAvailable ();
-
- response += Environment.NewLine + line;
- if (line.StartsWith(find, StringComparison.Ordinal))
- break;
- }
- }
- return new FtpStatus ((FtpStatusCode) code, response);
- }
- }
- private void InitiateSecureConnection (ref NetworkStream stream) {
- FtpStatus status = SendCommand (AuthCommand, "TLS");
- if (status.StatusCode != FtpStatusCode.ServerWantsSecureSession) {
- throw CreateExceptionFromResponse (status);
- }
- ChangeToSSLSocket (ref stream);
- }
- internal static bool ChangeToSSLSocket (ref NetworkStream stream) {
- #if TARGET_JVM
- stream.ChangeToSSLSocket ();
- return true;
- #else
- throw new NotImplementedException ();
- #endif
- }
-
- bool InFinalState () {
- return (State == RequestState.Aborted || State == RequestState.Error || State == RequestState.Finished);
- }
- bool InProgress () {
- return (State != RequestState.Before && !InFinalState ());
- }
- internal void CheckIfAborted () {
- if (State == RequestState.Aborted)
- throw new WebException ("Request aborted", WebExceptionStatus.RequestCanceled);
- }
- void CheckFinalState () {
- if (InFinalState ())
- throw new InvalidOperationException ("Cannot change final state");
- }
- class EmptyStream : MemoryStream
- {
- internal EmptyStream ()
- : base (new byte [0], false) {
- }
- }
- }
- }
- #endif
|