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

Last change on this file since 138 was 134, checked in by alloc, 10 years ago

Fixes

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