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

Last change on this file since 148 was 142, checked in by alloc, 10 years ago

Fixes: Fixed #49

File size: 3.7 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));
[140]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 {
[140]91 user = null;
92 return true;
[134]93 try {
94 user = (HttpListenerBasicIdentity)ctx.User.Identity;
95 return user.Name.Equals ("admin") && user.Password.Equals (GamePrefs.GetString (EnumGamePrefs.ControlPanelPassword));
96 } catch (NullReferenceException) {
97 user = null;
98 return false;
99 }
100 }
101
[133]102 public void Disconnect ()
103 {
104 try {
105 _listener.Stop ();
106 _listener.Close ();
107 } catch (Exception e) {
108 Log.Out ("Error in Web.Disconnect: " + e);
109 }
110 }
111
112 public void WriteToClient (string line)
113 {
114 try {
[142]115 //Log.Out ("NOT IMPLEMENTED: Web.WriteToClient");
[133]116 } catch (Exception e) {
117 Log.Out ("Error in Web.WriteToClient: " + e);
118 }
119 }
120
121 public void WriteToClient_Single (string line, IConnection client)
122 {
123 try {
[142]124 //Log.Out ("NOT IMPLEMENTED: Web.WriteToClient_Single");
[133]125 } catch (Exception e) {
126 Log.Out ("Error in Web.WriteToClient_Single: " + e);
127 }
128 }
129
130 }
131}
Note: See TracBrowser for help on using the repository browser.