Changeset 134
- Timestamp:
- Aug 27, 2014, 5:35:51 PM (10 years ago)
- Location:
- binary-improvements
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Web/PathHandler.cs
r133 r134 6 6 public abstract class PathHandler 7 7 { 8 public abstract void HandleRequest(HttpListenerRequest req, HttpListenerResponse resp );8 public abstract void HandleRequest(HttpListenerRequest req, HttpListenerResponse resp, HttpListenerBasicIdentity user); 9 9 } 10 10 } -
binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Web/SimpleRedirectHandler.cs
r133 r134 13 13 } 14 14 15 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp )15 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, HttpListenerBasicIdentity user) 16 16 { 17 17 resp.Redirect (target); -
binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Web/StaticHandler.cs
r133 r134 3 3 using System.IO; 4 4 using System.Net; 5 using System.Threading; 5 6 6 7 namespace AllocsFixes.NetConnections.Servers.Web … … 11 12 private string staticPart; 12 13 private bool cache; 14 private bool logMissingFiles; 13 15 private Dictionary<string, byte[]> fileCache = new Dictionary<string, byte[]> (); 14 16 15 public StaticHandler (string staticPart, string filePath, bool cache )17 public StaticHandler (string staticPart, string filePath, bool cache, bool logMissingFiles) 16 18 { 17 19 this.staticPart = staticPart; 18 20 this.datapath = filePath; 19 21 this.cache = cache; 22 this.logMissingFiles = logMissingFiles; 20 23 } 21 24 22 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp )25 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, HttpListenerBasicIdentity user) 23 26 { 24 27 try { … … 27 30 byte[] content; 28 31 if (cache) { 29 if (!fileCache.ContainsKey (fn)) { 30 if (!File.Exists (datapath + "/" + fn)) { 31 resp.StatusCode = (int)HttpStatusCode.NotFound; 32 return; 32 Monitor.Enter (fileCache); 33 try { 34 if (!fileCache.ContainsKey (fn)) { 35 if (!File.Exists (datapath + "/" + fn)) { 36 resp.StatusCode = (int)HttpStatusCode.NotFound; 37 if (logMissingFiles) 38 Log.Out ("Web:Static:FileNotFound: " + req.Url.AbsolutePath); 39 return; 40 } 41 42 fileCache.Add (fn, File.ReadAllBytes (datapath + "/" + fn)); 33 43 } 34 44 35 fileCache.Add (fn, File.ReadAllBytes (datapath + "/" + fn)); 45 content = fileCache [fn]; 46 } finally { 47 Monitor.Exit (fileCache); 36 48 } 37 38 content = fileCache [fn];39 49 } else { 40 50 if (!File.Exists (datapath + "/" + fn)) { 41 51 resp.StatusCode = (int)HttpStatusCode.NotFound; 52 if (logMissingFiles) 53 Log.Out ("Web:Static:FileNotFound: " + req.Url.AbsolutePath); 42 54 return; 43 55 } -
binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Web/Web.cs
r133 r134 20 20 21 21 handlers.Add ("/index.", new SimpleRedirectHandler ("/static/index.html")); 22 handlers.Add ("/static/", new StaticHandler ("/static/", Application.dataPath + "/../webserver", true ));23 handlers.Add ("/map/", new StaticHandler ("/map/", StaticDirectories.GetSaveGameDir () + "/map", false ));22 handlers.Add ("/static/", new StaticHandler ("/static/", Application.dataPath + "/../webserver", true, true)); 23 handlers.Add ("/map/", new StaticHandler ("/map/", StaticDirectories.GetSaveGameDir () + "/map", false, false)); 24 24 25 25 _listener.Prefixes.Add (String.Format ("http://*:{0}/", port)); 26 _listener.AuthenticationSchemes = AuthenticationSchemes.Basic; 26 27 _listener.Start (); 27 28 … … 32 33 ThreadPool.QueueUserWorkItem ((c) => 33 34 { 34 varctx = c as HttpListenerContext;35 HandleRequest (ctx .Request, ctx.Response);35 HttpListenerContext ctx = c as HttpListenerContext; 36 HandleRequest (ctx); 36 37 }, _listener.GetContext ()); 37 38 } … … 47 48 } 48 49 49 private void HandleRequest (HttpListener Request req, HttpListenerResponse resp)50 private void HandleRequest (HttpListenerContext ctx) 50 51 { 51 52 try { 52 resp.ProtocolVersion = new Version ("1.1");53 ctx.Response.ProtocolVersion = new Version ("1.1"); 53 54 54 if (req.Url.AbsolutePath.Length < 2) { 55 handlers ["/index."].HandleRequest (req, resp); 56 return; 57 } else { 58 foreach (KeyValuePair<string, PathHandler> kvp in handlers) { 59 if (req.Url.AbsolutePath.StartsWith (kvp.Key)) { 60 kvp.Value.HandleRequest (req, resp); 61 return; 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 } 62 67 } 63 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 } else { 73 ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized; 74 ctx.Response.AddHeader("WWW-Authenticate", "Basic realm=\"\""); 64 75 } 65 66 Log.Out ("Error in Web.HandleRequest(): No handler found for path \"" + req.Url.AbsolutePath + "\"");67 resp.StatusCode = (int)HttpStatusCode.NotFound;68 76 69 77 // byte[] buf = Encoding.UTF8.GetBytes ("Hello World"); … … 72 80 // resp.ContentEncoding = Encoding.UTF8; 73 81 // resp.OutputStream.Write (buf, 0, buf.Length); 74 } catch { 82 } catch (Exception e) { 83 Log.Out ("Error in Web.HandleRequest(): " + e); 75 84 } finally { 76 resp.Close (); 85 ctx.Response.Close (); 86 } 87 } 88 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; 77 97 } 78 98 }
Note:
See TracChangeset
for help on using the changeset viewer.