FtpWebRequestTest.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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_v4 ()
  212. {
  213. UploadFile1 (false);
  214. }
  215. [Test]
  216. #if FEATURE_NO_BSD_SOCKETS
  217. [ExpectedException (typeof (PlatformNotSupportedException))]
  218. #endif
  219. public void UploadFile1_v6 ()
  220. {
  221. if (!Socket.OSSupportsIPv6)
  222. Assert.Ignore ("IPv6 not supported.");
  223. UploadFile1 (true);
  224. }
  225. void UploadFile1 (bool ipv6)
  226. {
  227. ServerPut sp = new ServerPut (ipv6);
  228. sp.Start ();
  229. string uri = String.Format ("ftp://{0}:{1}/uploads/file.txt", EncloseIPv6 (sp.IPAddress), sp.Port);
  230. try {
  231. FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
  232. ftp.KeepAlive = false;
  233. ftp.Timeout = 5000;
  234. ftp.Method = WebRequestMethods.Ftp.UploadFile;
  235. ftp.ContentLength = 10;
  236. ftp.UseBinary = true;
  237. Stream stream = ftp.GetRequestStream ();
  238. for (int i = 0; i < 10; i++)
  239. stream.WriteByte ((byte)i);
  240. stream.Close ();
  241. FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
  242. Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "UP#01");
  243. Assert.AreEqual (10, sp.result.Count, "UP#02");
  244. response.Close ();
  245. } catch (Exception) {
  246. if (!String.IsNullOrEmpty (sp.Where))
  247. throw new Exception (sp.Where);
  248. throw;
  249. } finally {
  250. sp.Stop ();
  251. }
  252. }
  253. [Test]
  254. #if FEATURE_NO_BSD_SOCKETS
  255. [ExpectedException (typeof (PlatformNotSupportedException))]
  256. #endif
  257. public void UploadFile_WebClient_v4 ()
  258. {
  259. UploadFile_WebClient (false);
  260. }
  261. [Test]
  262. #if FEATURE_NO_BSD_SOCKETS
  263. [ExpectedException (typeof (PlatformNotSupportedException))]
  264. #endif
  265. public void UploadFile_WebClient_v6 ()
  266. {
  267. if (!Socket.OSSupportsIPv6)
  268. Assert.Ignore ("IPv6 not supported.");
  269. UploadFile_WebClient (true);
  270. }
  271. public void UploadFile_WebClient (bool ipv6)
  272. {
  273. ServerPut sp = new ServerPut (ipv6);
  274. File.WriteAllText (_tempFile, "0123456789");
  275. sp.Start ();
  276. using (WebClient m_WebClient = new WebClient())
  277. {
  278. string uri = String.Format ("ftp://{0}:{1}/uploads/file.txt", EncloseIPv6 (sp.IPAddress), sp.Port);
  279. m_WebClient.UploadFile(uri, _tempFile);
  280. }
  281. Assert.AreEqual (10, sp.result.Count, "WebClient/Ftp#01");
  282. sp.Stop ();
  283. }
  284. [Test]
  285. #if FEATURE_NO_BSD_SOCKETS
  286. [ExpectedException (typeof (PlatformNotSupportedException))]
  287. #endif
  288. public void DownloadFile1_v4 ()
  289. {
  290. DownloadFile (new ServerDownload (false));
  291. }
  292. [Test]
  293. #if FEATURE_NO_BSD_SOCKETS
  294. [ExpectedException (typeof (PlatformNotSupportedException))]
  295. #endif
  296. public void DownloadFile1_v6 ()
  297. {
  298. if (!Socket.OSSupportsIPv6)
  299. Assert.Ignore ("IPv6 not supported.");
  300. DownloadFile (new ServerDownload (true));
  301. }
  302. [Test]
  303. #if FEATURE_NO_BSD_SOCKETS
  304. [ExpectedException (typeof (PlatformNotSupportedException))]
  305. #endif
  306. public void DownloadFileNonLatinChars ()
  307. {
  308. string filename = "\u0411\u0430\u0448\u043DRowan-\u041F\u0435\u0441\u043D\u043F\u0440\u043E\u043C\u043E\u043D\u0430\u0445\u0430\u0422\u0435\u043E\u0434\u043E\u0440\u0443\u0441\u0430\u0438\u0437\u0413\u0430\u043C\u043C\u0435\u043B\u044C\u043D\u0430.mp3";
  309. DownloadFile (new ServerDownload (null, null, filename, false), "ftp://{0}:{1}/" + filename);
  310. }
  311. void DownloadFile (ServerDownload sp, string uriTemplate = "ftp://{0}:{1}/file.txt")
  312. {
  313. sp.Start ();
  314. string uri = String.Format (uriTemplate, EncloseIPv6 (sp.IPAddress), sp.Port);
  315. try {
  316. FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
  317. ftp.KeepAlive = false;
  318. ftp.Timeout = 5000;
  319. ftp.Method = WebRequestMethods.Ftp.DownloadFile;
  320. ftp.UseBinary = true;
  321. FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
  322. Assert.IsTrue ((int) response.StatusCode >= 100 && (int) response.StatusCode < 200, "DL#01");
  323. using (Stream st = response.GetResponseStream ()) {
  324. }
  325. // This should be "220 Bye" or similar (no KeepAlive)
  326. Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DL#02");
  327. response.Close ();
  328. } catch (Exception) {
  329. if (!String.IsNullOrEmpty (sp.Where))
  330. throw new Exception (sp.Where);
  331. throw;
  332. } finally {
  333. sp.Stop ();
  334. }
  335. }
  336. [Test]
  337. #if FEATURE_NO_BSD_SOCKETS
  338. [ExpectedException (typeof (PlatformNotSupportedException))]
  339. #endif
  340. public void DownloadFile2_v4 ()
  341. {
  342. // Some embedded FTP servers in Industrial Automation Hardware report
  343. // the PWD using backslashes, but allow forward slashes for CWD.
  344. DownloadFile (new ServerDownload (@"\Users\someuser", "/Users/someuser/", null, false));
  345. }
  346. [Test]
  347. #if FEATURE_NO_BSD_SOCKETS
  348. [ExpectedException (typeof (PlatformNotSupportedException))]
  349. #endif
  350. public void DownloadFile2_v6 ()
  351. {
  352. if (!Socket.OSSupportsIPv6)
  353. Assert.Ignore ("IPv6 not supported.");
  354. // Some embedded FTP servers in Industrial Automation Hardware report
  355. // the PWD using backslashes, but allow forward slashes for CWD.
  356. DownloadFile (new ServerDownload (@"\Users\someuser", "/Users/someuser/", null, true));
  357. }
  358. [Test]
  359. #if FEATURE_NO_BSD_SOCKETS
  360. [ExpectedException (typeof (PlatformNotSupportedException))]
  361. #endif
  362. public void DeleteFile1_v4 ()
  363. {
  364. DeleteFile1 (false);
  365. }
  366. [Test]
  367. #if FEATURE_NO_BSD_SOCKETS
  368. [ExpectedException (typeof (PlatformNotSupportedException))]
  369. #endif
  370. public void DeleteFile1_v6 ()
  371. {
  372. if (!Socket.OSSupportsIPv6)
  373. Assert.Ignore ("IPv6 not supported.");
  374. DeleteFile1 (true);
  375. }
  376. void DeleteFile1 (bool ipv6)
  377. {
  378. ServerDeleteFile sp = new ServerDeleteFile (ipv6);
  379. sp.Start ();
  380. string uri = String.Format ("ftp://{0}:{1}/file.txt", EncloseIPv6 (sp.IPAddress), sp.Port);
  381. try {
  382. FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
  383. Console.WriteLine (ftp.RequestUri);
  384. ftp.KeepAlive = false;
  385. ftp.Timeout = 5000;
  386. ftp.Method = WebRequestMethods.Ftp.DeleteFile;
  387. ftp.UseBinary = true;
  388. FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
  389. Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DF#01");
  390. response.Close ();
  391. } catch (Exception e) {
  392. Console.WriteLine (e);
  393. if (!String.IsNullOrEmpty (sp.Where))
  394. throw new Exception (sp.Where);
  395. throw;
  396. } finally {
  397. sp.Stop ();
  398. }
  399. }
  400. [Test]
  401. #if FEATURE_NO_BSD_SOCKETS
  402. [ExpectedException (typeof (PlatformNotSupportedException))]
  403. #endif
  404. public void ListDirectory1_v4 ()
  405. {
  406. ListDirectory1 (false);
  407. }
  408. [Test]
  409. #if FEATURE_NO_BSD_SOCKETS
  410. [ExpectedException (typeof (PlatformNotSupportedException))]
  411. #endif
  412. public void ListDirectory1_v6 ()
  413. {
  414. if (!Socket.OSSupportsIPv6)
  415. Assert.Ignore ("IPv6 not supported.");
  416. ListDirectory1 (true);
  417. }
  418. void ListDirectory1 (bool ipv6)
  419. {
  420. ServerListDirectory sp = new ServerListDirectory (ipv6);
  421. sp.Start ();
  422. string uri = String.Format ("ftp://{0}:{1}/somedir/", EncloseIPv6 (sp.IPAddress), sp.Port);
  423. try {
  424. FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
  425. Console.WriteLine (ftp.RequestUri);
  426. ftp.KeepAlive = false;
  427. ftp.Timeout = 5000;
  428. ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
  429. ftp.UseBinary = true;
  430. using (FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ()) {
  431. StreamReader reader = new StreamReader (response.GetResponseStream ());
  432. string result = reader.ReadToEnd ();
  433. Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DF#01");
  434. }
  435. } catch (Exception e) {
  436. Console.WriteLine (e);
  437. if (!String.IsNullOrEmpty (sp.Where))
  438. throw new Exception (sp.Where);
  439. throw;
  440. } finally {
  441. sp.Stop ();
  442. }
  443. }
  444. string EncloseIPv6 (IPAddress address)
  445. {
  446. if (address.AddressFamily == AddressFamily.InterNetwork)
  447. return address.ToString ();
  448. return String.Format ("[{0}]", address.ToString ());
  449. }
  450. class ServerListDirectory : FtpServer {
  451. public ServerListDirectory (bool ipv6)
  452. : base (ipv6)
  453. {
  454. }
  455. protected override void Run ()
  456. {
  457. Socket client = control.Accept ();
  458. NetworkStream ns = new NetworkStream (client, false);
  459. StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
  460. StreamReader reader = new StreamReader (ns, Encoding.UTF8);
  461. if (!DoAnonymousLogin (writer, reader)) {
  462. client.Close ();
  463. return;
  464. }
  465. if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/somedir/")) {
  466. client.Close ();
  467. return;
  468. }
  469. string str = reader.ReadLine ();
  470. string resp = FormatPassiveResponse (str);
  471. if (resp == null) {
  472. client.Close ();
  473. return;
  474. }
  475. writer.WriteLine (resp);
  476. writer.Flush ();
  477. str = reader.ReadLine ();
  478. if (str != "LIST") {
  479. Where = "LIST - '" + str + "'";
  480. client.Close ();
  481. return;
  482. }
  483. writer.WriteLine ("150 Here comes the directory listing");
  484. writer.Flush ();
  485. Socket data_cnc = data.Accept ();
  486. byte [] dontcare = Encoding.ASCII.GetBytes ("drwxr-xr-x 2 ftp ftp 4096 Oct 27 20:17 tests");
  487. data_cnc.Send (dontcare, 1, SocketFlags.None);
  488. data_cnc.Close ();
  489. writer.WriteLine ("226 Directory send Ok");
  490. writer.Flush ();
  491. if (!EndConversation (writer, reader)) {
  492. client.Close ();
  493. return;
  494. }
  495. client.Close ();
  496. }
  497. }
  498. class ServerDeleteFile : FtpServer {
  499. public ServerDeleteFile (bool ipv6)
  500. : base (ipv6)
  501. {
  502. }
  503. protected override void Run ()
  504. {
  505. Socket client = control.Accept ();
  506. NetworkStream ns = new NetworkStream (client, false);
  507. StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
  508. StreamReader reader = new StreamReader (ns, Encoding.UTF8);
  509. if (!DoAnonymousLogin (writer, reader)) {
  510. client.Close ();
  511. return;
  512. }
  513. if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/")) {
  514. client.Close ();
  515. return;
  516. }
  517. string str = reader.ReadLine ();
  518. if (str.Trim () != "DELE file.txt") {
  519. Where = "DELE - " + str;
  520. client.Close ();
  521. return;
  522. }
  523. writer.WriteLine ("250 Delete operation successful");
  524. writer.Flush ();
  525. if (!EndConversation (writer, reader)) {
  526. client.Close ();
  527. return;
  528. }
  529. client.Close ();
  530. }
  531. }
  532. class ServerDownload : FtpServer {
  533. string Pwd, Cwd, Filename;
  534. public ServerDownload (bool ipv6)
  535. : this (null, null, null, ipv6)
  536. {
  537. }
  538. public ServerDownload (string pwd, string cwd, string filename, bool ipv6)
  539. : base (ipv6)
  540. {
  541. Pwd = pwd ?? "/home/someuser";
  542. Cwd = cwd ?? "/home/someuser/";
  543. Filename = filename ?? "file.txt";
  544. }
  545. protected override void Run ()
  546. {
  547. Socket client = control.Accept ();
  548. NetworkStream ns = new NetworkStream (client, false);
  549. StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
  550. StreamReader reader = new StreamReader (ns, Encoding.UTF8);
  551. if (!DoAnonymousLogin (writer, reader)) {
  552. client.Close ();
  553. return;
  554. }
  555. if (!DoInitialDialog (writer, reader, Pwd, Cwd)) {
  556. client.Close ();
  557. return;
  558. }
  559. string str = reader.ReadLine ();
  560. string resp = FormatPassiveResponse (str);
  561. if (resp == null) {
  562. client.Close ();
  563. return;
  564. }
  565. writer.WriteLine (resp);
  566. writer.Flush ();
  567. str = reader.ReadLine ();
  568. if (str != $"RETR {Filename}") {
  569. Where = $"RETR - got: {str}, expected: RETR {Filename}";
  570. client.Close ();
  571. return;
  572. }
  573. writer.WriteLine ("150 Opening BINARY mode data connection for blah (n bytes)");
  574. writer.Flush ();
  575. Socket data_cnc = data.Accept ();
  576. byte [] dontcare = new byte [1];
  577. data_cnc.Receive (dontcare, 1, SocketFlags.None);
  578. data_cnc.Close ();
  579. writer.WriteLine ("226 File send Ok");
  580. writer.Flush ();
  581. if (!EndConversation (writer, reader)) {
  582. client.Close ();
  583. return;
  584. }
  585. client.Close ();
  586. }
  587. }
  588. class ServerPut : FtpServer {
  589. public List<byte> result = new List<byte> ();
  590. public ServerPut (bool ipv6)
  591. : base (ipv6)
  592. {
  593. }
  594. protected override void Run ()
  595. {
  596. Socket client = control.Accept ();
  597. NetworkStream ns = new NetworkStream (client, false);
  598. StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
  599. StreamReader reader = new StreamReader (ns, Encoding.UTF8);
  600. if (!DoAnonymousLogin (writer, reader)) {
  601. client.Close ();
  602. return;
  603. }
  604. if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/uploads/")) {
  605. client.Close ();
  606. return;
  607. }
  608. string str = reader.ReadLine ();
  609. string resp = FormatPassiveResponse (str);
  610. if (resp == null) {
  611. client.Close ();
  612. return;
  613. }
  614. writer.WriteLine (resp);
  615. writer.Flush ();
  616. str = reader.ReadLine ();
  617. if (str != "STOR file.txt") {
  618. Where = "STOR - " + str;
  619. client.Close ();
  620. return;
  621. }
  622. writer.WriteLine ("150 Ok to send data");
  623. writer.Flush ();
  624. Socket data_cnc = data.Accept ();
  625. var datastr = new NetworkStream (data_cnc, false);
  626. int ch;
  627. while ((ch = datastr.ReadByte ()) != -1){
  628. result.Add ((byte)ch);
  629. }
  630. data_cnc.Close ();
  631. writer.WriteLine ("226 File received Ok");
  632. writer.Flush ();
  633. if (!EndConversation (writer, reader)) {
  634. client.Close ();
  635. return;
  636. }
  637. client.Close ();
  638. }
  639. }
  640. abstract class FtpServer {
  641. protected Socket control;
  642. protected Socket data;
  643. protected ManualResetEvent evt;
  644. protected bool ipv6;
  645. public string Where = "";
  646. public FtpServer (bool ipv6)
  647. {
  648. control = new Socket (ipv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  649. control.Bind (new IPEndPoint (ipv6 ? IPAddress.IPv6Loopback : IPAddress.Loopback, 0));
  650. control.Listen (1);
  651. data = new Socket (ipv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  652. data.Bind (new IPEndPoint (ipv6 ? IPAddress.IPv6Loopback : IPAddress.Loopback, 0));
  653. data.Listen (1);
  654. this.ipv6 = ipv6;
  655. }
  656. public void Start ()
  657. {
  658. evt = new ManualResetEvent (false);
  659. Thread th = new Thread (new ThreadStart (Run));
  660. th.Start ();
  661. }
  662. public void Stop ()
  663. {
  664. evt.Set ();
  665. data.Close ();
  666. control.Close ();
  667. }
  668. // PWD, CWD and TYPE I (type could be moved out of here)
  669. protected bool DoInitialDialog (StreamWriter writer, StreamReader reader, string pwd, string cwd)
  670. {
  671. string str = reader.ReadLine ();
  672. if (!str.StartsWith ("OPTS utf8 on")) {
  673. Where = "OPTS utf8 - " + str;
  674. return false;
  675. }
  676. writer.WriteLine ("200 Always in UTF8 mode"); // vsftpd
  677. writer.Flush ();
  678. str = reader.ReadLine ();
  679. if (!str.StartsWith ("PWD")) {
  680. Where = "PWD - " + str;
  681. return false;
  682. }
  683. writer.WriteLine ("257 \"{0}\"", pwd);
  684. writer.Flush ();
  685. str = reader.ReadLine ();
  686. if (str != ("CWD " + cwd)) {
  687. Where = "CWD - " + str;
  688. return false;
  689. }
  690. writer.WriteLine ("250 Directory changed");
  691. writer.Flush ();
  692. str = reader.ReadLine ();
  693. if (str != ("TYPE I")) {
  694. Where = "TYPE - " + str;
  695. return false;
  696. }
  697. writer.WriteLine ("200 Switching to binary mode");
  698. writer.Flush ();
  699. return true;
  700. }
  701. protected bool EndConversation (StreamWriter writer, StreamReader reader)
  702. {
  703. string str = reader.ReadLine ();
  704. if (str != "QUIT") {
  705. Where = "QUIT";
  706. return false;
  707. }
  708. writer.WriteLine ("220 Bye");
  709. writer.Flush ();
  710. Thread.Sleep (250);
  711. return true;
  712. }
  713. protected bool DoAnonymousLogin (StreamWriter writer, StreamReader reader)
  714. {
  715. writer.WriteLine ("220 Welcome to the jungle");
  716. writer.Flush ();
  717. string str = reader.ReadLine ();
  718. if (!str.StartsWith ("USER ")) {
  719. Where = "USER";
  720. return false;
  721. }
  722. writer.WriteLine ("331 Say 'Mellon'");
  723. writer.Flush ();
  724. str = reader.ReadLine ();
  725. if (!str.StartsWith ("PASS ")) {
  726. Where = "PASS";
  727. return false;
  728. }
  729. writer.WriteLine ("230 Logged in");
  730. writer.Flush ();
  731. return true;
  732. }
  733. protected string FormatPassiveResponse (string request)
  734. {
  735. if (ipv6) {
  736. if (request != "EPSV") {
  737. Where = "EPSV";
  738. return null;
  739. }
  740. IPEndPoint end_data = (IPEndPoint) data.LocalEndPoint;
  741. return String.Format ("229 Extended Passive (|||{0}|)", end_data.Port);
  742. }
  743. else {
  744. if (request != "PASV") {
  745. Where = "PASV";
  746. return null;
  747. }
  748. IPEndPoint end_data = (IPEndPoint) data.LocalEndPoint;
  749. byte [] addr_bytes = end_data.Address.GetAddressBytes ();
  750. byte [] port = new byte [2];
  751. port[0] = (byte) ((end_data.Port >> 8) & 255);
  752. port[1] = (byte) (end_data.Port & 255);
  753. StringBuilder sb = new StringBuilder ("227 Passive (");
  754. foreach (byte b in addr_bytes) {
  755. sb.AppendFormat ("{0},", b);
  756. }
  757. sb.AppendFormat ("{0},", port [0]);
  758. sb.AppendFormat ("{0})", port [1]);
  759. return sb.ToString ();
  760. }
  761. }
  762. public IPAddress IPAddress {
  763. get { return ((IPEndPoint) control.LocalEndPoint).Address; }
  764. }
  765. public int Port {
  766. get { return ((IPEndPoint) control.LocalEndPoint).Port; }
  767. }
  768. protected abstract void Run ();
  769. }
  770. }
  771. }