Changeset 387 for binary-improvements2/MapRendering/Web/Handlers
- Timestamp:
- Aug 6, 2022, 11:32:32 PM (3 years ago)
- Location:
- binary-improvements2/MapRendering/Web/Handlers
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/MapRendering/Web/Handlers/AbsHandler.cs
r383 r387 1 using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;2 using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;3 4 1 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 5 2 public abstract class AbsHandler { … … 16 13 } 17 14 18 public abstract void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con, 19 int _permissionLevel); 15 public abstract void HandleRequest (RequestContext _context); 20 16 21 17 public virtual bool IsAuthorizedForHandler (WebConnection _user, int _permissionLevel) { -
binary-improvements2/MapRendering/Web/Handlers/ApiHandler.cs
r384 r387 4 4 using System.Reflection; 5 5 using AllocsFixes.NetConnections.Servers.Web.API; 6 using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;7 using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;8 6 9 7 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 10 8 public class ApiHandler : AbsHandler { 11 private readonly Dictionary<string, WebAPI> apis = new CaseInsensitiveStringDictionary<WebAPI> ();9 private readonly Dictionary<string, AbsWebAPI> apis = new CaseInsensitiveStringDictionary<AbsWebAPI> (); 12 10 13 11 public ApiHandler () : base (null) { … … 25 23 26 24 foreach (Type t in Assembly.GetExecutingAssembly ().GetTypes ()) { 27 if (!t.IsAbstract && t.IsSubclassOf (typeof ( WebAPI))) {25 if (!t.IsAbstract && t.IsSubclassOf (typeof (AbsWebAPI))) { 28 26 ConstructorInfo ctor = t.GetConstructor (apiWithParentCtorTypes); 29 27 if (ctor != null) { 30 WebAPI apiInstance = (WebAPI) ctor.Invoke (apiWithParentCtorArgs);28 AbsWebAPI apiInstance = (AbsWebAPI) ctor.Invoke (apiWithParentCtorArgs); 31 29 addApi (apiInstance); 32 30 continue; … … 35 33 ctor = t.GetConstructor (apiEmptyCtorTypes); 36 34 if (ctor != null) { 37 WebAPI apiInstance = (WebAPI) ctor.Invoke (apiEmptyCtorArgs);35 AbsWebAPI apiInstance = (AbsWebAPI) ctor.Invoke (apiEmptyCtorArgs); 38 36 addApi (apiInstance); 39 37 } … … 46 44 } 47 45 48 private void addApi ( WebAPI _api) {46 private void addApi (AbsWebAPI _api) { 49 47 apis.Add (_api.Name, _api); 50 48 WebPermissions.Instance.AddKnownModule ("webapi." + _api.Name, _api.DefaultPermissionLevel ()); … … 55 53 #endif 56 54 57 public override void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con, 58 int _permissionLevel) { 59 string apiName = _requestPath.Remove (0, urlBasePath.Length); 55 public override void HandleRequest (RequestContext _context) { 60 56 61 if (!apis.TryGetValue (apiName, out WebAPI api)) { 57 string apiName; 58 string subPath = null; 59 60 int pathSeparatorIndex = _context.RequestPath.IndexOf ('/', urlBasePath.Length); 61 if (pathSeparatorIndex >= 0) { 62 apiName = _context.RequestPath.Substring (urlBasePath.Length, pathSeparatorIndex - urlBasePath.Length); 63 subPath = _context.RequestPath.Substring (pathSeparatorIndex + 1); 64 } else { 65 apiName = _context.RequestPath.Substring (urlBasePath.Length); 66 } 67 68 if (!apis.TryGetValue (apiName, out AbsWebAPI api)) { 62 69 Log.Out ($"Error in {nameof(ApiHandler)}.HandleRequest(): No handler found for API \"{apiName}\""); 63 _ resp.StatusCode = (int) HttpStatusCode.NotFound;70 _context.Response.StatusCode = (int) HttpStatusCode.NotFound; 64 71 return; 65 72 } 66 73 67 if (!AuthorizeForApi (apiName, _ permissionLevel)) {68 _ resp.StatusCode = (int) HttpStatusCode.Forbidden;69 if (_con != null) {74 if (!AuthorizeForApi (apiName, _context.PermissionLevel)) { 75 _context.Response.StatusCode = (int) HttpStatusCode.Forbidden; 76 if (_context.Connection != null) { 70 77 //Log.Out ($"{nameof(ApiHandler)}: user '{user.SteamID}' not allowed to execute '{apiName}'"); 71 78 } … … 74 81 } 75 82 83 _context.RequestPath = subPath; 84 76 85 try { 77 86 #if ENABLE_PROFILER 78 87 apiHandlerSampler.Begin (); 79 88 #endif 80 api.HandleRequest (_ req, _resp, _con, _permissionLevel);89 api.HandleRequest (_context); 81 90 #if ENABLE_PROFILER 82 91 apiHandlerSampler.End (); … … 85 94 Log.Error ($"Error in {nameof(ApiHandler)}.HandleRequest(): Handler {api.Name} threw an exception:"); 86 95 Log.Exception (e); 87 _ resp.StatusCode = (int) HttpStatusCode.InternalServerError;96 _context.Response.StatusCode = (int) HttpStatusCode.InternalServerError; 88 97 } 89 98 } -
binary-improvements2/MapRendering/Web/Handlers/ItemIconHandler.cs
r382 r387 4 4 using System.Net; 5 5 using UnityEngine; 6 using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;7 using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;8 6 using Object = UnityEngine.Object; 9 7 … … 26 24 public static ItemIconHandler Instance { get; private set; } 27 25 28 public override void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con, 29 int _permissionLevel) { 26 public override void HandleRequest (RequestContext _context) { 30 27 if (!loaded) { 31 _ resp.StatusCode = (int) HttpStatusCode.InternalServerError;28 _context.Response.StatusCode = (int) HttpStatusCode.InternalServerError; 32 29 Log.Out ("Web:IconHandler: Icons not loaded"); 33 30 return; 34 31 } 35 32 36 string requestFileName = _ requestPath.Remove (0, urlBasePath.Length);33 string requestFileName = _context.RequestPath.Remove (0, urlBasePath.Length); 37 34 requestFileName = requestFileName.Remove (requestFileName.LastIndexOf ('.')); 38 35 39 if (icons.ContainsKey (requestFileName) && _ requestPath.EndsWith (".png", StringComparison.OrdinalIgnoreCase)) {40 _ resp.ContentType = MimeType.GetMimeType (".png");36 if (icons.ContainsKey (requestFileName) && _context.RequestPath.EndsWith (".png", StringComparison.OrdinalIgnoreCase)) { 37 _context.Response.ContentType = MimeType.GetMimeType (".png"); 41 38 42 39 byte[] itemIconData = icons [requestFileName]; 43 40 44 _ resp.ContentLength64 = itemIconData.Length;45 _ resp.OutputStream.Write (itemIconData, 0, itemIconData.Length);41 _context.Response.ContentLength64 = itemIconData.Length; 42 _context.Response.OutputStream.Write (itemIconData, 0, itemIconData.Length); 46 43 } else { 47 _ resp.StatusCode = (int) HttpStatusCode.NotFound;44 _context.Response.StatusCode = (int) HttpStatusCode.NotFound; 48 45 if (logMissingFiles) { 49 Log.Out ("Web:IconHandler:FileNotFound: \"" + _ requestPath + "\" ");46 Log.Out ("Web:IconHandler:FileNotFound: \"" + _context.RequestPath + "\" "); 50 47 } 51 48 } -
binary-improvements2/MapRendering/Web/Handlers/RewriteHandler.cs
r382 r387 1 using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;2 using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;3 4 1 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 5 2 public class RewriteHandler : AbsHandler { … … 12 9 } 13 10 14 public override void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con, 15 int _permissionLevel) { 16 string newRequestPath = fixedTarget ? target : target + _requestPath.Remove (0, urlBasePath.Length); 17 parent.ApplyPathHandler (newRequestPath, _req, _resp, _con, _permissionLevel); 11 public override void HandleRequest (RequestContext _context) { 12 _context.RequestPath = fixedTarget ? target : target + _context.RequestPath.Remove (0, urlBasePath.Length); 13 parent.ApplyPathHandler (_context); 18 14 } 19 15 } -
binary-improvements2/MapRendering/Web/Handlers/SessionHandler.cs
r382 r387 3 3 using System.Net; 4 4 using System.Text; 5 using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;6 using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;7 5 8 6 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { … … 29 27 } 30 28 31 public override void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con, 32 int _permissionLevel) { 29 public override void HandleRequest (RequestContext _context) { 33 30 34 IPEndPoint reqRemoteEndPoint = _ req.RemoteEndPoint;31 IPEndPoint reqRemoteEndPoint = _context.Request.RemoteEndPoint; 35 32 if (reqRemoteEndPoint == null) { 36 _ resp.Redirect (pageBasePath);33 _context.Response.Redirect (pageBasePath); 37 34 return; 38 35 } 39 36 40 string subpath = _ requestPath.Remove (0, urlBasePath.Length);37 string subpath = _context.RequestPath.Remove (0, urlBasePath.Length); 41 38 42 39 StringBuilder result = new StringBuilder (); … … 47 44 48 45 try { 49 ulong id = OpenID.Validate (_ req);46 ulong id = OpenID.Validate (_context.Request); 50 47 if (id > 0) { 51 48 WebConnection con = connectionHandler.LogIn (id, reqRemoteEndPoint.Address); … … 60 57 Secure = false 61 58 }; 62 _ resp.AppendCookie (cookie);63 _ resp.Redirect (pageBasePath);59 _context.Response.AppendCookie (cookie); 60 _context.Response.Redirect (pageBasePath); 64 61 65 62 return; … … 73 70 result.Append ($"<h1>Login failed, <a href=\"{pageBasePath}\">click to return to main page</a>.</h1>"); 74 71 } else if (subpath.StartsWith ("logout")) { 75 if (_con != null) {76 connectionHandler.LogOut (_con .SessionID);72 if (_context.Connection != null) { 73 connectionHandler.LogOut (_context.Connection.SessionID); 77 74 Cookie cookie = new Cookie ("sid", "", "/") { 78 75 Expired = true 79 76 }; 80 _ resp.AppendCookie (cookie);81 _ resp.Redirect (pageBasePath);77 _context.Response.AppendCookie (cookie); 78 _context.Response.Redirect (pageBasePath); 82 79 return; 83 80 } … … 85 82 result.Append ($"<h1>Not logged in, <a href=\"{pageBasePath}\">click to return to main page</a>.</h1>"); 86 83 } else if (subpath.StartsWith (steamLoginUrl)) { 87 string host = (Web.IsSslRedirected (_ req) ? "https://" : "http://") + _req.UserHostName;84 string host = (Web.IsSslRedirected (_context.Request) ? "https://" : "http://") + _context.Request.UserHostName; 88 85 string url = OpenID.GetOpenIdLoginUrl (host, host + urlBasePath + steamOpenIdVerifyUrl); 89 _ resp.Redirect (url);86 _context.Response.Redirect (url); 90 87 return; 91 88 } else { … … 95 92 result.Append (footer); 96 93 97 _resp.ContentType = MimeType.GetMimeType (".html"); 98 _resp.ContentEncoding = Encoding.UTF8; 99 byte[] buf = Encoding.UTF8.GetBytes (result.ToString ()); 100 _resp.ContentLength64 = buf.Length; 101 _resp.OutputStream.Write (buf, 0, buf.Length); 94 WebUtils.WriteText (_context.Response, result.ToString (), _mimeType: WebUtils.MimeHtml); 102 95 } 103 96 } -
binary-improvements2/MapRendering/Web/Handlers/SimpleRedirectHandler.cs
r383 r387 1 using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;2 using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;3 4 1 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 5 2 public class SimpleRedirectHandler : AbsHandler { … … 10 7 } 11 8 12 public override void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con, 13 int _permissionLevel) { 14 _resp.Redirect (target); 9 public override void HandleRequest (RequestContext _context) { 10 _context.Response.Redirect (target); 15 11 } 16 12 } -
binary-improvements2/MapRendering/Web/Handlers/StaticHandler.cs
r382 r387 2 2 using System.Net; 3 3 using AllocsFixes.FileCache; 4 using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;5 using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;6 4 7 5 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { … … 18 16 } 19 17 20 public override void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con, 21 int _permissionLevel) { 22 string fn = _requestPath.Remove (0, urlBasePath.Length); 18 public override void HandleRequest (RequestContext _context) { 19 string fn = _context.RequestPath.Remove (0, urlBasePath.Length); 23 20 24 21 byte[] content = cache.GetFileContent (datapath + fn); 25 22 26 23 if (content != null) { 27 _ resp.ContentType = MimeType.GetMimeType (Path.GetExtension (fn));28 _ resp.ContentLength64 = content.Length;29 _ resp.OutputStream.Write (content, 0, content.Length);24 _context.Response.ContentType = MimeType.GetMimeType (Path.GetExtension (fn)); 25 _context.Response.ContentLength64 = content.Length; 26 _context.Response.OutputStream.Write (content, 0, content.Length); 30 27 } else { 31 _ resp.StatusCode = (int) HttpStatusCode.NotFound;28 _context.Response.StatusCode = (int) HttpStatusCode.NotFound; 32 29 if (logMissingFiles) { 33 Log.Out ("Web:Static:FileNotFound: \"" + _ requestPath + "\" @ \"" + datapath + fn + "\"");30 Log.Out ("Web:Static:FileNotFound: \"" + _context.RequestPath + "\" @ \"" + datapath + fn + "\""); 34 31 } 35 32 } -
binary-improvements2/MapRendering/Web/Handlers/UserStatusHandler.cs
r382 r387 1 1 using AllocsFixes.JSON; 2 using AllocsFixes.NetConnections.Servers.Web.API;3 using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;4 using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;5 2 6 3 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { … … 9 6 } 10 7 11 public override void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con, 12 int _permissionLevel) { 8 public override void HandleRequest (RequestContext _context) { 13 9 JSONObject result = new JSONObject (); 14 10 15 result.Add ("loggedin", new JSONBoolean (_con != null));16 result.Add ("username", new JSONString (_con != null ? _con.UserId.ToString () : string.Empty));11 result.Add ("loggedin", new JSONBoolean (_context.Connection != null)); 12 result.Add ("username", new JSONString (_context.Connection != null ? _context.Connection.UserId.ToString () : string.Empty)); 17 13 18 14 JSONArray perms = new JSONArray (); … … 20 16 JSONObject permObj = new JSONObject (); 21 17 permObj.Add ("module", new JSONString (perm.module)); 22 permObj.Add ("allowed", new JSONBoolean (perm.permissionLevel >= _ permissionLevel));18 permObj.Add ("allowed", new JSONBoolean (perm.permissionLevel >= _context.PermissionLevel)); 23 19 perms.Add (permObj); 24 20 } … … 26 22 result.Add ("permissions", perms); 27 23 28 Web API.WriteJSON (_resp, result);24 WebUtils.WriteJson (_context.Response, result); 29 25 } 30 26 }
Note:
See TracChangeset
for help on using the changeset viewer.