FtpWebRequestTest.cs 19 KB

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