Ignore:
Timestamp:
Aug 6, 2022, 11:32:32 PM (3 years ago)
Author:
alloc
Message:

Big refactoring in Web to pass around a Context instead of a bunch of individual arguments all the time

Location:
binary-improvements2/MapRendering/Web/SSE
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • binary-improvements2/MapRendering/Web/SSE/EventLog.cs

    r382 r387  
    1010
    1111                private void LogCallback (string _formattedMsg, string _plainMsg, string _trace, LogType _type, DateTime _timestamp, long _uptime) {
    12                         string date = $"{_timestamp.Year:0000}-{_timestamp.Month:00}-{_timestamp.Day:00}";
    13                         string time = $"{_timestamp.Hour:00}:{_timestamp.Minute:00}:{_timestamp.Second:00}";
    1412                        string isotime = _timestamp.ToString ("o");
    1513                        string uptime = _uptime.ToString ();
     
    2018                        data.Add ("type", new JSONString (_type.ToStringCached ()));
    2119                        data.Add ("trace", new JSONString (_trace));
    22                         data.Add ("date", new JSONString (date));
    23                         data.Add ("time", new JSONString (time));
    2420                        data.Add ("isotime", new JSONString (isotime));
    2521                        data.Add ("uptime", new JSONString (uptime));
  • binary-improvements2/MapRendering/Web/SSE/SseHandler.cs

    r382 r387  
    55using System.Threading;
    66using AllocsFixes.NetConnections.Servers.Web.Handlers;
    7 using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;
    8 using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;
    97
    108// Implemented following HTML spec
     
    5250                }
    5351
    54                 public override void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con,
    55                         int _permissionLevel) {
    56                         string eventName = _requestPath.Remove (0, urlBasePath.Length);
     52                public override void HandleRequest (RequestContext _context) {
     53                        string eventName = _context.RequestPath.Remove (0, urlBasePath.Length);
    5754
    5855                        if (!events.TryGetValue (eventName, out EventBase eventInstance)) {
    5956                                Log.Out ($"Error in {nameof (SseHandler)}.HandleRequest(): No handler found for event \"{eventName}\"");
    60                                 _resp.StatusCode = (int)HttpStatusCode.NotFound;
     57                                _context.Response.StatusCode = (int)HttpStatusCode.NotFound;
    6158                                return;
    6259                        }
    6360
    64                         if (!IsAuthorizedForEvent (eventName, _permissionLevel)) {
    65                                 _resp.StatusCode = (int)HttpStatusCode.Forbidden;
    66                                 if (_con != null) {
     61                        if (!IsAuthorizedForEvent (eventName, _context.PermissionLevel)) {
     62                                _context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
     63                                if (_context.Connection != null) {
    6764                                        //Log.Out ($"{nameof(SseHandler)}: user '{user.SteamID}' not allowed to access '{eventName}'");
    6865                                }
     
    7269
    7370                        try {
    74                                 eventInstance.AddListener (_resp);
     71                                eventInstance.AddListener (_context.Response);
    7572
    7673                                // Keep the request open
    77                                 _resp.SendChunked = true;
     74                                _context.Response.SendChunked = true;
    7875
    79                                 _resp.AddHeader ("Content-Type", "text/event-stream");
    80                                 _resp.OutputStream.Flush ();
     76                                _context.Response.AddHeader ("Content-Type", "text/event-stream");
     77                                _context.Response.OutputStream.Flush ();
    8178                        } catch (Exception e) {
    8279                                Log.Error ($"Error in {nameof (SseHandler)}.HandleRequest(): Handler {eventInstance.Name} threw an exception:");
    8380                                Log.Exception (e);
    84                                 _resp.StatusCode = (int)HttpStatusCode.InternalServerError;
     81                                _context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
    8582                        }
    8683                }
Note: See TracChangeset for help on using the changeset viewer.