Changeset 487 for TFP-WebServer/WebServer/src/UrlHandlers
- Timestamp:
- Jun 17, 2024, 5:25:43 PM (7 months ago)
- Location:
- TFP-WebServer/WebServer/src/UrlHandlers
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
TFP-WebServer/WebServer/src/UrlHandlers/ApiHandler.cs
r460 r487 30 30 31 31 private void apiFoundCallback (Type _type) { 32 ConstructorInfo ctor = _type.GetConstructor (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, (Binder)null,33 apiWithParentCtorTypes, (ParameterModifier[])null);32 ConstructorInfo ctor = _type.GetConstructor (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, 33 apiWithParentCtorTypes, null); 34 34 if (ctor != null) { 35 35 AbsWebAPI apiInstance = (AbsWebAPI)ctor.Invoke (apiWithParentCtorArgs); … … 38 38 } 39 39 40 ctor = _type.GetConstructor (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, (Binder)null,41 apiEmptyCtorTypes, (ParameterModifier[])null);40 ctor = _type.GetConstructor (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, 41 apiEmptyCtorTypes, null); 42 42 if (ctor != null) { 43 43 AbsWebAPI apiInstance = (AbsWebAPI)ctor.Invoke (apiEmptyCtorArgs); -
TFP-WebServer/WebServer/src/UrlHandlers/SessionHandler.cs
r457 r487 20 20 private const string userPassLoginUrl = "login"; 21 21 public const string userPassLoginName = "User/pass"; 22 private const string userPassErrorPage = "UserPassLoginFailed";23 22 24 23 public SessionHandler () : base (null) { -
TFP-WebServer/WebServer/src/UrlHandlers/SseHandler.cs
r426 r487 20 20 private static readonly Type[] ctorTypes = { typeof (SseHandler) }; 21 21 private static readonly object[] ctorParams = new object[1]; 22 23 private readonly List<SseClient> clients = new List<SseClient>(); 22 24 23 25 public SseHandler (string _moduleName = null) : base (_moduleName) { … … 57 59 58 60 public override void HandleRequest (RequestContext _context) { 59 string eventName = _context.RequestPath.Remove (0, urlBasePath.Length); 60 61 if (!events.TryGetValue (eventName, out AbsEvent eventInstance)) { 62 Log.Warning ($"[Web] [SSE] In {nameof (SseHandler)}.HandleRequest(): No handler found for event \"{eventName}\""); 63 _context.Response.StatusCode = (int)HttpStatusCode.NotFound; 61 string eventNames = _context.QueryParameters ["events"]; 62 if (string.IsNullOrEmpty (eventNames)) { 63 Log.Warning ($"[Web] [SSE] In {nameof (SseHandler)}.HandleRequest(): No 'events' query parameter given"); 64 _context.Response.StatusCode = (int)HttpStatusCode.BadRequest; 64 65 return; 65 66 } 66 67 67 if (!IsAuthorizedForEvent (eventName, _context.PermissionLevel)) { 68 SseClient client; 69 try { 70 client = new SseClient(this, _context.Response); 71 } catch (Exception e) { 72 Log.Error ($"[Web] [SSE] In {nameof (SseHandler)}.HandleRequest(): Could not create client:"); 73 Log.Exception (e); 74 _context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; 75 return; 76 } 77 78 int eventsFound = 0; 79 int eventsAuthorized = 0; 80 int eventsRegistered = 0; 81 foreach (string eventName in eventNames.Split (',', StringSplitOptions.RemoveEmptyEntries)) { 82 if (!events.TryGetValue (eventName, out AbsEvent eventInstance)) { 83 Log.Warning ($"[Web] [SSE] In {nameof (SseHandler)}.HandleRequest(): No handler found for event \"{eventName}\""); 84 continue; 85 } 86 87 eventsFound++; 88 89 if (!IsAuthorizedForEvent (eventName, _context.PermissionLevel)) { 90 continue; 91 } 92 93 eventsAuthorized++; 94 95 try 96 { 97 eventInstance.AddListener (client); 98 } catch (Exception e) { 99 Log.Error ($"[Web] [SSE] In {nameof (SseHandler)}.HandleRequest(): Handler {eventInstance.Name} threw an exception:"); 100 Log.Exception (e); 101 _context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; 102 } 103 104 eventsRegistered++; 105 } 106 107 if (eventsFound == 0) { 108 _context.Response.StatusCode = (int)HttpStatusCode.BadRequest; 109 _context.Response.Close (); 110 return; 111 } 112 113 if (eventsAuthorized == 0) { 68 114 _context.Response.StatusCode = (int)HttpStatusCode.Forbidden; 69 115 if (_context.Connection != null) { … … 71 117 } 72 118 119 _context.Response.Close (); 73 120 return; 74 121 } 75 122 76 try { 77 eventInstance.AddListener (_context.Response); 78 79 // Keep the request open 80 _context.Response.SendChunked = true; 81 82 _context.Response.AddHeader ("Content-Type", "text/event-stream"); 83 _context.Response.OutputStream.Flush (); 84 } catch (Exception e) { 85 Log.Error ($"[Web] [SSE] In {nameof (SseHandler)}.HandleRequest(): Handler {eventInstance.Name} threw an exception:"); 86 Log.Exception (e); 87 _context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; 88 } 123 clients.Add (client); 89 124 } 90 125 … … 105 140 } 106 141 } 142 143 for (int index = clients.Count - 1; index >= 0; index--) { 144 clients[index].HandleKeepAlive (); 145 } 107 146 } 108 147 } … … 111 150 evSendRequest.Set (); 112 151 } 152 153 public void ClientClosed (SseClient _client) { 154 foreach ((string eventName, AbsEvent eventHandler) in events) { 155 try { 156 eventHandler.ClientClosed (_client); 157 } catch (Exception e) { 158 Log.Error($"[Web] [SSE] '{eventName}': Error closing client"); 159 Log.Exception(e); 160 } 161 } 162 163 clients.Remove (_client); 164 } 113 165 } 114 166 }
Note:
See TracChangeset
for help on using the changeset viewer.