fork download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. public static void Main()
  6. {
  7. Console.WriteLine("www.example.com > " + Parse("www.example.com"));
  8. Console.WriteLine("://www.example.com > " + Parse("://www.example.com"));
  9. Console.WriteLine("http://w...content-available-to-author-only...e.com > " + Parse("http://w...content-available-to-author-only...e.com"));
  10. Console.WriteLine("https://w...content-available-to-author-only...e.com > " + Parse("https://w...content-available-to-author-only...e.com"));
  11. Console.WriteLine("ftp://w...content-available-to-author-only...e.com > " + Parse("ftp://w...content-available-to-author-only...e.com"));
  12. Console.WriteLine("rubbish://www.example.com > " + Parse("rubbish://www.example.com"));
  13. Console.WriteLine("https://rubbish://www.example.com > " + Parse("https://rubbish://www.example.com"));
  14. Console.WriteLine("rubbish://morerubbish://www.example.com > " + Parse("rubbish://morerubbish://www.example.com"));
  15. }
  16.  
  17. static string Parse(string downloadURL)
  18. {
  19. string[] approvedSchemes = new string[] { "http", "https", "ftp" };
  20. string userScheme = "";
  21.  
  22. if(downloadURL.Contains("://"))
  23. {
  24. // Get the first scheme defined, we will use this if it is in the approved list.
  25. userScheme = downloadURL.Substring(0, downloadURL.IndexOf("://"));
  26. // To cater for multiple :// remove all of them
  27. downloadURL = downloadURL.Substring(downloadURL.LastIndexOf("://") + 3);
  28. }
  29.  
  30. // Check if the user defined scheme is in the approved list, if not then set to http.
  31. if(Array.IndexOf(approvedSchemes, userScheme.ToLowerInvariant()) > -1)
  32. downloadURL = userScheme + "://" + downloadURL;
  33. else
  34. downloadURL = "http://" + downloadURL;
  35.  
  36. return downloadURL;
  37. }
  38. }
Success #stdin #stdout 0.06s 34072KB
stdin
Standard input is empty
stdout
www.example.com > http://w...content-available-to-author-only...e.com
://www.example.com > http://w...content-available-to-author-only...e.com
http://w...content-available-to-author-only...e.com > http://w...content-available-to-author-only...e.com
https://w...content-available-to-author-only...e.com > https://w...content-available-to-author-only...e.com
ftp://w...content-available-to-author-only...e.com > ftp://w...content-available-to-author-only...e.com
rubbish://www.example.com > http://w...content-available-to-author-only...e.com
https://rubbish://www.example.com > https://w...content-available-to-author-only...e.com
rubbish://morerubbish://www.example.com > http://w...content-available-to-author-only...e.com