[133] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
[168] | 3 | using System.IO;
|
---|
[133] | 4 | using System.Net;
|
---|
| 5 | using System.Text;
|
---|
| 6 | using System.Threading;
|
---|
| 7 | using UnityEngine;
|
---|
| 8 |
|
---|
| 9 | namespace AllocsFixes.NetConnections.Servers.Web
|
---|
| 10 | {
|
---|
| 11 | public class Web : IServer
|
---|
| 12 | {
|
---|
| 13 | private readonly HttpListener _listener = new HttpListener ();
|
---|
| 14 | private Dictionary<string, PathHandler> handlers = new Dictionary<string, PathHandler> ();
|
---|
| 15 |
|
---|
| 16 | public Web (int port)
|
---|
| 17 | {
|
---|
| 18 | try {
|
---|
| 19 | if (!HttpListener.IsSupported)
|
---|
| 20 | throw new NotSupportedException ("Needs Windows XP SP2, Server 2003 or later.");
|
---|
| 21 |
|
---|
[154] | 22 | handlers.Add ("/index.htm", new SimpleRedirectHandler ("/static/index.html"));
|
---|
[151] | 23 | handlers.Add ("/static/", new StaticHandler ("/static/", Application.dataPath + "/../webserver", false/*true*/, true)); // TODO: Enable cache
|
---|
[134] | 24 | handlers.Add ("/map/", new StaticHandler ("/map/", StaticDirectories.GetSaveGameDir () + "/map", false, false));
|
---|
[154] | 25 | handlers.Add ("/api/", new ApiHandler ("/api/"));
|
---|
[133] | 26 |
|
---|
| 27 | _listener.Prefixes.Add (String.Format ("http://*:{0}/", port));
|
---|
[168] | 28 | if (File.Exists (Application.dataPath + "/../webserver/protect"))
|
---|
| 29 | _listener.AuthenticationSchemes = AuthenticationSchemes.Basic;
|
---|
[133] | 30 | _listener.Start ();
|
---|
| 31 |
|
---|
| 32 | ThreadPool.QueueUserWorkItem ((o) =>
|
---|
| 33 | {
|
---|
| 34 | try {
|
---|
| 35 | while (_listener.IsListening) {
|
---|
| 36 | ThreadPool.QueueUserWorkItem ((c) =>
|
---|
| 37 | {
|
---|
[134] | 38 | HttpListenerContext ctx = c as HttpListenerContext;
|
---|
| 39 | HandleRequest (ctx);
|
---|
[133] | 40 | }, _listener.GetContext ());
|
---|
| 41 | }
|
---|
| 42 | } catch {
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | );
|
---|
| 46 |
|
---|
[168] | 47 | Log.Out ("Started Webserver on " + port + " (authentication " + (_listener.AuthenticationSchemes == AuthenticationSchemes.Basic ? "enabled" : "disabled") + ")");
|
---|
[133] | 48 | } catch (Exception e) {
|
---|
| 49 | Log.Out ("Error in Web.ctor: " + e);
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[134] | 53 | private void HandleRequest (HttpListenerContext ctx)
|
---|
[133] | 54 | {
|
---|
| 55 | try {
|
---|
[134] | 56 | ctx.Response.ProtocolVersion = new Version ("1.1");
|
---|
[133] | 57 |
|
---|
[154] | 58 | HttpListenerBasicIdentity user = Authorize (ctx);
|
---|
[134] | 59 |
|
---|
[154] | 60 | if (ctx.Request.Url.AbsolutePath.Length < 2) {
|
---|
| 61 | handlers ["/index.htm"].HandleRequest (ctx.Request, ctx.Response, user);
|
---|
| 62 | return;
|
---|
| 63 | } else {
|
---|
| 64 | foreach (KeyValuePair<string, PathHandler> kvp in handlers) {
|
---|
| 65 | if (ctx.Request.Url.AbsolutePath.StartsWith (kvp.Key)) {
|
---|
| 66 | kvp.Value.HandleRequest (ctx.Request, ctx.Response, user);
|
---|
| 67 | return;
|
---|
[133] | 68 | }
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[154] | 72 | Log.Out ("Error in Web.HandleRequest(): No handler found for path \"" + ctx.Request.Url.AbsolutePath + "\"");
|
---|
| 73 | ctx.Response.StatusCode = (int)HttpStatusCode.NotFound;
|
---|
| 74 |
|
---|
[133] | 75 | // byte[] buf = Encoding.UTF8.GetBytes ("Hello World");
|
---|
| 76 | // resp.ContentLength64 = buf.Length;
|
---|
| 77 | // resp.ContentType = "text/html";
|
---|
| 78 | // resp.ContentEncoding = Encoding.UTF8;
|
---|
| 79 | // resp.OutputStream.Write (buf, 0, buf.Length);
|
---|
[134] | 80 | } catch (Exception e) {
|
---|
| 81 | Log.Out ("Error in Web.HandleRequest(): " + e);
|
---|
[133] | 82 | } finally {
|
---|
[134] | 83 | ctx.Response.Close ();
|
---|
[133] | 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[154] | 87 | private HttpListenerBasicIdentity Authorize (HttpListenerContext ctx)
|
---|
[134] | 88 | {
|
---|
| 89 | try {
|
---|
[154] | 90 | return (HttpListenerBasicIdentity)ctx.User.Identity;
|
---|
[134] | 91 | } catch (NullReferenceException) {
|
---|
[154] | 92 | return null;
|
---|
[134] | 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[133] | 96 | public void Disconnect ()
|
---|
| 97 | {
|
---|
| 98 | try {
|
---|
| 99 | _listener.Stop ();
|
---|
| 100 | _listener.Close ();
|
---|
| 101 | } catch (Exception e) {
|
---|
| 102 | Log.Out ("Error in Web.Disconnect: " + e);
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | public void WriteToClient (string line)
|
---|
| 107 | {
|
---|
| 108 | try {
|
---|
[142] | 109 | //Log.Out ("NOT IMPLEMENTED: Web.WriteToClient");
|
---|
[133] | 110 | } catch (Exception e) {
|
---|
| 111 | Log.Out ("Error in Web.WriteToClient: " + e);
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | public void WriteToClient_Single (string line, IConnection client)
|
---|
| 116 | {
|
---|
| 117 | try {
|
---|
[142] | 118 | //Log.Out ("NOT IMPLEMENTED: Web.WriteToClient_Single");
|
---|
[133] | 119 | } catch (Exception e) {
|
---|
| 120 | Log.Out ("Error in Web.WriteToClient_Single: " + e);
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[163] | 124 | public static void SetResponseTextContent (HttpListenerResponse resp, string text)
|
---|
| 125 | {
|
---|
| 126 | byte[] buf = Encoding.UTF8.GetBytes (text);
|
---|
| 127 | resp.ContentLength64 = buf.Length;
|
---|
| 128 | resp.ContentType = "text/html";
|
---|
| 129 | resp.ContentEncoding = Encoding.UTF8;
|
---|
| 130 | resp.OutputStream.Write (buf, 0, buf.Length);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[133] | 133 | }
|
---|
| 134 | }
|
---|