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

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

fixes

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