- Timestamp:
- Oct 23, 2014, 3:09:29 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Web/Web.cs
r199 r202 3 3 using System.IO; 4 4 using System.Net; 5 using System.Net.Sockets; 5 6 using System.Text; 6 7 using System.Threading; … … 15 16 private bool authEnabled = false; 16 17 private string realm = "7dtd Admin Panel"; 18 public static int handlingCount = 0; 19 public static int currentHandlers = 0; 17 20 18 21 public Web () … … 62 65 _listener.Realm = realm; 63 66 64 ThreadPool.QueueUserWorkItem ((o) =>65 {66 try {67 while (_listener.IsListening) {68 ThreadPool.QueueUserWorkItem ((c) =>69 {70 HttpListenerContext ctx = c as HttpListenerContext;71 HandleRequest (ctx);72 }, _listener.GetContext ());73 }74 } catch {75 }76 }77 );78 79 67 NetTelnetServer.RegisterServer (this); 80 68 69 _listener.BeginGetContext (new AsyncCallback (HandleRequest), _listener); 81 70 82 71 Log.Out ("Started Webserver on " + (webPort + 2) + " (authentication " + (authEnabled ? "enabled" : "disabled") + ")"); … … 86 75 } 87 76 88 private void HandleRequest ( HttpListenerContext ctx)77 private void HandleRequest (IAsyncResult result) 89 78 { 90 try { 91 ctx.Response.ProtocolVersion = new Version ("1.1"); 79 if (_listener.IsListening) { 80 Interlocked.Increment(ref handlingCount); 81 Interlocked.Increment(ref currentHandlers); 82 HttpListenerContext ctx = _listener.EndGetContext (result); 83 _listener.BeginGetContext (new AsyncCallback (HandleRequest), _listener); 84 try { 85 ctx.Response.ProtocolVersion = new Version ("1.1"); 92 86 93 HttpListenerBasicIdentity user = Authorize (ctx);87 HttpListenerBasicIdentity user = Authorize (ctx); 94 88 95 if (!authEnabled || (user.Name.ToLower ().Equals ("admin") && user.Password.Equals (GamePrefs.GetString (EnumGamePrefs.ControlPanelPassword)))) { 96 if (ctx.Request.Url.AbsolutePath.Length < 2) { 97 handlers ["/index.htm"].HandleRequest (ctx.Request, ctx.Response, user); 98 return; 99 } else { 100 foreach (KeyValuePair<string, PathHandler> kvp in handlers) { 101 if (ctx.Request.Url.AbsolutePath.StartsWith (kvp.Key)) { 102 kvp.Value.HandleRequest (ctx.Request, ctx.Response, user); 103 return; 89 if (!authEnabled || (user.Name.ToLower ().Equals ("admin") && user.Password.Equals (GamePrefs.GetString (EnumGamePrefs.ControlPanelPassword)))) { 90 if (ctx.Request.Url.AbsolutePath.Length < 2) { 91 handlers ["/index.htm"].HandleRequest (ctx.Request, ctx.Response, user); 92 return; 93 } else { 94 foreach (KeyValuePair<string, PathHandler> kvp in handlers) { 95 if (ctx.Request.Url.AbsolutePath.StartsWith (kvp.Key)) { 96 kvp.Value.HandleRequest (ctx.Request, ctx.Response, user); 97 return; 98 } 104 99 } 105 100 } 101 102 Log.Out ("Error in Web.HandleRequest(): No handler found for path \"" + ctx.Request.Url.AbsolutePath + "\""); 103 ctx.Response.StatusCode = (int)HttpStatusCode.NotFound; 104 } else { 105 ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized; 106 ctx.Response.Headers ["WWW-Authenticate"] = "Basic realm=\"" + realm + "\""; 106 107 } 107 108 Log.Out ("Error in Web.HandleRequest(): No handler found for path \"" + ctx.Request.Url.AbsolutePath + "\""); 109 ctx.Response.StatusCode = (int)HttpStatusCode.NotFound; 110 } else { 111 ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized; 112 ctx.Response.Headers ["WWW-Authenticate"] = "Basic realm=\"" + realm + "\""; 108 } catch (IOException e) { 109 if (e.InnerException is SocketException) { 110 if (e.InnerException.Message.Contains ("forcibly closed") || e.InnerException.Message.Contains ("socket has been shut down")) 111 Log.Out ("Error in Web.HandleRequest(): Remote host closed connection"); 112 else 113 Log.Out ("Error (IO->Socket) in Web.HandleRequest(): " + e); 114 } else { 115 Log.Out ("Error (IO) in Web.HandleRequest(): " + e); 116 } 117 } catch (Exception e) { 118 Log.Out ("Error in Web.HandleRequest(): " + e); 119 } finally { 120 if (ctx != null) 121 ctx.Response.OutputStream.Close (); 122 Interlocked.Decrement(ref currentHandlers); 113 123 } 114 115 // byte[] buf = Encoding.UTF8.GetBytes ("Hello World");116 // resp.ContentLength64 = buf.Length;117 // resp.ContentType = "text/html";118 // resp.ContentEncoding = Encoding.UTF8;119 // resp.OutputStream.Write (buf, 0, buf.Length);120 } catch (Exception e) {121 Log.Out ("Error in Web.HandleRequest(): " + e);122 } finally {123 ctx.Response.Close ();124 124 } 125 125 }
Note:
See TracChangeset
for help on using the changeset viewer.