Changeset 351 for binary-improvements/MapRendering/Web/Handlers
- Timestamp:
- Jan 19, 2019, 6:12:21 PM (6 years ago)
- Location:
- binary-improvements/MapRendering/Web/Handlers
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/MapRendering/Web/Handlers/ApiHandler.cs
r332 r351 11 11 private readonly string staticPart; 12 12 13 public ApiHandler (string staticPart, string moduleName = null) : base (moduleName) {14 this.staticPart =staticPart;13 public ApiHandler (string _staticPart, string _moduleName = null) : base (_moduleName) { 14 staticPart = _staticPart; 15 15 16 16 foreach (Type t in Assembly.GetExecutingAssembly ().GetTypes ()) { … … 45 45 #endif 46 46 47 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, 48 int permissionLevel) { 49 string apiName = req.Url.AbsolutePath.Remove (0, staticPart.Length); 50 if (!AuthorizeForCommand (apiName, user, permissionLevel)) { 51 resp.StatusCode = (int) HttpStatusCode.Forbidden; 52 if (user != null) { 47 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 48 int _permissionLevel) { 49 string apiName = _req.Url.AbsolutePath.Remove (0, staticPart.Length); 50 51 WebAPI api; 52 if (!apis.TryGetValue (apiName, out api)) { 53 Log.Out ("Error in ApiHandler.HandleRequest(): No handler found for API \"" + apiName + "\""); 54 _resp.StatusCode = (int) HttpStatusCode.NotFound; 55 return; 56 } 57 58 if (!AuthorizeForCommand (apiName, _user, _permissionLevel)) { 59 _resp.StatusCode = (int) HttpStatusCode.Forbidden; 60 if (_user != null) { 53 61 //Log.Out ("ApiHandler: user '{0}' not allowed to execute '{1}'", user.SteamID, apiName); 54 62 } … … 57 65 } 58 66 59 WebAPI api; 60 if (apis.TryGetValue (apiName, out api)) { 61 try { 67 try { 62 68 #if ENABLE_PROFILER 63 69 apiHandlerSampler.Begin (); 64 70 #endif 65 api.HandleRequest (req, resp, user,permissionLevel);71 api.HandleRequest (_req, _resp, _user, _permissionLevel); 66 72 #if ENABLE_PROFILER 67 73 apiHandlerSampler.End (); 68 74 #endif 69 return; 70 } catch (Exception e) { 71 Log.Error ("Error in ApiHandler.HandleRequest(): Handler {0} threw an exception:", api.Name); 72 Log.Exception (e); 73 resp.StatusCode = (int) HttpStatusCode.InternalServerError; 74 return; 75 } 75 } catch (Exception e) { 76 Log.Error ("Error in ApiHandler.HandleRequest(): Handler {0} threw an exception:", api.Name); 77 Log.Exception (e); 78 _resp.StatusCode = (int) HttpStatusCode.InternalServerError; 76 79 } 77 78 Log.Out ("Error in ApiHandler.HandleRequest(): No handler found for API \"" + apiName + "\"");79 resp.StatusCode = (int) HttpStatusCode.NotFound;80 80 } 81 81 82 private bool AuthorizeForCommand (string apiName, WebConnection user, intpermissionLevel) {83 return WebPermissions.Instance.ModuleAllowedWithLevel ("webapi." + apiName,permissionLevel);82 private bool AuthorizeForCommand (string _apiName, WebConnection _user, int _permissionLevel) { 83 return WebPermissions.Instance.ModuleAllowedWithLevel ("webapi." + _apiName, _permissionLevel); 84 84 } 85 85 } -
binary-improvements/MapRendering/Web/Handlers/ItemIconHandler.cs
r326 r351 18 18 } 19 19 20 public ItemIconHandler (string staticPart, bool logMissingFiles, string moduleName = null) : base (moduleName) {21 this.staticPart =staticPart;22 this.logMissingFiles =logMissingFiles;20 public ItemIconHandler (string _staticPart, bool _logMissingFiles, string _moduleName = null) : base (_moduleName) { 21 staticPart = _staticPart; 22 logMissingFiles = _logMissingFiles; 23 23 Instance = this; 24 24 } … … 26 26 public static ItemIconHandler Instance { get; private set; } 27 27 28 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnectionuser,29 int permissionLevel) {28 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 29 int _permissionLevel) { 30 30 if (!loaded) { 31 resp.StatusCode = (int) HttpStatusCode.InternalServerError;31 _resp.StatusCode = (int) HttpStatusCode.InternalServerError; 32 32 Log.Out ("Web:IconHandler: Icons not loaded"); 33 33 return; 34 34 } 35 35 36 string requestFileName = req.Url.AbsolutePath.Remove (0, staticPart.Length);36 string requestFileName = _req.Url.AbsolutePath.Remove (0, staticPart.Length); 37 37 requestFileName = requestFileName.Remove (requestFileName.LastIndexOf ('.')); 38 38 39 if (icons.ContainsKey (requestFileName) && req.Url.AbsolutePath.EndsWith (".png", StringComparison.OrdinalIgnoreCase)) {40 resp.ContentType = MimeType.GetMimeType (".png");39 if (icons.ContainsKey (requestFileName) && _req.Url.AbsolutePath.EndsWith (".png", StringComparison.OrdinalIgnoreCase)) { 40 _resp.ContentType = MimeType.GetMimeType (".png"); 41 41 42 42 byte[] itemIconData = icons [requestFileName]; 43 43 44 resp.ContentLength64 = itemIconData.Length;45 resp.OutputStream.Write (itemIconData, 0, itemIconData.Length);44 _resp.ContentLength64 = itemIconData.Length; 45 _resp.OutputStream.Write (itemIconData, 0, itemIconData.Length); 46 46 } else { 47 resp.StatusCode = (int) HttpStatusCode.NotFound;47 _resp.StatusCode = (int) HttpStatusCode.NotFound; 48 48 if (logMissingFiles) { 49 Log.Out ("Web:IconHandler:FileNotFound: \"" + req.Url.AbsolutePath + "\" ");49 Log.Out ("Web:IconHandler:FileNotFound: \"" + _req.Url.AbsolutePath + "\" "); 50 50 } 51 51 } -
binary-improvements/MapRendering/Web/Handlers/PathHandler.cs
r325 r351 14 14 } 15 15 16 public abstract void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnectionuser,17 int permissionLevel);16 public abstract void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 17 int _permissionLevel); 18 18 19 public bool IsAuthorizedForHandler (WebConnection user, intpermissionLevel) {19 public bool IsAuthorizedForHandler (WebConnection _user, int _permissionLevel) { 20 20 if (moduleName != null) { 21 return WebPermissions.Instance.ModuleAllowedWithLevel (moduleName, permissionLevel);21 return WebPermissions.Instance.ModuleAllowedWithLevel (moduleName, _permissionLevel); 22 22 } 23 23 -
binary-improvements/MapRendering/Web/Handlers/SessionHandler.cs
r325 r351 10 10 private readonly string staticPart; 11 11 12 public SessionHandler (string _staticPart, string _dataFolder, Web _parent, string moduleName = null) :13 base ( moduleName) {12 public SessionHandler (string _staticPart, string _dataFolder, Web _parent, string _moduleName = null) : 13 base (_moduleName) { 14 14 staticPart = _staticPart; 15 15 parent = _parent; … … 24 24 } 25 25 26 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnectionuser,27 int permissionLevel) {28 string subpath = req.Url.AbsolutePath.Remove (0, staticPart.Length);26 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 27 int _permissionLevel) { 28 string subpath = _req.Url.AbsolutePath.Remove (0, staticPart.Length); 29 29 30 30 StringBuilder result = new StringBuilder (); … … 32 32 33 33 if (subpath.StartsWith ("verify")) { 34 if ( user != null) {35 resp.Redirect ("/static/index.html");34 if (_user != null) { 35 _resp.Redirect ("/static/index.html"); 36 36 return; 37 37 } … … 40 40 "<h1>Login failed, <a href=\"/static/index.html\">click to return to main page</a>.</h1>"); 41 41 } else if (subpath.StartsWith ("logout")) { 42 if ( user != null) {43 parent.connectionHandler.LogOut ( user.SessionID);42 if (_user != null) { 43 parent.connectionHandler.LogOut (_user.SessionID); 44 44 Cookie cookie = new Cookie ("sid", "", "/"); 45 45 cookie.Expired = true; 46 resp.AppendCookie (cookie);47 resp.Redirect ("/static/index.html");46 _resp.AppendCookie (cookie); 47 _resp.Redirect ("/static/index.html"); 48 48 return; 49 49 } … … 52 52 "<h1>Not logged in, <a href=\"/static/index.html\">click to return to main page</a>.</h1>"); 53 53 } else if (subpath.StartsWith ("login")) { 54 string host = (Web.isSslRedirected ( req) ? "https://" : "http://") +req.UserHostName;54 string host = (Web.isSslRedirected (_req) ? "https://" : "http://") + _req.UserHostName; 55 55 string url = OpenID.GetOpenIdLoginUrl (host, host + "/session/verify"); 56 resp.Redirect (url);56 _resp.Redirect (url); 57 57 return; 58 58 } else { … … 63 63 result.Append (footer); 64 64 65 resp.ContentType = MimeType.GetMimeType (".html");66 resp.ContentEncoding = Encoding.UTF8;65 _resp.ContentType = MimeType.GetMimeType (".html"); 66 _resp.ContentEncoding = Encoding.UTF8; 67 67 byte[] buf = Encoding.UTF8.GetBytes (result.ToString ()); 68 resp.ContentLength64 = buf.Length;69 resp.OutputStream.Write (buf, 0, buf.Length);68 _resp.ContentLength64 = buf.Length; 69 _resp.OutputStream.Write (buf, 0, buf.Length); 70 70 } 71 71 } -
binary-improvements/MapRendering/Web/Handlers/SimpleRedirectHandler.cs
r325 r351 5 5 private readonly string target; 6 6 7 public SimpleRedirectHandler (string target, string moduleName = null) : base (moduleName) {8 t his.target =target;7 public SimpleRedirectHandler (string _target, string _moduleName = null) : base (_moduleName) { 8 target = _target; 9 9 } 10 10 11 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnectionuser,12 int permissionLevel) {13 resp.Redirect (target);11 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 12 int _permissionLevel) { 13 _resp.Redirect (target); 14 14 } 15 15 } -
binary-improvements/MapRendering/Web/Handlers/StaticHandler.cs
r332 r351 10 10 private readonly string staticPart; 11 11 12 public StaticHandler (string staticPart, string filePath, AbstractCache cache, boollogMissingFiles,13 string moduleName = null) : base (moduleName) {14 this.staticPart =staticPart;15 datapath = filePath + (filePath [filePath.Length - 1] == '/' ? "" : "/");16 this.cache =cache;17 this.logMissingFiles =logMissingFiles;12 public StaticHandler (string _staticPart, string _filePath, AbstractCache _cache, bool _logMissingFiles, 13 string _moduleName = null) : base (_moduleName) { 14 staticPart = _staticPart; 15 datapath = _filePath + (_filePath [_filePath.Length - 1] == '/' ? "" : "/"); 16 cache = _cache; 17 logMissingFiles = _logMissingFiles; 18 18 } 19 19 20 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnectionuser,21 int permissionLevel) {22 string fn = req.Url.AbsolutePath.Remove (0, staticPart.Length);20 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 21 int _permissionLevel) { 22 string fn = _req.Url.AbsolutePath.Remove (0, staticPart.Length); 23 23 24 24 byte[] content = cache.GetFileContent (datapath + fn); 25 25 26 26 if (content != null) { 27 resp.ContentType = MimeType.GetMimeType (Path.GetExtension (fn));28 resp.ContentLength64 = content.Length;29 resp.OutputStream.Write (content, 0, content.Length);27 _resp.ContentType = MimeType.GetMimeType (Path.GetExtension (fn)); 28 _resp.ContentLength64 = content.Length; 29 _resp.OutputStream.Write (content, 0, content.Length); 30 30 } else { 31 resp.StatusCode = (int) HttpStatusCode.NotFound;31 _resp.StatusCode = (int) HttpStatusCode.NotFound; 32 32 if (logMissingFiles) { 33 Log.Out ("Web:Static:FileNotFound: \"" + req.Url.AbsolutePath + "\" @ \"" + datapath + fn + "\"");33 Log.Out ("Web:Static:FileNotFound: \"" + _req.Url.AbsolutePath + "\" @ \"" + datapath + fn + "\""); 34 34 } 35 35 } -
binary-improvements/MapRendering/Web/Handlers/UserStatusHandler.cs
r332 r351 5 5 namespace AllocsFixes.NetConnections.Servers.Web.Handlers { 6 6 public class UserStatusHandler : PathHandler { 7 public UserStatusHandler (string moduleName = null) : base (moduleName) {7 public UserStatusHandler (string _moduleName = null) : base (_moduleName) { 8 8 } 9 9 10 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnectionuser,11 int permissionLevel) {10 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 11 int _permissionLevel) { 12 12 JSONObject result = new JSONObject (); 13 13 14 result.Add ("loggedin", new JSONBoolean ( user != null));15 result.Add ("username", new JSONString ( user != null ?user.SteamID.ToString () : string.Empty));14 result.Add ("loggedin", new JSONBoolean (_user != null)); 15 result.Add ("username", new JSONString (_user != null ? _user.SteamID.ToString () : string.Empty)); 16 16 17 17 JSONArray perms = new JSONArray (); … … 19 19 JSONObject permObj = new JSONObject (); 20 20 permObj.Add ("module", new JSONString (perm.module)); 21 permObj.Add ("allowed", new JSONBoolean (perm.permissionLevel >= permissionLevel));21 permObj.Add ("allowed", new JSONBoolean (perm.permissionLevel >= _permissionLevel)); 22 22 perms.Add (permObj); 23 23 } … … 25 25 result.Add ("permissions", perms); 26 26 27 WebAPI.WriteJSON ( resp, result);27 WebAPI.WriteJSON (_resp, result); 28 28 } 29 29 }
Note:
See TracChangeset
for help on using the changeset viewer.