- Timestamp:
- Oct 23, 2014, 3:09:29 PM (10 years ago)
- Location:
- binary-improvements/7dtd-server-fixes/src/NetConnections/Servers
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Telnet/TelnetConnection.cs
r190 r202 59 59 { 60 60 toClientQueue.Enqueue ("*** Connected with 7DTD server."); 61 toClientQueue.Enqueue ("*** Server version: " + GamePrefs.GetString(EnumGamePrefs.GameVersion)); 61 62 toClientQueue.Enqueue ("*** Dedicated server only build"); 62 63 toClientQueue.Enqueue ("*** Allocs server fixes loaded"); -
binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Web/StaticHandler.cs
r199 r202 24 24 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, HttpListenerBasicIdentity user) 25 25 { 26 try { 27 string fn = req.Url.AbsolutePath.Remove (0, staticPart.Length); 26 string fn = req.Url.AbsolutePath.Remove (0, staticPart.Length); 28 27 29 byte[] content = cache.GetFileContent (datapath + "/" + fn); 30 if (content != null) { 31 resp.ContentType = MimeType.GetMimeType (Path.GetExtension (fn)); 32 resp.ContentLength64 = content.Length; 33 resp.OutputStream.Write (content, 0, content.Length); 34 } else { 35 resp.StatusCode = (int)HttpStatusCode.NotFound; 36 if (logMissingFiles) 37 Log.Out ("Web:Static:FileNotFound: \"" + req.Url.AbsolutePath + "\" @ \"" + datapath + "/" + req.Url.AbsolutePath.Remove (0, staticPart.Length) + "\""); 38 return; 39 } 40 } catch (Exception e) { 41 Log.Out ("Error in StaticHandler.HandleRequest: " + e); 28 byte[] content = cache.GetFileContent (datapath + "/" + fn); 29 if (content != null) { 30 resp.ContentType = MimeType.GetMimeType (Path.GetExtension (fn)); 31 resp.ContentLength64 = content.Length; 32 resp.OutputStream.Write (content, 0, content.Length); 33 } else { 34 resp.StatusCode = (int)HttpStatusCode.NotFound; 35 if (logMissingFiles) 36 Log.Out ("Web:Static:FileNotFound: \"" + req.Url.AbsolutePath + "\" @ \"" + datapath + "/" + req.Url.AbsolutePath.Remove (0, staticPart.Length) + "\""); 37 return; 42 38 } 43 39 } -
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.