Merge branch 'alexischr/nursery-canaries-managed-alloc'
[mono.git] / mcs / class / System / Test / System.Net / FtpWebRequestTest.cs
1 //
2 // FtpWebRequestTest.cs - NUnit Test Cases for System.Net.FtpWebRequest
3 //
4 // Authors:
5 //      Carlos Alberto Cortez <calberto.cortez@gmail.com>
6 //      Gonzalo Paniagua Javier <gonzalo@novell.com>
7 //
8 // Copyright (c) 2006,2007,2008 Novell, Inc. (http://www.novell.com)
9 //
10 using NUnit.Framework;
11 using System;
12 using System.Collections.Generic;
13 using System.IO;
14 using System.Net;
15 using System.Net.Sockets;
16 using System.Text;
17 using System.Threading;
18
19 namespace MonoTests.System.Net 
20 {
21         [TestFixture]
22         public class FtpWebRequestTest
23         {
24                 FtpWebRequest _defaultRequest;
25                 FtpWebRequest defaultRequest {
26                         get { return _defaultRequest ?? (_defaultRequest = (FtpWebRequest) WebRequest.Create ("ftp://www.contoso.com")); }
27                 }
28                 
29                 private string _tempDirectory;
30                 private string _tempFile;
31
32                 [SetUp]
33                 public void SetUp ()
34                 {
35                         _tempDirectory = Path.Combine (Path.GetTempPath (), "MonoTests.System.Net.FileWebRequestTest");
36                         _tempFile = Path.Combine (_tempDirectory, "FtpWebRequestTest.tmp");
37                         if (!Directory.Exists (_tempDirectory)) {
38                                 Directory.CreateDirectory (_tempDirectory);
39                         } else {
40                                 // ensure no files are left over from previous runs
41                                 string [] files = Directory.GetFiles (_tempDirectory, "*");
42                                 foreach (string file in files)
43                                         File.Delete (file);
44                         }
45                 }
46
47                 [TearDown]
48                 public void TearDown ()
49                 {
50                         if (Directory.Exists (_tempDirectory))
51                                 Directory.Delete (_tempDirectory, true);
52                 }
53
54                 [Test]
55                 public void ContentLength ()
56                 {
57                         try {
58                                 long l = defaultRequest.ContentLength;
59 #if FEATURE_NO_BSD_SOCKETS
60                                 Assert.Fail ("#1a");
61                         } catch (PlatformNotSupportedException) {
62                                 // OK.
63 #else
64                         } catch (NotSupportedException) {
65                                 Assert.Fail ("#1"); // Not overriden
66 #endif
67                         }
68
69                         try {
70                                 defaultRequest.ContentLength = 2;
71 #if FEATURE_NO_BSD_SOCKETS
72                                 Assert.Fail ("#2a");
73                         } catch (PlatformNotSupportedException) {
74                                 // OK.
75 #else
76                         } catch (NotSupportedException) {
77                                 Assert.Fail ("#2"); // Not overriden
78 #endif
79                         }
80                 }
81
82                 [Test]
83                 public void ContentType ()
84                 {
85                         try {
86                                 string t = defaultRequest.ContentType;
87                                 Assert.Fail ("#1");
88                         } catch (NotSupportedException) {
89                         }
90
91                         try {
92                                 defaultRequest.ContentType = String.Empty;
93                                 Assert.Fail ("#2");
94                         } catch (NotSupportedException) {
95                         }
96                 }
97
98                 [Test]
99 #if FEATURE_NO_BSD_SOCKETS
100                 [ExpectedException (typeof (PlatformNotSupportedException))]
101 #endif
102                 public void ContentOffset ()
103                 {
104                         try {
105                                 defaultRequest.ContentOffset = -2;
106                                 Assert.Fail ("#1");
107                         } catch (ArgumentOutOfRangeException) {
108                         }
109                 }
110
111                 [Test]
112 #if FEATURE_NO_BSD_SOCKETS
113                 [ExpectedException (typeof (PlatformNotSupportedException))]
114 #endif
115                 public void Credentials ()
116                 {
117                         try {
118                                 defaultRequest.Credentials = null;
119                                 Assert.Fail ("#1");
120                         } catch (ArgumentNullException) {
121                         }
122
123                 }
124
125                 [Test]
126 #if FEATURE_NO_BSD_SOCKETS
127                 [ExpectedException (typeof (PlatformNotSupportedException))]
128 #endif
129                 public void Method ()
130                 {
131                         try {
132                                 defaultRequest.Method = null;
133                                 Assert.Fail ("#1");
134                         } catch (ArgumentNullException) {
135                         }
136
137                         try {
138                                 defaultRequest.Method = String.Empty;
139                                 Assert.Fail ("#2");
140                         } catch (ArgumentException) {
141                         }
142
143                         try {
144                                 defaultRequest.Method = "WrongValue";
145                                 Assert.Fail ("#3");
146                         } catch (ArgumentException) {
147                         }
148                 }
149
150                 [Test]
151                 public void PreAuthenticate ()
152                 {
153                         try {
154                                 bool p = defaultRequest.PreAuthenticate;
155                                 Assert.Fail ("#1");
156                         } catch (NotSupportedException) {
157                         }
158
159                         try {
160                                 defaultRequest.PreAuthenticate = true;
161                         } catch (NotSupportedException) {
162                         }
163                 }
164
165                 [Test]
166 #if FEATURE_NO_BSD_SOCKETS
167                 [ExpectedException (typeof (PlatformNotSupportedException))]
168 #endif
169                 public void ReadWriteTimeout ()
170                 {
171                         try {
172                                 defaultRequest.ReadWriteTimeout = -2;
173                                 Assert.Fail ("#2");
174                         } catch (ArgumentOutOfRangeException) {
175                         }
176                 }
177
178                 [Test]
179 #if FEATURE_NO_BSD_SOCKETS
180                 [ExpectedException (typeof (PlatformNotSupportedException))]
181 #endif
182                 public void Timeout ()
183                 {
184                         try {
185                                 defaultRequest.Timeout = -2;
186                                 Assert.Fail ("#2");
187                         } catch (ArgumentOutOfRangeException) {
188                         }
189                 }
190                 
191                 [Test]
192 #if FEATURE_NO_BSD_SOCKETS
193                 [ExpectedException (typeof (PlatformNotSupportedException))]
194 #endif
195                 public void DefaultValues ()
196                 {
197                         FtpWebRequest request = (FtpWebRequest) WebRequest.Create ("ftp://www.contoso.com");
198                         
199                         Assert.AreEqual (0, request.ContentOffset, "ContentOffset");
200                         Assert.AreEqual (false, request.EnableSsl, "EnableSsl");
201                         // FIXME: Disabled this one by now. KeepAlive is not well supported.
202                         // Assert.AreEqual (true, request.KeepAlive, "KeepAlive");
203                         Assert.AreEqual (WebRequestMethods.Ftp.DownloadFile, request.Method, "#1");
204                         Assert.AreEqual (300000, request.ReadWriteTimeout, "ReadWriteTimeout");
205                         Assert.IsNull (request.RenameTo, "RenameTo");
206                         Assert.AreEqual (true, request.UseBinary, "UseBinary");
207                         Assert.AreEqual (100000, request.Timeout, "Timeout");
208                         Assert.AreEqual (true, request.UsePassive, "UsePassive");
209                 }
210
211                 [Test]
212 #if FEATURE_NO_BSD_SOCKETS
213                 [ExpectedException (typeof (PlatformNotSupportedException))]
214 #endif
215                 public void RenameTo ()
216                 {
217                         try {
218                                 defaultRequest.RenameTo = null;
219                                 Assert.Fail ("#1");
220                         } catch (ArgumentException) {
221                         }
222
223                         try {
224                                 defaultRequest.RenameTo = String.Empty;
225                                 Assert.Fail ("#2");
226                         } catch (ArgumentException) {
227                         }
228                 }
229
230                 [Test]
231 #if FEATURE_NO_BSD_SOCKETS
232                 [ExpectedException (typeof (PlatformNotSupportedException))]
233 #endif
234                 public void UploadFile1_v4 ()
235                 {
236                         UploadFile1 (false);
237                 }
238
239                 [Test]
240 #if FEATURE_NO_BSD_SOCKETS
241                 [ExpectedException (typeof (PlatformNotSupportedException))]
242 #endif
243                 public void UploadFile1_v6 ()
244                 {
245                         if (!Socket.OSSupportsIPv6)
246                                 Assert.Ignore ("IPv6 not supported.");
247
248                         UploadFile1 (true);
249                 }
250
251                 void UploadFile1 (bool ipv6)
252                 {
253                         ServerPut sp = new ServerPut (ipv6);
254                         sp.Start ();
255                         string uri = String.Format ("ftp://{0}:{1}/uploads/file.txt", EncloseIPv6 (sp.IPAddress), sp.Port);
256                         try {
257                                 FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
258                                 ftp.KeepAlive = false;
259                                 ftp.Timeout = 5000;
260                                 ftp.Method = WebRequestMethods.Ftp.UploadFile;
261                                 ftp.ContentLength = 10;
262                                 ftp.UseBinary = true;
263                                 Stream stream = ftp.GetRequestStream ();
264                                 for (int i = 0; i < 10; i++)
265                                         stream.WriteByte ((byte)i);
266                                 stream.Close ();
267                                 FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
268                                 Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "UP#01");
269                                 Assert.AreEqual (10, sp.result.Count, "UP#02");
270                                 response.Close ();
271                         } catch (Exception) {
272                                 if (!String.IsNullOrEmpty (sp.Where))
273                                         throw new Exception (sp.Where);
274                                 throw;
275                         } finally {
276                                 sp.Stop ();
277                         }
278                 }
279
280                 [Test]
281 #if FEATURE_NO_BSD_SOCKETS
282                 [ExpectedException (typeof (PlatformNotSupportedException))]
283 #endif
284                 public void UploadFile_WebClient_v4 ()
285                 {
286                         UploadFile_WebClient (false);
287                 }
288
289                 [Test]
290 #if FEATURE_NO_BSD_SOCKETS
291                 [ExpectedException (typeof (PlatformNotSupportedException))]
292 #endif
293                 public void UploadFile_WebClient_v6 ()
294                 {
295                         if (!Socket.OSSupportsIPv6)
296                                 Assert.Ignore ("IPv6 not supported.");
297
298                         UploadFile_WebClient (true);
299                 }
300
301                 public void UploadFile_WebClient (bool ipv6)
302                 {
303                         ServerPut sp = new ServerPut (ipv6);
304                         File.WriteAllText (_tempFile, "0123456789");
305                         sp.Start ();
306
307                         using (WebClient m_WebClient = new WebClient())
308                         {
309                                 string uri = String.Format ("ftp://{0}:{1}/uploads/file.txt", EncloseIPv6 (sp.IPAddress), sp.Port);
310                                 
311                                 m_WebClient.UploadFile(uri, _tempFile);
312                         }
313                         Assert.AreEqual (10, sp.result.Count, "WebClient/Ftp#01");
314             
315                         sp.Stop ();
316                 }
317
318                 [Test]
319 #if FEATURE_NO_BSD_SOCKETS
320                 [ExpectedException (typeof (PlatformNotSupportedException))]
321 #endif
322                 public void DownloadFile1_v4 ()
323                 {
324                         DownloadFile (new ServerDownload (false));
325                 }
326
327                 [Test]
328 #if FEATURE_NO_BSD_SOCKETS
329                 [ExpectedException (typeof (PlatformNotSupportedException))]
330 #endif
331                 public void DownloadFile1_v6 ()
332                 {
333                         if (!Socket.OSSupportsIPv6)
334                                 Assert.Ignore ("IPv6 not supported.");
335
336                         DownloadFile (new ServerDownload (true));
337                 }
338
339                 void DownloadFile (ServerDownload sp)
340                 {
341                         sp.Start ();
342                         string uri = String.Format ("ftp://{0}:{1}/file.txt", EncloseIPv6 (sp.IPAddress), sp.Port);
343                         try {
344                                 FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
345                                 ftp.KeepAlive = false;
346                                 ftp.Timeout = 5000;
347                                 ftp.Method = WebRequestMethods.Ftp.DownloadFile;
348                                 ftp.UseBinary = true;
349                                 FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
350                                 Assert.IsTrue ((int) response.StatusCode >= 100 && (int) response.StatusCode < 200, "DL#01");
351                                 using (Stream st = response.GetResponseStream ()) {
352                                 }
353                                 // This should be "220 Bye" or similar (no KeepAlive)
354                                 Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DL#02");
355                                 response.Close ();
356                         } catch (Exception) {
357                                 if (!String.IsNullOrEmpty (sp.Where))
358                                         throw new Exception (sp.Where);
359                                 throw;
360                         } finally {
361                                 sp.Stop ();
362                         }
363                 }
364
365                 [Test]
366 #if FEATURE_NO_BSD_SOCKETS
367                 [ExpectedException (typeof (PlatformNotSupportedException))]
368 #endif
369                 public void DownloadFile2_v4 ()
370                 {
371                         // Some embedded FTP servers in Industrial Automation Hardware report
372                         // the PWD using backslashes, but allow forward slashes for CWD.
373                         DownloadFile (new ServerDownload (@"\Users\someuser", "/Users/someuser/", false));
374                 }
375
376                 [Test]
377 #if FEATURE_NO_BSD_SOCKETS
378                 [ExpectedException (typeof (PlatformNotSupportedException))]
379 #endif
380                 public void DownloadFile2_v6 ()
381                 {
382                         // Some embedded FTP servers in Industrial Automation Hardware report
383                         // the PWD using backslashes, but allow forward slashes for CWD.
384                         DownloadFile (new ServerDownload (@"\Users\someuser", "/Users/someuser/", true));
385                 }
386
387                 [Test]
388 #if FEATURE_NO_BSD_SOCKETS
389                 [ExpectedException (typeof (PlatformNotSupportedException))]
390 #endif
391                 public void DeleteFile1_v4 ()
392                 {
393                         DeleteFile1 (false);
394                 }
395
396                 [Test]
397 #if FEATURE_NO_BSD_SOCKETS
398                 [ExpectedException (typeof (PlatformNotSupportedException))]
399 #endif
400                 public void DeleteFile1_v6 ()
401                 {
402                         if (!Socket.OSSupportsIPv6)
403                                 Assert.Ignore ("IPv6 not supported.");
404
405                         DeleteFile1 (true);
406                 }
407
408                 void DeleteFile1 (bool ipv6)
409                 {
410                         ServerDeleteFile sp = new ServerDeleteFile (ipv6);
411                         sp.Start ();
412                         string uri = String.Format ("ftp://{0}:{1}/file.txt", EncloseIPv6 (sp.IPAddress), sp.Port);
413                         try {
414                                 FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
415                                 Console.WriteLine (ftp.RequestUri);
416                                 ftp.KeepAlive = false;
417                                 ftp.Timeout = 5000;
418                                 ftp.Method = WebRequestMethods.Ftp.DeleteFile;
419                                 ftp.UseBinary = true;
420                                 FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
421                                 Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DF#01");
422                                 response.Close ();
423                         } catch (Exception e) {
424                                 Console.WriteLine (e);
425                                 if (!String.IsNullOrEmpty (sp.Where))
426                                         throw new Exception (sp.Where);
427                                 throw;
428                         } finally {
429                                 sp.Stop ();
430                         }
431                 }
432
433                 [Test]
434 #if FEATURE_NO_BSD_SOCKETS
435                 [ExpectedException (typeof (PlatformNotSupportedException))]
436 #endif
437                 public void ListDirectory1_v4 ()
438                 {
439                         ListDirectory1 (false);
440                 }
441
442                 [Test]
443 #if FEATURE_NO_BSD_SOCKETS
444                 [ExpectedException (typeof (PlatformNotSupportedException))]
445 #endif
446                 public void ListDirectory1_v6 ()
447                 {
448                         if (!Socket.OSSupportsIPv6)
449                                 Assert.Ignore ("IPv6 not supported.");
450
451                         ListDirectory1 (true);
452                 }
453
454                 void ListDirectory1 (bool ipv6)
455                 {
456                         ServerListDirectory sp = new ServerListDirectory (ipv6);
457                         sp.Start ();
458                         string uri = String.Format ("ftp://{0}:{1}/somedir/", EncloseIPv6 (sp.IPAddress), sp.Port);
459                         try {
460                                 FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
461                                 Console.WriteLine (ftp.RequestUri);
462                                 ftp.KeepAlive = false;
463                                 ftp.Timeout = 5000;
464                                 ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
465                                 ftp.UseBinary = true;
466                                 using (FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ()) {
467                                         StreamReader reader = new StreamReader (response.GetResponseStream ());
468                                         string result = reader.ReadToEnd ();
469                                         Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DF#01");
470                                 }
471                         } catch (Exception e) {
472                                 Console.WriteLine (e);
473                                 if (!String.IsNullOrEmpty (sp.Where))
474                                         throw new Exception (sp.Where);
475                                 throw;
476                         } finally {
477                                 sp.Stop ();
478                         }
479                 }
480
481                 string EncloseIPv6 (IPAddress address)
482                 {
483                         if (address.AddressFamily == AddressFamily.InterNetwork)
484                                 return address.ToString ();
485                         
486                         return String.Format ("[{0}]", address.ToString ());
487                 }
488
489                 class ServerListDirectory : FtpServer {
490                         public ServerListDirectory (bool ipv6)
491                                 : base (ipv6)
492                         {
493                         }
494
495                         protected override void Run ()
496                         {
497                                 Socket client = control.Accept ();
498                                 NetworkStream ns = new NetworkStream (client, false);
499                                 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
500                                 StreamReader reader = new StreamReader (ns, Encoding.UTF8);
501                                 if (!DoAnonymousLogin (writer, reader)) {
502                                         client.Close ();
503                                         return;
504                                 }
505
506                                 if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/somedir/")) {
507                                         client.Close ();
508                                         return;
509                                 }
510
511                                 string str = reader.ReadLine ();
512                                 string resp = FormatPassiveResponse (str);
513                                 if (resp == null) {
514                                         client.Close ();
515                                         return;
516                                 }
517                                 writer.WriteLine (resp);
518                                 writer.Flush ();
519
520                                 str = reader.ReadLine ();
521                                 if (str != "LIST") {
522                                         Where = "LIST - '" + str + "'";
523                                         client.Close ();
524                                         return;
525                                 }
526                                 writer.WriteLine ("150 Here comes the directory listing");
527                                 writer.Flush ();
528
529                                 Socket data_cnc = data.Accept ();
530                                 byte [] dontcare = Encoding.ASCII.GetBytes ("drwxr-xr-x    2 ftp      ftp          4096 Oct 27 20:17 tests");
531                                 data_cnc.Send (dontcare, 1, SocketFlags.None);
532                                 data_cnc.Close ();
533                                 writer.WriteLine ("226 Directory send Ok");
534                                 writer.Flush ();
535                                 if (!EndConversation (writer, reader)) {
536                                         client.Close ();
537                                         return;
538                                 }
539                                 client.Close ();
540                         }
541                 }
542
543                 class ServerDeleteFile : FtpServer {
544                         public ServerDeleteFile (bool ipv6)
545                                 : base (ipv6)
546                         {
547                         }
548
549                         protected override void Run ()
550                         {
551                                 Socket client = control.Accept ();
552                                 NetworkStream ns = new NetworkStream (client, false);
553                                 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
554                                 StreamReader reader = new StreamReader (ns, Encoding.UTF8);
555                                 if (!DoAnonymousLogin (writer, reader)) {
556                                         client.Close ();
557                                         return;
558                                 }
559
560                                 if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/")) {
561                                         client.Close ();
562                                         return;
563                                 }
564
565                                 string str = reader.ReadLine ();
566                                 if (str.Trim () != "DELE file.txt") {
567                                         Where = "DELE - " + str;
568                                         client.Close ();
569                                         return;
570                                 }
571                                 writer.WriteLine ("250 Delete operation successful");
572                                 writer.Flush ();
573                                 if (!EndConversation (writer, reader)) {
574                                         client.Close ();
575                                         return;
576                                 }
577                                 client.Close ();
578                         }
579                 }
580
581                 class ServerDownload : FtpServer {
582
583                         string Pwd, Cwd;
584
585                         public ServerDownload (bool ipv6)
586                                 : this (null, null, ipv6)
587                         {
588                         }
589
590                         public ServerDownload (string pwd, string cwd, bool ipv6)
591                                 : base (ipv6)
592                         {
593                                 Pwd = pwd ?? "/home/someuser";
594                                 Cwd = cwd ?? "/home/someuser/";
595                         }
596
597                         protected override void Run ()
598                         {
599                                 Socket client = control.Accept ();
600                                 NetworkStream ns = new NetworkStream (client, false);
601                                 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
602                                 StreamReader reader = new StreamReader (ns, Encoding.UTF8);
603                                 if (!DoAnonymousLogin (writer, reader)) {
604                                         client.Close ();
605                                         return;
606                                 }
607
608                                 if (!DoInitialDialog (writer, reader, Pwd, Cwd)) {
609                                         client.Close ();
610                                         return;
611                                 }
612
613                                 string str = reader.ReadLine ();
614                                 string resp = FormatPassiveResponse (str);
615                                 if (resp == null) {
616                                         client.Close ();
617                                         return;
618                                 }
619                                 writer.WriteLine (resp);
620                                 writer.Flush ();
621
622                                 str = reader.ReadLine ();
623                                 if (str != "RETR file.txt") {
624                                         Where = "RETR - " + str;
625                                         client.Close ();
626                                         return;
627                                 }
628                                 writer.WriteLine ("150 Opening BINARY mode data connection for blah (n bytes)");
629                                 writer.Flush ();
630
631                                 Socket data_cnc = data.Accept ();
632                                 byte [] dontcare = new byte [1];
633                                 data_cnc.Receive (dontcare, 1, SocketFlags.None);
634                                 data_cnc.Close ();
635                                 writer.WriteLine ("226 File send Ok");
636                                 writer.Flush ();
637                                 if (!EndConversation (writer, reader)) {
638                                         client.Close ();
639                                         return;
640                                 }
641                                 client.Close ();
642                         }
643                 }
644
645                 class ServerPut : FtpServer {
646                         public List<byte> result = new List<byte> ();
647                         
648                         public ServerPut (bool ipv6)
649                                 : base (ipv6)
650                         {
651                         }
652
653                         protected override void Run ()
654                         {
655                                 Socket client = control.Accept ();
656                                 NetworkStream ns = new NetworkStream (client, false);
657                                 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
658                                 StreamReader reader = new StreamReader (ns, Encoding.UTF8);
659                                 if (!DoAnonymousLogin (writer, reader)) {
660                                         client.Close ();
661                                         return;
662                                 }
663
664                                 if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/uploads/")) {
665                                         client.Close ();
666                                         return;
667                                 }
668
669                                 string str = reader.ReadLine ();
670                                 string resp = FormatPassiveResponse (str);
671                                 if (resp == null) {
672                                         client.Close ();
673                                         return;
674                                 }
675                                 writer.WriteLine (resp);
676                                 writer.Flush ();
677
678                                 str = reader.ReadLine ();
679                                 if (str != "STOR file.txt") {
680                                         Where = "STOR - " + str;
681                                         client.Close ();
682                                         return;
683                                 }
684                                 writer.WriteLine ("150 Ok to send data");
685                                 writer.Flush ();
686
687                                 Socket data_cnc = data.Accept ();
688                                 var datastr = new NetworkStream (data_cnc, false);
689                                 int ch;
690                                 while ((ch = datastr.ReadByte ()) != -1){
691                                         result.Add ((byte)ch);
692
693                                 }
694                                 data_cnc.Close ();
695                                 writer.WriteLine ("226 File received Ok");
696                                 writer.Flush ();
697                                 if (!EndConversation (writer, reader)) {
698                                         client.Close ();
699                                         return;
700                                 }
701                                 client.Close ();
702                         }
703                 }
704
705                 abstract class FtpServer {
706                         protected Socket control;
707                         protected Socket data;
708                         protected ManualResetEvent evt;
709                         protected bool ipv6;
710                         public string Where = "";
711
712                         public FtpServer (bool ipv6)
713                         {
714                                 control = new Socket (ipv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
715                                 control.Bind (new IPEndPoint (ipv6 ? IPAddress.IPv6Loopback : IPAddress.Loopback, 0));
716                                 control.Listen (1);
717                                 data = new Socket (ipv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
718                                 data.Bind (new IPEndPoint (ipv6 ? IPAddress.IPv6Loopback : IPAddress.Loopback, 0));
719                                 data.Listen (1);
720                                 this.ipv6 = ipv6;
721                         }
722
723                         public void Start ()
724                         {
725                                 evt = new ManualResetEvent (false);
726                                 Thread th = new Thread (new ThreadStart (Run));
727                                 th.Start ();
728                         }
729
730                         public void Stop ()
731                         {
732                                 evt.Set ();
733                                 data.Close ();
734                                 control.Close ();
735                         }
736
737                         // PWD, CWD and TYPE I (type could be moved out of here)
738                         protected bool DoInitialDialog (StreamWriter writer, StreamReader reader, string pwd, string cwd)
739                         {
740                                 string str = reader.ReadLine ();
741                                 if (!str.StartsWith ("OPTS utf8 on")) {
742                                         Where = "OPTS utf8 - " + str;
743                                         return false;
744                                 }
745                                 writer.WriteLine ("200 Always in UTF8 mode"); // vsftpd
746                                 writer.Flush ();
747                                 str = reader.ReadLine ();
748                                 if (!str.StartsWith ("PWD")) {
749                                         Where = "PWD - " + str;
750                                         return false;
751                                 }
752                                 writer.WriteLine ("257 \"{0}\"", pwd);
753                                 writer.Flush ();
754                                 str = reader.ReadLine ();
755                                 if (str != ("CWD " + cwd)) {
756                                         Where = "CWD - " + str;
757                                         return false;
758                                 }
759                                 writer.WriteLine ("250 Directory changed");
760                                 writer.Flush ();
761                                 str = reader.ReadLine ();
762                                 if (str != ("TYPE I")) {
763                                         Where = "TYPE - " + str;
764                                         return false;
765                                 }
766                                 writer.WriteLine ("200 Switching to binary mode");
767                                 writer.Flush ();
768                                 return true;
769                         }
770
771                         protected bool EndConversation (StreamWriter writer, StreamReader reader)
772                         {
773                                 string str = reader.ReadLine ();
774                                 if (str != "QUIT") {
775                                         Where = "QUIT";
776                                         return false;
777                                 }
778                                 writer.WriteLine ("220 Bye");
779                                 writer.Flush ();
780                                 Thread.Sleep (250);
781                                 return true;
782                         }
783
784                         protected bool DoAnonymousLogin (StreamWriter writer, StreamReader reader)
785                         {
786                                 writer.WriteLine ("220 Welcome to the jungle");
787                                 writer.Flush ();
788                                 string str = reader.ReadLine ();
789                                 if (!str.StartsWith ("USER ")) {
790                                         Where = "USER";
791                                         return false;
792                                 }
793                                 writer.WriteLine ("331 Say 'Mellon'");
794                                 writer.Flush ();
795                                 str = reader.ReadLine ();
796                                 if (!str.StartsWith ("PASS ")) {
797                                         Where = "PASS";
798                                         return false;
799                                 }
800                                 writer.WriteLine ("230 Logged in");
801                                 writer.Flush ();
802                                 return true;
803                         }
804
805                         protected string FormatPassiveResponse (string request)
806                         {
807                                 if (ipv6) {
808                                         if (request != "EPSV") {
809                                                 Where = "EPSV";
810                                                 return null;
811                                         }
812
813                                         IPEndPoint end_data = (IPEndPoint) data.LocalEndPoint;
814                                         return String.Format ("229 Extended Passive (|||{0}|)", end_data.Port);
815                                 }
816                                 else {
817                                         if (request != "PASV") {
818                                                 Where = "PASV";
819                                                 return null;
820                                         }
821
822                                         IPEndPoint end_data = (IPEndPoint) data.LocalEndPoint;
823                                         byte [] addr_bytes = end_data.Address.GetAddressBytes ();
824                                         byte [] port = new byte [2];
825                                         port[0] = (byte) ((end_data.Port >> 8) & 255);
826                                         port[1] = (byte) (end_data.Port & 255);
827                                         StringBuilder sb = new StringBuilder ("227 Passive (");
828                                         foreach (byte b in addr_bytes) {
829                                                 sb.AppendFormat ("{0},", b);    
830                                         }
831                                         sb.AppendFormat ("{0},", port [0]);     
832                                         sb.AppendFormat ("{0})", port [1]);     
833                                         return sb.ToString ();
834                                 }
835                         }
836
837                         public IPAddress IPAddress {
838                                 get { return ((IPEndPoint) control.LocalEndPoint).Address; }
839                         }
840                         
841                         public int Port {
842                                 get { return ((IPEndPoint) control.LocalEndPoint).Port; }
843                         }
844
845                         protected abstract void Run ();
846                 }
847         }
848 }
849
850