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

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

Fixes

File size: 3.6 KB
RevLine 
[133]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"));
[134]22 handlers.Add ("/static/", new StaticHandler ("/static/", Application.dataPath + "/../webserver", true, true));
23 handlers.Add ("/map/", new StaticHandler ("/map/", StaticDirectories.GetSaveGameDir () + "/map", false, false));
[133]24
25 _listener.Prefixes.Add (String.Format ("http://*:{0}/", port));
[134]26 _listener.AuthenticationSchemes = AuthenticationSchemes.Basic;
[133]27 _listener.Start ();
28
29 ThreadPool.QueueUserWorkItem ((o) =>
30 {
31 try {
32 while (_listener.IsListening) {
33 ThreadPool.QueueUserWorkItem ((c) =>
34 {
[134]35 HttpListenerContext ctx = c as HttpListenerContext;
36 HandleRequest (ctx);
[133]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
[134]50 private void HandleRequest (HttpListenerContext ctx)
[133]51 {
52 try {
[134]53 ctx.Response.ProtocolVersion = new Version ("1.1");
[133]54
[134]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 }
[133]67 }
68 }
[134]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=\"\"");
[133]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);
[134]82 } catch (Exception e) {
83 Log.Out ("Error in Web.HandleRequest(): " + e);
[133]84 } finally {
[134]85 ctx.Response.Close ();
[133]86 }
87 }
88
[134]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
[133]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.