Guest User

Untitled

a guest
Feb 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. public class LocalServer
  2. {
  3. public bool runListener = true;
  4. private string prefix;
  5. private string baseDirectory = "M:/ftp backup/";
  6.  
  7. public LocalServer(int port)
  8. {
  9. prefix = string.Format("http://+:{0}/", port);
  10. Thread listenerThread = new Thread(LocalServerThread);
  11. listenerThread.Start();
  12. }
  13.  
  14. public void LocalServerThread()
  15. {
  16. HttpListener listener = new HttpListener();
  17. listener.Prefixes.Add(prefix);
  18. listener.Start();
  19. Debug.Log(DateTime.Now.ToString(SimpleHTTPServer.dateFormat) + "<color=green>Started local web server</color>");
  20. while (runListener)
  21. {
  22. IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
  23. result.AsyncWaitHandle.WaitOne();
  24. }
  25. listener.Close();
  26. Debug.Log(DateTime.Now.ToString(SimpleHTTPServer.dateFormat) + "<color=green>Stopped local web server</color>");
  27. }
  28.  
  29. public void ListenerCallback(IAsyncResult result)
  30. {
  31. HttpListener listener = (HttpListener)result.AsyncState;
  32. HttpListenerContext context = listener.EndGetContext(result);
  33. string fileNameComplete = context.Request.Url.AbsolutePath.Substring(1).Replace("%20", " "); //removing the first slash and replacing the %20 web prefix by space
  34. string pathComplete = baseDirectory + fileNameComplete;
  35. Debug.Log(DateTime.Now.ToString(SimpleHTTPServer.dateFormat) + pathComplete + " requested to LocalServer");
  36. using (Stream fileStream = new FileStream(pathComplete, FileMode.Open))
  37. {
  38. byte[] buffer = new byte[fileStream.Length];
  39. fileStream.Read(buffer, 0, (int)fileStream.Length);
  40. context.Response.ContentLength64 = fileStream.Length;
  41. context.Response.ContentType = SimpleHTTPServer.GetMimeType(fileNameComplete);
  42.  
  43. //personal headers
  44. context.Response.StatusCode = (int)HttpStatusCode.OK;
  45. ////apache headers
  46. //context.Response.StatusDescription = "OK";
  47. //context.Response.ProtocolVersion = new Version("1.1");
  48. //context.Response.SendChunked = false;
  49. //context.Response.KeepAlive = true;
  50. //context.Response.AddHeader("Accept-Ranges", "bytes");
  51. //context.Response.AddHeader("ETag", BinarySocket.RandomString(20));
  52. //context.Response.AddHeader("Connection", "Keep-Alive");
  53. //context.Response.AddHeader("Keep-Alive", "timeout=5, max=100");
  54. ////CORS
  55. //context.Response.AddHeader("Access-Control-Allow-Origin", "*");
  56. //context.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
  57. //context.Response.AddHeader("Access-Control-Allow-Headers", "X-Requested-With");
  58. //context.Response.AddHeader("Access-Control-Max-Age", "86400");
  59.  
  60. context.Response.OutputStream.Write(buffer, 0, buffer.Length);
  61. context.Response.Close();
  62. }
  63. }
  64. }
Add Comment
Please, Sign In to add comment