Changeset 434 for binary-improvements2/WebServer/src/WebAPI/AbsRestApi.cs
- Timestamp:
- May 17, 2023, 11:05:59 PM (18 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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
Note:
See TracChangeset
for help on using the changeset viewer.