source: binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Web/Web.cs@ 174

Last change on this file since 174 was 172, checked in by alloc, 10 years ago

fixes

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