You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
7.2 KiB

  1. // MIT License - Copyright (c) 2016 Can Güney Aksakalli
  2. // https://aksakalli.github.io/2014/02/24/simple-http-server-with-csparp.html
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Net.Sockets;
  8. using System.Net;
  9. using System.IO;
  10. using System.Threading;
  11. using System.Diagnostics;
  12. class SimpleHTTPServer
  13. {
  14. private readonly string[] _indexFiles = {
  15. "index.html",
  16. "index.htm",
  17. "default.html",
  18. "default.htm"
  19. };
  20. private static IDictionary<string, string> _mimeTypeMappings = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase) {
  21. #region extension to MIME type list
  22. {".asf", "video/x-ms-asf"},
  23. {".asx", "video/x-ms-asf"},
  24. {".avi", "video/x-msvideo"},
  25. {".bin", "application/octet-stream"},
  26. {".cco", "application/x-cocoa"},
  27. {".crt", "application/x-x509-ca-cert"},
  28. {".css", "text/css"},
  29. {".deb", "application/octet-stream"},
  30. {".der", "application/x-x509-ca-cert"},
  31. {".dll", "application/octet-stream"},
  32. {".dmg", "application/octet-stream"},
  33. {".ear", "application/java-archive"},
  34. {".eot", "application/octet-stream"},
  35. {".exe", "application/octet-stream"},
  36. {".flv", "video/x-flv"},
  37. {".gif", "image/gif"},
  38. {".hqx", "application/mac-binhex40"},
  39. {".htc", "text/x-component"},
  40. {".htm", "text/html"},
  41. {".html", "text/html"},
  42. {".ico", "image/x-icon"},
  43. {".img", "application/octet-stream"},
  44. {".iso", "application/octet-stream"},
  45. {".jar", "application/java-archive"},
  46. {".jardiff", "application/x-java-archive-diff"},
  47. {".jng", "image/x-jng"},
  48. {".jnlp", "application/x-java-jnlp-file"},
  49. {".jpeg", "image/jpeg"},
  50. {".jpg", "image/jpeg"},
  51. {".js", "application/x-javascript"},
  52. {".mml", "text/mathml"},
  53. {".mng", "video/x-mng"},
  54. {".mov", "video/quicktime"},
  55. {".mp3", "audio/mpeg"},
  56. {".mpeg", "video/mpeg"},
  57. {".mpg", "video/mpeg"},
  58. {".msi", "application/octet-stream"},
  59. {".msm", "application/octet-stream"},
  60. {".msp", "application/octet-stream"},
  61. {".pdb", "application/x-pilot"},
  62. {".pdf", "application/pdf"},
  63. {".pem", "application/x-x509-ca-cert"},
  64. {".pl", "application/x-perl"},
  65. {".pm", "application/x-perl"},
  66. {".png", "image/png"},
  67. {".prc", "application/x-pilot"},
  68. {".ra", "audio/x-realaudio"},
  69. {".rar", "application/x-rar-compressed"},
  70. {".rpm", "application/x-redhat-package-manager"},
  71. {".rss", "text/xml"},
  72. {".run", "application/x-makeself"},
  73. {".sea", "application/x-sea"},
  74. {".shtml", "text/html"},
  75. {".sit", "application/x-stuffit"},
  76. {".swf", "application/x-shockwave-flash"},
  77. {".tcl", "application/x-tcl"},
  78. {".tk", "application/x-tcl"},
  79. {".txt", "text/plain"},
  80. {".war", "application/java-archive"},
  81. {".wbmp", "image/vnd.wap.wbmp"},
  82. {".wmv", "video/x-ms-wmv"},
  83. {".xml", "text/xml"},
  84. {".xpi", "application/x-xpinstall"},
  85. {".zip", "application/zip"},
  86. #endregion
  87. };
  88. private Thread _serverThread;
  89. private string _rootDirectory;
  90. private HttpListener _listener;
  91. private int _port;
  92. public int Port {
  93. get { return _port; }
  94. private set { }
  95. }
  96. /// <summary>
  97. /// Construct server with given port.
  98. /// </summary>
  99. /// <param name="path">Directory path to serve.</param>
  100. /// <param name="port">Port of the server.</param>
  101. public SimpleHTTPServer(string path, int port)
  102. {
  103. this.Initialize(path, port);
  104. }
  105. /// <summary>
  106. /// Construct server with suitable port.
  107. /// </summary>
  108. /// <param name="path">Directory path to serve.</param>
  109. public SimpleHTTPServer(string path)
  110. {
  111. //get an empty port
  112. TcpListener l = new TcpListener(IPAddress.Loopback, 0);
  113. l.Start();
  114. int port = ((IPEndPoint)l.LocalEndpoint).Port;
  115. l.Stop();
  116. this.Initialize(path, port);
  117. }
  118. /// <summary>
  119. /// Stop server and dispose all functions.
  120. /// </summary>
  121. public void Stop()
  122. {
  123. _serverThread.Abort();
  124. _listener.Stop();
  125. }
  126. private void Listen()
  127. {
  128. _listener = new HttpListener();
  129. _listener.Prefixes.Add("http://*:" + _port.ToString() + "/");
  130. _listener.Start();
  131. while (true)
  132. {
  133. try
  134. {
  135. HttpListenerContext context = _listener.GetContext();
  136. UnityEngine.Debug.Log("client Connected to webpage");
  137. Process(context);
  138. }
  139. catch (Exception ex)
  140. {
  141. }
  142. }
  143. }
  144. private void Process(HttpListenerContext context)
  145. {
  146. string filename = context.Request.Url.AbsolutePath;
  147. Console.WriteLine(filename);
  148. filename = filename.Substring(1);
  149. if (string.IsNullOrEmpty(filename))
  150. {
  151. foreach (string indexFile in _indexFiles)
  152. {
  153. if (File.Exists(Path.Combine(_rootDirectory, indexFile)))
  154. {
  155. filename = indexFile;
  156. break;
  157. }
  158. }
  159. }
  160. filename = Path.Combine(_rootDirectory, filename);
  161. UnityEngine.Debug.Log("Client wants:" + filename);
  162. if (File.Exists(filename))
  163. {
  164. try
  165. {
  166. Stream input = new FileStream(filename, FileMode.Open);
  167. //Adding permanent http response headers
  168. string mime;
  169. context.Response.ContentType = _mimeTypeMappings.TryGetValue(Path.GetExtension(filename), out mime) ? mime : "application/octet-stream";
  170. context.Response.AddHeader("Date", DateTime.Now.ToString("r"));
  171. context.Response.AddHeader("Last-Modified", System.IO.File.GetLastWriteTime(filename).ToString("r"));
  172. if (Path.GetExtension(filename) == ".unityweb")
  173. context.Response.AddHeader("Content-Encoding", "gzip");
  174. var bytes = default(byte[]);
  175. using (var memstream = new MemoryStream())
  176. {
  177. input.CopyTo(memstream);
  178. bytes = memstream.ToArray();
  179. }
  180. context.Response.StatusCode = (int)HttpStatusCode.OK;
  181. context.Response.ContentLength64 = bytes.Length;
  182. Stream st = context.Response.OutputStream;
  183. st.Write(bytes, 0, bytes.Length);
  184. UnityEngine.Debug.Log("Sent");
  185. }
  186. catch (Exception ex)
  187. {
  188. context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
  189. }
  190. }
  191. else
  192. {
  193. context.Response.StatusCode = (int)HttpStatusCode.NotFound;
  194. }
  195. context.Response.OutputStream.Close();
  196. }
  197. private void Initialize(string path, int port)
  198. {
  199. this._rootDirectory = path;
  200. this._port = port;
  201. _serverThread = new Thread(this.Listen);
  202. _serverThread.Start();
  203. }
  204. }