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

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

fixes

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