Changeset 434
- Timestamp:
- May 17, 2023, 11:05:59 PM (20 months ago)
- Location:
- binary-improvements2
- Files:
-
- 8 added
- 39 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/CommandExtensions/ModInfo.xml
r432 r434 5 5 <Description value="Additional commands for server operation" /> 6 6 <Author value="The Fun Pimps LLC" /> 7 <Version value="21.0.28 0.0" />7 <Version value="21.0.288.0" /> 8 8 <Website value="" /> 9 9 </xml> -
binary-improvements2/MapRendering/ModInfo.xml
r432 r434 5 5 <Description value="Render the game map to image map tiles as it is uncovered" /> 6 6 <Author value="The Fun Pimps LLC" /> 7 <Version value="21.0.28 0.0" />7 <Version value="21.0.288.0" /> 8 8 <Website value="" /> 9 9 </xml> -
binary-improvements2/MapRendering/src/Api/Map.cs
r426 r434 24 24 break; 25 25 default: 26 SendE rrorResult(_context, HttpStatusCode.NotImplemented, _errorCode: "INVALID_ID");26 SendEmptyResponse (_context, HttpStatusCode.NotImplemented, _errorCode: "INVALID_ID"); 27 27 return; 28 28 } -
binary-improvements2/MarkersMod/ModInfo.xml
r432 r434 5 5 <Description value="Allows placing custom markers on the web map" /> 6 6 <Author value="Catalysm and Alloc" /> 7 <Version value="21.0.28 0.0" />7 <Version value="21.0.288.0" /> 8 8 <Website value="" /> 9 9 </xml> -
binary-improvements2/MarkersMod/src/Markers.cs
r430 r434 26 26 markers.Add (WebUtils.GenerateGuid (), (lat, lng, null)); 27 27 } 28 29 GameRandomManager.Instance.FreeGameRandom (random); 28 30 } 29 31 … … 58 60 59 61 if (!markers.TryGetValue (id, out (int, int, string) properties2)) { 60 writer.WriteRaw ( JsonEmptyData);62 writer.WriteRaw (WebUtils.JsonEmptyData); 61 63 SendEnvelopedResult (_context, ref writer, HttpStatusCode.NotFound); 62 64 return; … … 77 79 _writer.WriteString (_markerId); 78 80 _writer.WriteRaw (jsonKeyLat); 79 _writer.WriteInt32 (_properties.Item1); 81 (int lat, int lng, string icon) = _properties; 82 _writer.WriteInt32 (lat); 80 83 _writer.WriteRaw (jsonKeyLng); 81 _writer.WriteInt32 ( _properties.Item2);84 _writer.WriteInt32 (lng); 82 85 _writer.WriteRaw (jsonKeyIcon); 83 _writer.WriteString ( _properties.Item3?? defaultIcon);86 _writer.WriteString (icon ?? defaultIcon); 84 87 _writer.WriteEndObject (); 85 88 } 86 89 87 90 protected override void HandleRestPost (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) { 88 if (! TryGetJsonField (_jsonInput, "lat", out int lat)) {89 SendE rrorResult(_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LAT");91 if (!JsonCommons.TryGetJsonField (_jsonInput, "lat", out int lat)) { 92 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LAT"); 90 93 return; 91 94 } 92 95 93 if (! TryGetJsonField (_jsonInput, "lng", out int lng)) {94 SendE rrorResult(_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LNG");96 if (!JsonCommons.TryGetJsonField (_jsonInput, "lng", out int lng)) { 97 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LNG"); 95 98 return; 96 99 } 97 100 98 TryGetJsonField (_jsonInput, "icon", out string icon);101 JsonCommons.TryGetJsonField (_jsonInput, "icon", out string icon); 99 102 if (string.IsNullOrEmpty (icon)) { 100 103 icon = null; … … 110 113 111 114 protected override void HandleRestPut (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) { 112 if (! TryGetJsonField (_jsonInput, "lat", out int lat)) {113 SendE rrorResult(_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LAT");115 if (!JsonCommons.TryGetJsonField (_jsonInput, "lat", out int lat)) { 116 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LAT"); 114 117 return; 115 118 } 116 119 117 if (! TryGetJsonField (_jsonInput, "lng", out int lng)) {118 SendE rrorResult(_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LNG");120 if (!JsonCommons.TryGetJsonField (_jsonInput, "lng", out int lng)) { 121 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LNG"); 119 122 return; 120 123 } 121 124 122 125 bool keepIcon = !_jsonInput.TryGetValue ("icon", out _); 123 TryGetJsonField (_jsonInput, "icon", out string icon);126 JsonCommons.TryGetJsonField (_jsonInput, "icon", out string icon); 124 127 if (string.IsNullOrEmpty (icon)) { 125 128 icon = null; … … 129 132 130 133 if (!markers.TryGetValue (id, out (int, int, string) properties)) { 131 SendE rrorResult(_context, HttpStatusCode.NotFound, _jsonInputData, "ID_NOT_FOUND");134 SendEmptyResponse (_context, HttpStatusCode.NotFound, _jsonInputData, "ID_NOT_FOUND"); 132 135 return; 133 136 } … … 154 157 string id = _context.RequestPath; 155 158 156 PrepareEnvelopedResult (out JsonWriter writer); 157 writer.WriteRaw (JsonEmptyData); 158 SendEnvelopedResult (_context, ref writer, markers.Remove (id) ? HttpStatusCode.NoContent : HttpStatusCode.NotFound); 159 SendEmptyResponse (_context, markers.Remove (id) ? HttpStatusCode.NoContent : HttpStatusCode.NotFound); 159 160 } 160 161 -
binary-improvements2/WebServer/ModInfo.xml
r432 r434 5 5 <Description value="Integrated Webserver for the Web Dashboard and server APIs" /> 6 6 <Author value="The Fun Pimps LLC" /> 7 <Version value="21.0.28 0.0" />7 <Version value="21.0.288.0" /> 8 8 <Website value="" /> 9 9 </xml> -
binary-improvements2/WebServer/WebServer.csproj
r433 r434 124 124 <Compile Include="src\WebAPI\APIs\GameData\Mods.cs" /> 125 125 <Compile Include="src\WebAPI\APIs\Log.cs" /> 126 <Compile Include="src\WebAPI\APIs\Permissions\Blacklist.cs" /> 127 <Compile Include="src\WebAPI\APIs\Permissions\CommandPermissions.cs" /> 128 <Compile Include="src\WebAPI\APIs\Permissions\PermissionsApiHelpers.cs" /> 129 <Compile Include="src\WebAPI\APIs\Permissions\UserPermissions.cs" /> 126 130 <Compile Include="src\WebAPI\APIs\Permissions\RegisterUser.cs" /> 131 <Compile Include="src\WebAPI\APIs\Permissions\WebApiTokens.cs" /> 132 <Compile Include="src\WebAPI\APIs\Permissions\WebModules.cs" /> 133 <Compile Include="src\WebAPI\APIs\Permissions\WebUsers.cs" /> 134 <Compile Include="src\WebAPI\APIs\Permissions\Whitelist.cs" /> 127 135 <Compile Include="src\WebAPI\APIs\ServerInfo.cs" /> 128 136 <Compile Include="src\WebAPI\APIs\ServerStats.cs" /> -
binary-improvements2/WebServer/src/Commands/WebPermissionsCmd.cs
r426 r434 139 139 140 140 SdtdConsole.Instance.Output ($" {wmp.Name,-25}: {wmp.LevelGlobal,4}{(wmp.IsDefault ? " (default permissions)" : "")}"); 141 if (wmp.LevelPerMethod != null) { 142 for (int iMethod = 0; iMethod < wmp.LevelPerMethod.Length; iMethod++) { 143 int methodLevel = wmp.LevelPerMethod [iMethod]; 144 ERequestMethod method = (ERequestMethod)iMethod; 141 if (wmp.LevelPerMethod == null) { 142 continue; 143 } 144 145 for (int iMethod = 0; iMethod < wmp.LevelPerMethod.Length; iMethod++) { 146 int methodLevel = wmp.LevelPerMethod [iMethod]; 147 ERequestMethod method = (ERequestMethod)iMethod; 145 148 146 147 148 149 if (methodLevel == AdminWebModules.MethodLevelNotSupported) { 150 continue; 151 } 149 152 150 if (methodLevel == AdminWebModules.MethodLevelInheritGlobal) { 151 SdtdConsole.Instance.Output ($" {method.ToStringCached (),25}: {wmp.LevelGlobal,4} (Using API level)"); 152 } else { 153 SdtdConsole.Instance.Output ($" {method.ToStringCached (),25}: {methodLevel,4}"); 154 } 153 if (methodLevel == AdminWebModules.MethodLevelInheritGlobal) { 154 SdtdConsole.Instance.Output ($" {method.ToStringCached (),25}: {wmp.LevelGlobal,4} (Using API level)"); 155 } else { 156 SdtdConsole.Instance.Output ($" {method.ToStringCached (),25}: {methodLevel,4}"); 155 157 } 156 158 } -
binary-improvements2/WebServer/src/LogBuffer.cs
r391 r434 64 64 lock (logEntries) { 65 65 logEntries.Add (le); 66 if (logEntries.Count > maxEntries) { 67 listOffset += logEntries.Count - maxEntries; 68 logEntries.RemoveRange (0, logEntries.Count - maxEntries); 66 if (logEntries.Count <= maxEntries) { 67 return; 69 68 } 69 70 listOffset += logEntries.Count - maxEntries; 71 logEntries.RemoveRange (0, logEntries.Count - maxEntries); 70 72 } 71 73 } … … 124 126 125 127 public class LogEntry { 126 public readonly DateTime timestamp;127 public readonly string isoTime;128 public readonly string message;129 public readonly string trace;130 public readonly LogType type;131 public readonly long uptime;128 public readonly DateTime Timestamp; 129 public readonly string IsoTime; 130 public readonly string Message; 131 public readonly string Trace; 132 public readonly LogType Type; 133 public readonly long Uptime; 132 134 133 135 public LogEntry (DateTime _timestamp, string _message, string _trace, LogType _type, long _uptime) { 134 timestamp = _timestamp;135 isoTime = _timestamp.ToString ("o");136 Timestamp = _timestamp; 137 IsoTime = _timestamp.ToString ("o"); 136 138 137 message = _message;138 trace = _trace;139 type = _type;140 uptime = _uptime;139 Message = _message; 140 Trace = _trace; 141 Type = _type; 142 Uptime = _uptime; 141 143 } 142 144 } -
binary-improvements2/WebServer/src/Permissions/AdminWebModules.cs
r427 r434 1 1 using System.Collections.Generic; 2 2 using System.Xml; 3 using UnityEngine;4 3 5 4 namespace Webserver.Permissions { -
binary-improvements2/WebServer/src/UrlHandlers/AbsHandler.cs
r426 r434 3 3 namespace Webserver.UrlHandlers { 4 4 public abstract class AbsHandler { 5 p rotected readonly string moduleName;5 public readonly string ModuleName; 6 6 protected string urlBasePath; 7 7 protected Web parent; 8 8 9 public string ModuleName => moduleName;10 9 public string UrlBasePath => urlBasePath; 11 10 12 11 protected AbsHandler (string _moduleName, int _defaultPermissionLevel = 0) { 13 moduleName = _moduleName;12 ModuleName = _moduleName; 14 13 AdminWebModules.Instance.AddKnownModule (new AdminWebModules.WebModule(_moduleName, _defaultPermissionLevel, true)); 15 14 } … … 18 17 19 18 public virtual bool IsAuthorizedForHandler (WebConnection _user, int _permissionLevel) { 20 return moduleName == null || AdminWebModules.Instance.ModuleAllowedWithLevel (moduleName, _permissionLevel);19 return ModuleName == null || AdminWebModules.Instance.ModuleAllowedWithLevel (ModuleName, _permissionLevel); 21 20 } 22 21 -
binary-improvements2/WebServer/src/Web.cs
r431 r434 19 19 20 20 private readonly List<AbsHandler> handlers = new List<AbsHandler> (); 21 public readonly List<WebMod> webMods = new List<WebMod> ();21 public readonly List<WebMod> WebMods = new List<WebMod> (); 22 22 public readonly ConnectionHandler ConnectionHandler; 23 23 … … 134 134 try { 135 135 WebMod webMod = new WebMod (this, mod, _useStaticCache); 136 webMods.Add (webMod);136 WebMods.Add (webMod); 137 137 } catch (InvalidDataException e) { 138 138 Log.Error ($"[Web] Could not load webmod from mod {mod.Name}: {e.Message}"); -
binary-improvements2/WebServer/src/WebAPI/APIs/Command.cs
r426 r434 91 91 92 92 protected override void HandleRestPost (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) { 93 if (! TryGetJsonField (_jsonInput, "command", out string commandString)) {94 SendE rrorResult(_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_COMMAND");93 if (!JsonCommons.TryGetJsonField (_jsonInput, "command", out string commandString)) { 94 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_COMMAND"); 95 95 return; 96 96 } … … 98 98 WebCommandResult.ResultType responseType = WebCommandResult.ResultType.Full; 99 99 100 if ( TryGetJsonField (_jsonInput, "format", out string formatString)) {100 if (JsonCommons.TryGetJsonField (_jsonInput, "format", out string formatString)) { 101 101 if (formatString.EqualsCaseInsensitive ("raw")) { 102 102 responseType = WebCommandResult.ResultType.Raw; … … 115 115 116 116 if (command == null) { 117 SendE rrorResult(_context, HttpStatusCode.NotFound, _jsonInputData, "UNKNOWN_COMMAND");117 SendEmptyResponse (_context, HttpStatusCode.NotFound, _jsonInputData, "UNKNOWN_COMMAND"); 118 118 return; 119 119 } … … 122 122 123 123 if (_context.PermissionLevel > commandPermissionLevel) { 124 SendE rrorResult(_context, HttpStatusCode.Forbidden, _jsonInputData, "NO_PERMISSION");124 SendEmptyResponse (_context, HttpStatusCode.Forbidden, _jsonInputData, "NO_PERMISSION"); 125 125 return; 126 126 } -
binary-improvements2/WebServer/src/WebAPI/APIs/GameData/Item.cs
r433 r434 3 3 using Webserver.Permissions; 4 4 5 namespace Webserver.WebAPI.APIs {5 namespace Webserver.WebAPI.APIs.GameData { 6 6 [UsedImplicitly] 7 7 internal class Item : AbsRestApi { -
binary-improvements2/WebServer/src/WebAPI/APIs/GameData/Mods.cs
r433 r434 3 3 using Webserver.Permissions; 4 4 5 namespace Webserver.WebAPI.APIs {5 namespace Webserver.WebAPI.APIs.GameData { 6 6 [UsedImplicitly] 7 7 public class Mods : AbsRestApi { … … 12 12 writer.WriteBeginArray (); 13 13 14 for (int i = 0; i < _parent. webMods.Count; i++) {15 WebMod webMod = _parent. webMods [i];14 for (int i = 0; i < _parent.WebMods.Count; i++) { 15 WebMod webMod = _parent.WebMods [i]; 16 16 17 17 if (i > 0) { … … 62 62 _writer.WriteValueSeparator (); 63 63 _writer.WritePropertyName ("displayName"); 64 JsonCommons.WriteStringOrNull (ref _writer,_webMod.ParentMod.DisplayName);64 _writer.WriteString (_webMod.ParentMod.DisplayName); 65 65 66 66 _writer.WriteValueSeparator (); 67 67 _writer.WritePropertyName ("description"); 68 JsonCommons.WriteStringOrNull (ref _writer,_webMod.ParentMod.Description);68 _writer.WriteString (_webMod.ParentMod.Description); 69 69 70 70 _writer.WriteValueSeparator (); 71 71 _writer.WritePropertyName ("author"); 72 JsonCommons.WriteStringOrNull (ref _writer,_webMod.ParentMod.Author);72 _writer.WriteString (_webMod.ParentMod.Author); 73 73 74 74 _writer.WriteValueSeparator (); 75 75 _writer.WritePropertyName ("version"); 76 JsonCommons.WriteStringOrNull (ref _writer,_webMod.ParentMod.VersionString);76 _writer.WriteString (_webMod.ParentMod.VersionString); 77 77 78 78 _writer.WriteValueSeparator (); 79 79 _writer.WritePropertyName ("website"); 80 JsonCommons.WriteStringOrNull (ref _writer,_webMod.ParentMod.Website);80 _writer.WriteString (_webMod.ParentMod.Website); 81 81 } 82 82 -
binary-improvements2/WebServer/src/WebAPI/APIs/Log.cs
r408 r434 55 55 56 56 writer.WriteRaw (jsonMsgKey); 57 writer.WriteString (logEntry. message);57 writer.WriteString (logEntry.Message); 58 58 59 59 writer.WriteRaw (jsonTypeKey); 60 writer.WriteString (logEntry. type.ToStringCached ());60 writer.WriteString (logEntry.Type.ToStringCached ()); 61 61 62 62 writer.WriteRaw (jsonTraceKey); 63 writer.WriteString (logEntry. trace);63 writer.WriteString (logEntry.Trace); 64 64 65 65 writer.WriteRaw (jsonIsotimeKey); 66 writer.WriteString (logEntry. isoTime);66 writer.WriteString (logEntry.IsoTime); 67 67 68 68 writer.WriteRaw (jsonUptimeKey); 69 writer.WriteString (logEntry. uptime.ToString ());69 writer.WriteString (logEntry.Uptime.ToString ()); 70 70 71 71 writer.WriteEndObject (); -
binary-improvements2/WebServer/src/WebAPI/APIs/Permissions/RegisterUser.cs
r433 r434 9 9 using Webserver.UrlHandlers; 10 10 11 namespace Webserver.WebAPI.APIs {11 namespace Webserver.WebAPI.APIs.Permissions { 12 12 [UsedImplicitly] 13 13 public class RegisterUser : AbsRestApi { … … 27 27 28 28 if (string.IsNullOrEmpty (token)) { 29 SendE rrorResult(_context, HttpStatusCode.BadRequest, null, "NO_TOKEN");29 SendEmptyResponse (_context, HttpStatusCode.BadRequest, null, "NO_TOKEN"); 30 30 return; 31 31 } 32 32 33 33 if (!UserRegistrationTokens.TryValidate (token, out UserRegistrationTokens.RegistrationData regData)) { 34 SendE rrorResult(_context, HttpStatusCode.NotFound, null, "INVALID_OR_EXPIRED_TOKEN");34 SendEmptyResponse (_context, HttpStatusCode.NotFound, null, "INVALID_OR_EXPIRED_TOKEN"); 35 35 return; 36 36 } … … 50 50 51 51 protected override void HandleRestPost (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) { 52 if (! TryGetJsonField (_jsonInput, "token", out string token)) {53 SendE rrorResult(_context, HttpStatusCode.BadRequest, _jsonInputData, "MISSING_TOKEN");52 if (!JsonCommons.TryGetJsonField (_jsonInput, "token", out string token)) { 53 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "MISSING_TOKEN"); 54 54 return; 55 55 } 56 56 57 if (! TryGetJsonField (_jsonInput, "username", out string username)) {58 SendE rrorResult(_context, HttpStatusCode.BadRequest, _jsonInputData, "MISSING_USERNAME");57 if (!JsonCommons.TryGetJsonField (_jsonInput, "username", out string username)) { 58 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "MISSING_USERNAME"); 59 59 return; 60 60 } 61 61 62 if (! TryGetJsonField (_jsonInput, "password", out string password)) {63 SendE rrorResult(_context, HttpStatusCode.BadRequest, _jsonInputData, "MISSING_PASSWORD");62 if (!JsonCommons.TryGetJsonField (_jsonInput, "password", out string password)) { 63 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "MISSING_PASSWORD"); 64 64 return; 65 65 } 66 66 67 67 if (!UserRegistrationTokens.TryValidate (token, out UserRegistrationTokens.RegistrationData regData)) { 68 SendE rrorResult(_context, HttpStatusCode.Unauthorized, null, "INVALID_OR_EXPIRED_TOKEN");68 SendEmptyResponse (_context, HttpStatusCode.Unauthorized, null, "INVALID_OR_EXPIRED_TOKEN"); 69 69 return; 70 70 } 71 71 72 72 if (!userValidationRegex.IsMatch (username)) { 73 SendE rrorResult(_context, HttpStatusCode.Unauthorized, _jsonInputData, "INVALID_USERNAME");73 SendEmptyResponse (_context, HttpStatusCode.Unauthorized, _jsonInputData, "INVALID_USERNAME"); 74 74 return; 75 75 } 76 76 77 77 if (!passValidationRegex.IsMatch (password)) { 78 SendE rrorResult(_context, HttpStatusCode.Unauthorized, _jsonInputData, "INVALID_PASSWORD");78 SendEmptyResponse (_context, HttpStatusCode.Unauthorized, _jsonInputData, "INVALID_PASSWORD"); 79 79 return; 80 80 } … … 86 86 !PlatformUserIdentifierAbs.Equals (existingMapping.CrossPlatformUser, regData.CrossPlatformUserId)) { 87 87 // Username already in use by another player 88 SendE rrorResult(_context, HttpStatusCode.Unauthorized, _jsonInputData, "DUPLICATE_USERNAME");88 SendEmptyResponse (_context, HttpStatusCode.Unauthorized, _jsonInputData, "DUPLICATE_USERNAME"); 89 89 return; 90 90 } -
binary-improvements2/WebServer/src/WebAPI/APIs/WorldState/Animal.cs
r433 r434 4 4 using Webserver.LiveData; 5 5 6 namespace Webserver.WebAPI.APIs {6 namespace Webserver.WebAPI.APIs.WorldState { 7 7 [UsedImplicitly] 8 8 internal class Animal : AbsRestApi { -
binary-improvements2/WebServer/src/WebAPI/APIs/WorldState/Hostile.cs
r433 r434 4 4 using Webserver.LiveData; 5 5 6 namespace Webserver.WebAPI.APIs {6 namespace Webserver.WebAPI.APIs.WorldState { 7 7 [UsedImplicitly] 8 8 internal class Hostile : AbsRestApi { -
binary-improvements2/WebServer/src/WebAPI/APIs/WorldState/Player.cs
r433 r434 6 6 using Webserver.Permissions; 7 7 8 namespace Webserver.WebAPI.APIs {8 namespace Webserver.WebAPI.APIs.WorldState { 9 9 [UsedImplicitly] 10 10 public class Player : AbsRestApi { … … 216 216 217 217 protected override void HandleRestPost (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) { 218 if (! TryGetJsonField (_jsonInput, "command", out string commandString)) {219 SendE rrorResult(_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_COMMAND");218 if (!JsonCommons.TryGetJsonField (_jsonInput, "command", out string commandString)) { 219 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_COMMAND"); 220 220 return; 221 221 } … … 223 223 WebCommandResult.ResultType responseType = WebCommandResult.ResultType.Full; 224 224 225 if ( TryGetJsonField (_jsonInput, "format", out string formatString)) {225 if (JsonCommons.TryGetJsonField (_jsonInput, "format", out string formatString)) { 226 226 if (formatString.EqualsCaseInsensitive ("raw")) { 227 227 responseType = WebCommandResult.ResultType.Raw; … … 240 240 241 241 if (command == null) { 242 SendE rrorResult(_context, HttpStatusCode.NotFound, _jsonInputData, "UNKNOWN_COMMAND");242 SendEmptyResponse (_context, HttpStatusCode.NotFound, _jsonInputData, "UNKNOWN_COMMAND"); 243 243 return; 244 244 } … … 247 247 248 248 if (_context.PermissionLevel > commandPermissionLevel) { 249 SendE rrorResult(_context, HttpStatusCode.Forbidden, _jsonInputData, "NO_PERMISSION");249 SendEmptyResponse (_context, HttpStatusCode.Forbidden, _jsonInputData, "NO_PERMISSION"); 250 250 return; 251 251 } -
binary-improvements2/WebServer/src/WebAPI/AbsRestApi.cs
r426 r434 44 44 jsonDeserializeSampler.End (); 45 45 46 SendE rrorResult(_context, HttpStatusCode.BadRequest, null, "INVALID_BODY", e);46 SendEmptyResponse (_context, HttpStatusCode.BadRequest, null, "INVALID_BODY", e); 47 47 return; 48 48 } … … 53 53 case ERequestMethod.GET: 54 54 if (inputJson != null) { 55 SendE rrorResult(_context, HttpStatusCode.BadRequest, jsonInputData, "GET_WITH_BODY");55 SendEmptyResponse (_context, HttpStatusCode.BadRequest, jsonInputData, "GET_WITH_BODY"); 56 56 return; 57 57 } … … 60 60 return; 61 61 case ERequestMethod.POST: 62 if (! string.IsNullOrEmpty (_context.RequestPath)) {63 SendE rrorResult(_context, HttpStatusCode.BadRequest, jsonInputData, "POST_WITH_ID");62 if (!AllowPostWithId && !string.IsNullOrEmpty (_context.RequestPath)) { 63 SendEmptyResponse (_context, HttpStatusCode.BadRequest, jsonInputData, "POST_WITH_ID"); 64 64 return; 65 65 } 66 66 67 67 if (inputJson == null) { 68 SendE rrorResult(_context, HttpStatusCode.BadRequest, null, "POST_WITHOUT_BODY");68 SendEmptyResponse (_context, HttpStatusCode.BadRequest, null, "POST_WITHOUT_BODY"); 69 69 return; 70 70 } … … 74 74 case ERequestMethod.PUT: 75 75 if (string.IsNullOrEmpty (_context.RequestPath)) { 76 SendE rrorResult(_context, HttpStatusCode.BadRequest, jsonInputData, "PUT_WITHOUT_ID");76 SendEmptyResponse (_context, HttpStatusCode.BadRequest, jsonInputData, "PUT_WITHOUT_ID"); 77 77 return; 78 78 } 79 79 80 80 if (inputJson == null) { 81 SendE rrorResult(_context, HttpStatusCode.BadRequest, null, "PUT_WITHOUT_BODY");81 SendEmptyResponse (_context, HttpStatusCode.BadRequest, null, "PUT_WITHOUT_BODY"); 82 82 return; 83 83 } … … 87 87 case ERequestMethod.DELETE: 88 88 if (string.IsNullOrEmpty (_context.RequestPath)) { 89 SendE rrorResult(_context, HttpStatusCode.BadRequest, jsonInputData, "DELETE_WITHOUT_ID");89 SendEmptyResponse (_context, HttpStatusCode.BadRequest, jsonInputData, "DELETE_WITHOUT_ID"); 90 90 return; 91 91 } 92 92 93 93 if (inputJson != null) { 94 SendE rrorResult(_context, HttpStatusCode.BadRequest, null, "DELETE_WITH_BODY");94 SendEmptyResponse (_context, HttpStatusCode.BadRequest, null, "DELETE_WITH_BODY"); 95 95 return; 96 96 } … … 99 99 return; 100 100 default: 101 SendE rrorResult(_context, HttpStatusCode.BadRequest, null, "INVALID_METHOD");101 SendEmptyResponse (_context, HttpStatusCode.BadRequest, null, "INVALID_METHOD"); 102 102 return; 103 103 } 104 104 } catch (Exception e) { 105 SendE rrorResult(_context, HttpStatusCode.InternalServerError, jsonInputData, "ERROR_PROCESSING", e);105 SendEmptyResponse (_context, HttpStatusCode.InternalServerError, jsonInputData, "ERROR_PROCESSING", e); 106 106 } 107 107 } 108 108 109 109 protected virtual void HandleRestGet (RequestContext _context) { 110 SendE rrorResult(_context, HttpStatusCode.MethodNotAllowed, null, "Unsupported");110 SendEmptyResponse (_context, HttpStatusCode.MethodNotAllowed, null, "Unsupported"); 111 111 } 112 112 113 113 protected virtual void HandleRestPost (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) { 114 SendE rrorResult(_context, HttpStatusCode.MethodNotAllowed, _jsonInputData, "Unsupported");114 SendEmptyResponse (_context, HttpStatusCode.MethodNotAllowed, _jsonInputData, "Unsupported"); 115 115 } 116 116 117 117 protected virtual void HandleRestPut (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) { 118 SendE rrorResult(_context, HttpStatusCode.MethodNotAllowed, _jsonInputData, "Unsupported");118 SendEmptyResponse (_context, HttpStatusCode.MethodNotAllowed, _jsonInputData, "Unsupported"); 119 119 } 120 120 121 121 protected virtual void HandleRestDelete (RequestContext _context) { 122 SendE rrorResult(_context, HttpStatusCode.MethodNotAllowed, null, "Unsupported");122 SendEmptyResponse (_context, HttpStatusCode.MethodNotAllowed, null, "Unsupported"); 123 123 } 124 124 … … 126 126 AdminWebModules.WebModule module = AdminWebModules.Instance.GetModule (CachedApiModuleName); 127 127 128 if (module.LevelPerMethod != null) { 129 int perMethodLevel = module.LevelPerMethod [(int)_context.Method]; 130 if (perMethodLevel == AdminWebModules.MethodLevelNotSupported) { 131 return false; 132 } 128 if (module.LevelPerMethod == null) { 129 return module.LevelGlobal >= _context.PermissionLevel; 130 } 133 131 134 if (perMethodLevel != AdminWebModules.MethodLevelInheritGlobal) { 135 return perMethodLevel >= _context.PermissionLevel; 136 } 132 int perMethodLevel = module.LevelPerMethod [(int)_context.Method]; 133 if (perMethodLevel == AdminWebModules.MethodLevelNotSupported) { 134 return false; 135 } 136 137 if (perMethodLevel != AdminWebModules.MethodLevelInheritGlobal) { 138 return perMethodLevel >= _context.PermissionLevel; 137 139 } 138 140 139 141 return module.LevelGlobal >= _context.PermissionLevel; 140 142 } 143 144 protected virtual bool AllowPostWithId => false; 141 145 142 146 /// <summary> … … 154 158 #region Helpers 155 159 156 protected static readonly byte[] JsonEmptyData;157 158 static AbsRestApi () {159 JsonWriter writer = new JsonWriter ();160 writer.WriteBeginArray ();161 writer.WriteEndArray ();162 JsonEmptyData = writer.ToUtf8ByteArray ();163 }164 165 160 protected static void PrepareEnvelopedResult (out JsonWriter _writer) { 166 161 WebUtils.PrepareEnvelopedResult (out _writer); … … 173 168 } 174 169 175 protected static void SendE rrorResult (RequestContext _context, HttpStatusCode _statusCode, byte[] _jsonInputData = null, string _errorCode = null, Exception _exception = null) {170 protected static void SendEmptyResponse (RequestContext _context, HttpStatusCode _statusCode = HttpStatusCode.OK, byte[] _jsonInputData = null, string _errorCode = null, Exception _exception = null) { 176 171 PrepareEnvelopedResult (out JsonWriter writer); 177 writer.WriteRaw ( JsonEmptyData);172 writer.WriteRaw (WebUtils.JsonEmptyData); 178 173 SendEnvelopedResult (_context, ref writer, _statusCode, _jsonInputData, _errorCode, _exception); 179 174 } 180 181 protected static bool TryGetJsonField (IDictionary<string, object> _jsonObject, string _fieldName, out int _value) {182 _value = default;183 184 if (!_jsonObject.TryGetValue (_fieldName, out object fieldNode)) {185 return false;186 }187 188 if (fieldNode is not double value) {189 return false;190 }191 192 try {193 _value = (int)value;194 return true;195 } catch (Exception) {196 return false;197 }198 }199 200 protected static bool TryGetJsonField (IDictionary<string, object> _jsonObject, string _fieldName, out double _value) {201 _value = default;202 203 if (!_jsonObject.TryGetValue (_fieldName, out object fieldNode)) {204 return false;205 }206 207 if (fieldNode is not double value) {208 return false;209 }210 211 try {212 _value = value;213 return true;214 } catch (Exception) {215 return false;216 }217 }218 219 protected static bool TryGetJsonField (IDictionary<string, object> _jsonObject, string _fieldName, out string _value) {220 _value = default;221 222 if (!_jsonObject.TryGetValue (_fieldName, out object fieldNode)) {223 return false;224 }225 226 if (fieldNode is not string value) {227 return false;228 }229 230 try {231 _value = value;232 return true;233 } catch (Exception) {234 return false;235 }236 }237 238 175 239 176 #endregion -
binary-improvements2/WebServer/src/WebAPI/JsonCommons.cs
r426 r434 1 1 using System; 2 using System.Collections.Generic; 3 using System.Globalization; 2 4 using UnityEngine; 3 5 using Utf8Json; … … 57 59 } 58 60 61 public static bool TryReadPlatformUserIdentifier (IDictionary<string, object> _jsonInput, out PlatformUserIdentifierAbs _userIdentifier) { 62 if (TryGetJsonField (_jsonInput, "combinedString", out string combinedString)) { 63 _userIdentifier = PlatformUserIdentifierAbs.FromCombinedString (combinedString, false); 64 if (_userIdentifier != null) { 65 return true; 66 } 67 } 68 69 if (!TryGetJsonField (_jsonInput, "platformId", out string platformId)) { 70 _userIdentifier = default; 71 return false; 72 } 73 74 if (!TryGetJsonField (_jsonInput, "userId", out string userId)) { 75 _userIdentifier = default; 76 return false; 77 } 78 79 _userIdentifier = PlatformUserIdentifierAbs.FromPlatformAndId (platformId, userId, false); 80 return _userIdentifier != null; 81 } 82 59 83 public static void WriteDateTime (ref JsonWriter _writer, DateTime _dateTime) { 60 84 _writer.WriteString (_dateTime.ToString ("o")); 61 85 } 62 86 63 public static void WriteStringOrNull (ref JsonWriter _writer, string _string) { 64 if (_string == null) { 65 _writer.WriteNull (); 66 } else { 67 _writer.WriteString (_string); 87 public static bool TryReadDateTime (IDictionary<string, object> _jsonInput, string _fieldName, out DateTime _result) { 88 _result = default; 89 90 if (!TryGetJsonField (_jsonInput, _fieldName, out string dateTimeString)) { 91 return false; 92 } 93 94 return DateTime.TryParse (dateTimeString, null, DateTimeStyles.RoundtripKind, out _result); 95 } 96 97 98 public static bool TryGetJsonField (IDictionary<string, object> _jsonObject, string _fieldName, out int _value) { 99 _value = default; 100 101 if (!_jsonObject.TryGetValue (_fieldName, out object fieldNode)) { 102 return false; 103 } 104 105 if (fieldNode is not double value) { 106 return false; 107 } 108 109 try { 110 _value = (int)value; 111 return true; 112 } catch (Exception) { 113 return false; 114 } 115 } 116 117 public static bool TryGetJsonField (IDictionary<string, object> _jsonObject, string _fieldName, out double _value) { 118 _value = default; 119 120 if (!_jsonObject.TryGetValue (_fieldName, out object fieldNode)) { 121 return false; 122 } 123 124 if (fieldNode is not double value) { 125 return false; 126 } 127 128 try { 129 _value = value; 130 return true; 131 } catch (Exception) { 132 return false; 133 } 134 } 135 136 public static bool TryGetJsonField (IDictionary<string, object> _jsonObject, string _fieldName, out string _value) { 137 _value = default; 138 139 if (!_jsonObject.TryGetValue (_fieldName, out object fieldNode)) { 140 return false; 141 } 142 143 if (fieldNode is not string value) { 144 return false; 145 } 146 147 try { 148 _value = value; 149 return true; 150 } catch (Exception) { 151 return false; 152 } 153 } 154 155 public static bool TryGetJsonField (IDictionary<string, object> _jsonObject, string _fieldName, 156 out IDictionary<string, object> _value) { 157 _value = default; 158 159 if (!_jsonObject.TryGetValue (_fieldName, out object fieldNode)) { 160 return false; 161 } 162 163 if (fieldNode is not IDictionary<string, object> value) { 164 return false; 165 } 166 167 try { 168 _value = value; 169 return true; 170 } catch (Exception) { 171 return false; 68 172 } 69 173 } -
binary-improvements2/WebServer/src/WebConnection.cs
r425 r434 3 3 using System.Net; 4 4 using UnityEngine; 5 using Webserver.Permissions;6 5 7 6 namespace Webserver { -
binary-improvements2/WebServer/src/WebMod.cs
r426 r434 20 20 IsWebMod = Directory.Exists (folder); 21 21 22 if (IsWebMod) { 23 string urlWebModBase = $"{modsBaseUrl}{_parentMod.FolderName}/"; 22 if (!IsWebMod) { 23 return; 24 } 24 25 25 ReactBundle = $"{folder}/{reactBundleName}"; 26 ReactBundle = File.Exists (ReactBundle) ? $"{urlWebModBase}{reactBundleName}" : null; 26 string urlWebModBase = $"{modsBaseUrl}{_parentMod.FolderName}/"; 27 27 28 CssPath = $"{folder}/{stylingFileName}";29 CssPath = File.Exists (CssPath) ? $"{urlWebModBase}{stylingFileName}" : null;28 ReactBundle = $"{folder}/{reactBundleName}"; 29 ReactBundle = File.Exists (ReactBundle) ? $"{urlWebModBase}{reactBundleName}" : null; 30 30 31 _parentWeb.RegisterPathHandler (urlWebModBase, new StaticHandler ( 32 folder, 33 _useStaticCache ? new SimpleCache () : new DirectAccess (), 34 false) 35 ); 36 } 31 CssPath = $"{folder}/{stylingFileName}"; 32 CssPath = File.Exists (CssPath) ? $"{urlWebModBase}{stylingFileName}" : null; 33 34 _parentWeb.RegisterPathHandler (urlWebModBase, new StaticHandler ( 35 folder, 36 _useStaticCache ? new SimpleCache () : new DirectAccess (), 37 false) 38 ); 37 39 } 38 40 } -
binary-improvements2/WebServer/src/WebUtils.cs
r425 r434 4 4 using System.Net; 5 5 using System.Text; 6 using UnityEngine.Profiling; 6 7 using Utf8Json; 7 8 using Webserver.WebAPI; … … 11 12 namespace Webserver { 12 13 public static class WebUtils { 14 static WebUtils () { 15 JsonWriter writer = new JsonWriter (); 16 writer.WriteBeginArray (); 17 writer.WriteEndArray (); 18 JsonEmptyData = writer.ToUtf8ByteArray (); 19 } 20 13 21 // ReSharper disable once MemberCanBePrivate.Global 14 22 public const string MimePlain = "text/plain"; … … 17 25 public const string MimeJson = "application/json"; 18 26 19 private static readonly UnityEngine.Profiling.CustomSampler envelopeBuildSampler = UnityEngine.Profiling.CustomSampler.Create ("JSON_EnvelopeBuilding");20 private static readonly UnityEngine.Profiling.CustomSampler netWriteSampler = UnityEngine.Profiling.CustomSampler.Create ("JSON_Write");27 private static readonly CustomSampler envelopeBuildSampler = CustomSampler.Create ("JSON_EnvelopeBuilding"); 28 private static readonly CustomSampler netWriteSampler = CustomSampler.Create ("JSON_Write"); 21 29 22 30 public static void WriteText (HttpListenerResponse _resp, string _text, HttpStatusCode _statusCode = HttpStatusCode.OK, string _mimeType = null) { … … 56 64 } 57 65 58 66 public static readonly byte[] JsonEmptyData; 67 59 68 private static readonly byte[] jsonRawDataKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("data"); // {"data": 60 69 … … 117 126 } 118 127 128 public static void SendEmptyResponse (RequestContext _context, HttpStatusCode _statusCode = HttpStatusCode.OK, byte[] _jsonInputData = null, string _errorCode = null, Exception _exception = null) { 129 PrepareEnvelopedResult (out JsonWriter writer); 130 writer.WriteRaw (JsonEmptyData); 131 SendEnvelopedResult (_context, ref writer, _statusCode, _jsonInputData, _errorCode, _exception); 132 } 133 134 119 135 public static bool TryGetValue (this NameValueCollection _nameValueCollection, string _name, out string _result) { 120 136 _result = _nameValueCollection [_name]; -
binary-improvements2/bin/Mods/TFP_CommandExtensions/ModInfo.xml
r432 r434 5 5 <Description value="Additional commands for server operation" /> 6 6 <Author value="The Fun Pimps LLC" /> 7 <Version value="21.0.28 0.0" />7 <Version value="21.0.288.0" /> 8 8 <Website value="" /> 9 9 </xml> -
binary-improvements2/bin/Mods/TFP_MapRendering/ModInfo.xml
r432 r434 5 5 <Description value="Render the game map to image map tiles as it is uncovered" /> 6 6 <Author value="The Fun Pimps LLC" /> 7 <Version value="21.0.28 0.0" />7 <Version value="21.0.288.0" /> 8 8 <Website value="" /> 9 9 </xml> -
binary-improvements2/bin/Mods/TFP_WebServer/ModInfo.xml
r432 r434 5 5 <Description value="Integrated Webserver for the Web Dashboard and server APIs" /> 6 6 <Author value="The Fun Pimps LLC" /> 7 <Version value="21.0.28 0.0" />7 <Version value="21.0.288.0" /> 8 8 <Website value="" /> 9 9 </xml> -
binary-improvements2/bin/Mods/Xample_MarkersMod/ModInfo.xml
r432 r434 5 5 <Description value="Allows placing custom markers on the web map" /> 6 6 <Author value="Catalysm and Alloc" /> 7 <Version value="21.0.28 0.0" />7 <Version value="21.0.288.0" /> 8 8 <Website value="" /> 9 9 </xml>
Note:
See TracChangeset
for help on using the changeset viewer.