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

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

fixes

File size: 3.4 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
[154]21 handlers.Add ("/index.htm", new SimpleRedirectHandler ("/static/index.html"));
[151]22 handlers.Add ("/static/", new StaticHandler ("/static/", Application.dataPath + "/../webserver", false/*true*/, true)); // TODO: Enable cache
[134]23 handlers.Add ("/map/", new StaticHandler ("/map/", StaticDirectories.GetSaveGameDir () + "/map", false, false));
[154]24 handlers.Add ("/api/", new ApiHandler ("/api/"));
[133]25
26 _listener.Prefixes.Add (String.Format ("http://*:{0}/", port));
[140]27 //_listener.AuthenticationSchemes = AuthenticationSchemes.Basic;
[133]28 _listener.Start ();
29
30 ThreadPool.QueueUserWorkItem ((o) =>
31 {
32 try {
33 while (_listener.IsListening) {
34 ThreadPool.QueueUserWorkItem ((c) =>
35 {
[134]36 HttpListenerContext ctx = c as HttpListenerContext;
37 HandleRequest (ctx);
[133]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
[134]51 private void HandleRequest (HttpListenerContext ctx)
[133]52 {
53 try {
[134]54 ctx.Response.ProtocolVersion = new Version ("1.1");
[133]55
[154]56 HttpListenerBasicIdentity user = Authorize (ctx);
[134]57
[154]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;
[133]66 }
67 }
68 }
69
[154]70 Log.Out ("Error in Web.HandleRequest(): No handler found for path \"" + ctx.Request.Url.AbsolutePath + "\"");
71 ctx.Response.StatusCode = (int)HttpStatusCode.NotFound;
72
[133]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);
[134]78 } catch (Exception e) {
79 Log.Out ("Error in Web.HandleRequest(): " + e);
[133]80 } finally {
[134]81 ctx.Response.Close ();
[133]82 }
83 }
84
[154]85 private HttpListenerBasicIdentity Authorize (HttpListenerContext ctx)
[134]86 {
87 try {
[154]88 return (HttpListenerBasicIdentity)ctx.User.Identity;
[134]89 } catch (NullReferenceException) {
[154]90 return null;
[134]91 }
92 }
93
[133]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 {
[142]107 //Log.Out ("NOT IMPLEMENTED: Web.WriteToClient");
[133]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 {
[142]116 //Log.Out ("NOT IMPLEMENTED: Web.WriteToClient_Single");
[133]117 } catch (Exception e) {
118 Log.Out ("Error in Web.WriteToClient_Single: " + e);
119 }
120 }
121
122 }
123}
Note: See TracBrowser for help on using the repository browser.