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