- Timestamp:
- Aug 27, 2014, 5:35:51 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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.