Changeset 382 for binary-improvements2/MapRendering/Web/SSE/SseHandler.cs
- Timestamp:
- Aug 1, 2022, 12:54:31 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/MapRendering/Web/SSE/SseHandler.cs
r367 r382 5 5 using System.Threading; 6 6 using AllocsFixes.NetConnections.Servers.Web.Handlers; 7 using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest; 8 using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse; 7 9 8 10 // Implemented following HTML spec … … 10 12 11 13 namespace AllocsFixes.NetConnections.Servers.Web.SSE { 12 public class SseHandler : PathHandler {14 public class SseHandler : AbsHandler { 13 15 private readonly Dictionary<string, EventBase> events = new CaseInsensitiveStringDictionary<EventBase> (); 14 16 15 17 private ThreadManager.ThreadInfo queueThead; 16 18 private readonly AutoResetEvent evSendRequest = new AutoResetEvent (false); … … 18 20 19 21 public SseHandler (string _moduleName = null) : base (_moduleName) { 20 Type[] ctorTypes = { typeof (SseHandler)};21 object[] ctorParams = { this};22 Type[] ctorTypes = { typeof (SseHandler) }; 23 object[] ctorParams = { this }; 22 24 23 25 foreach (Type t in Assembly.GetExecutingAssembly ().GetTypes ()) { … … 25 27 ConstructorInfo ctor = t.GetConstructor (ctorTypes); 26 28 if (ctor != null) { 27 EventBase apiInstance = (EventBase) 28 addEvent (apiInstance.Name, apiInstance);29 EventBase apiInstance = (EventBase)ctor.Invoke (ctorParams); 30 AddEvent (apiInstance.Name, apiInstance); 29 31 } 30 32 } … … 34 36 public override void SetBasePathAndParent (Web _parent, string _relativePath) { 35 37 base.SetBasePathAndParent (_parent, _relativePath); 36 38 37 39 queueThead = ThreadManager.StartThread ("SSE-Processing_" + urlBasePath, QueueProcessThread, ThreadPriority.BelowNormal, 38 40 _useRealThread: true); … … 45 47 } 46 48 47 p rivate void addEvent (string _eventName, EventBase _eventInstance) {49 public void AddEvent (string _eventName, EventBase _eventInstance) { 48 50 events.Add (_eventName, _eventInstance); 49 51 WebPermissions.Instance.AddKnownModule ("webevent." + _eventName, _eventInstance.DefaultPermissionLevel ()); 50 52 } 51 53 52 public override void HandleRequest ( HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,54 public override void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con, 53 55 int _permissionLevel) { 54 string eventName = _req .Url.AbsolutePath.Remove (0, urlBasePath.Length);56 string eventName = _requestPath.Remove (0, urlBasePath.Length); 55 57 56 58 if (!events.TryGetValue (eventName, out EventBase eventInstance)) { 57 59 Log.Out ($"Error in {nameof (SseHandler)}.HandleRequest(): No handler found for event \"{eventName}\""); 58 _resp.StatusCode = (int) 60 _resp.StatusCode = (int)HttpStatusCode.NotFound; 59 61 return; 60 62 } 61 63 62 if (! AuthorizeForEvent (eventName, _permissionLevel)) {63 _resp.StatusCode = (int) 64 if (_ user!= null) {64 if (!IsAuthorizedForEvent (eventName, _permissionLevel)) { 65 _resp.StatusCode = (int)HttpStatusCode.Forbidden; 66 if (_con != null) { 65 67 //Log.Out ($"{nameof(SseHandler)}: user '{user.SteamID}' not allowed to access '{eventName}'"); 66 68 } … … 80 82 Log.Error ($"Error in {nameof (SseHandler)}.HandleRequest(): Handler {eventInstance.Name} threw an exception:"); 81 83 Log.Exception (e); 82 _resp.StatusCode = (int) 84 _resp.StatusCode = (int)HttpStatusCode.InternalServerError; 83 85 } 84 86 } 85 87 86 private bool AuthorizeForEvent (string _eventName, int _permissionLevel) {88 private bool IsAuthorizedForEvent (string _eventName, int _permissionLevel) { 87 89 return WebPermissions.Instance.ModuleAllowedWithLevel ("webevent." + _eventName, _permissionLevel); 88 90 } 89 91 90 92 private void QueueProcessThread (ThreadManager.ThreadInfo _threadInfo) { 91 try { 92 while (!shutdown && !_threadInfo.TerminationRequested ()) { 93 evSendRequest.WaitOne (500); 93 while (!shutdown && !_threadInfo.TerminationRequested ()) { 94 evSendRequest.WaitOne (500); 94 95 95 foreach (KeyValuePair<string, EventBase> kvp in events) { 96 foreach (KeyValuePair<string, EventBase> kvp in events) { 97 try { 96 98 kvp.Value.ProcessSendQueue (); 99 } catch (Exception e) { 100 Log.Error ($"SSE ({kvp.Key}): Error processing send queue"); 101 Log.Exception (e); 97 102 } 98 103 } 99 } catch (Exception e) {100 Log.Error ("SSE: Error processing send queue");101 Log.Exception (e);102 104 } 103 105 }
Note:
See TracChangeset
for help on using the changeset viewer.