Changeset 402 for binary-improvements2/WebServer/src/WebAPI/AbsRestApi.cs
- Timestamp:
- Jan 27, 2023, 7:28:00 PM (22 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/WebServer/src/WebAPI/AbsRestApi.cs
r394 r402 1 1 using System; 2 using System.Collections.Generic; 2 3 using System.IO; 3 4 using System.Net; 4 using AllocsFixes.JSON;5 using Utf8Json; 5 6 6 7 namespace Webserver.WebAPI { … … 9 10 10 11 public sealed override void HandleRequest (RequestContext _context) { 11 JsonNode jsonBody = null; 12 IDictionary<string, object> inputJson = null; 13 byte[] jsonInputData = null; 14 15 if (_context.Request.HasEntityBody) { 16 Stream requestInputStream = _context.Request.InputStream; 17 18 jsonInputData = new byte[_context.Request.ContentLength64]; 19 requestInputStream.Read (jsonInputData, 0, (int)_context.Request.ContentLength64); 20 21 try { 22 jsonDeserializeSampler.Begin (); 23 inputJson = JsonSerializer.Deserialize<IDictionary<string, object>> (jsonInputData); 24 25 // Log.Out ("JSON body:"); 26 // foreach ((string key, object value) in inputJson) { 27 // Log.Out ($" - {key} = {value} ({value.GetType ()})"); 28 // } 29 30 jsonDeserializeSampler.End (); 31 } catch (Exception e) { 32 jsonDeserializeSampler.End (); 12 33 13 if (_context.Request.HasEntityBody) { 14 string body = new StreamReader (_context.Request.InputStream).ReadToEnd (); 15 16 if (!string.IsNullOrEmpty (body)) { 17 try { 18 jsonDeserializeSampler.Begin (); 19 jsonBody = Parser.Parse (body); 20 jsonDeserializeSampler.End (); 21 } catch (Exception e) { 22 jsonDeserializeSampler.End (); 23 24 SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, null, "INVALID_BODY", e); 25 return; 26 } 34 SendErrorResult (_context, HttpStatusCode.BadRequest, null, "INVALID_BODY", e); 35 return; 27 36 } 28 37 } … … 31 40 switch (_context.Request.HttpMethod) { 32 41 case "GET": 33 if ( jsonBody!= null) {34 SendE nvelopedResult (_context, null, HttpStatusCode.BadRequest, jsonBody, "GET_WITH_BODY");42 if (inputJson != null) { 43 SendErrorResult (_context, HttpStatusCode.BadRequest, jsonInputData, "GET_WITH_BODY"); 35 44 return; 36 45 } … … 40 49 case "POST": 41 50 if (!string.IsNullOrEmpty (_context.RequestPath)) { 42 SendE nvelopedResult (_context, null, HttpStatusCode.BadRequest, jsonBody, "POST_WITH_ID");51 SendErrorResult (_context, HttpStatusCode.BadRequest, jsonInputData, "POST_WITH_ID"); 43 52 return; 44 53 } 45 54 46 if ( jsonBody== null) {47 SendE nvelopedResult (_context, null, HttpStatusCode.BadRequest, null, "POST_WITHOUT_BODY");55 if (inputJson == null) { 56 SendErrorResult (_context, HttpStatusCode.BadRequest, null, "POST_WITHOUT_BODY"); 48 57 return; 49 58 } 50 59 51 HandleRestPost (_context, jsonBody);60 HandleRestPost (_context, inputJson, jsonInputData); 52 61 return; 53 62 case "PUT": 54 63 if (string.IsNullOrEmpty (_context.RequestPath)) { 55 SendE nvelopedResult (_context, null, HttpStatusCode.BadRequest, jsonBody, "PUT_WITHOUT_ID");64 SendErrorResult (_context, HttpStatusCode.BadRequest, jsonInputData, "PUT_WITHOUT_ID"); 56 65 return; 57 66 } 58 67 59 if ( jsonBody== null) {60 SendE nvelopedResult (_context, null, HttpStatusCode.BadRequest, null, "PUT_WITHOUT_BODY");68 if (inputJson == null) { 69 SendErrorResult (_context, HttpStatusCode.BadRequest, null, "PUT_WITHOUT_BODY"); 61 70 return; 62 71 } 63 72 64 HandleRestPut (_context, jsonBody);73 HandleRestPut (_context, inputJson, jsonInputData); 65 74 return; 66 75 case "DELETE": 67 76 if (string.IsNullOrEmpty (_context.RequestPath)) { 68 SendE nvelopedResult (_context, null, HttpStatusCode.BadRequest, jsonBody, "DELETE_WITHOUT_ID");77 SendErrorResult (_context, HttpStatusCode.BadRequest, jsonInputData, "DELETE_WITHOUT_ID"); 69 78 return; 70 79 } 71 80 72 if ( jsonBody!= null) {73 SendE nvelopedResult (_context, null, HttpStatusCode.BadRequest, null, "DELETE_WITH_BODY");81 if (inputJson != null) { 82 SendErrorResult (_context, HttpStatusCode.BadRequest, null, "DELETE_WITH_BODY"); 74 83 return; 75 84 } … … 78 87 return; 79 88 default: 80 SendE nvelopedResult (_context, null, HttpStatusCode.BadRequest, null, "INVALID_METHOD");89 SendErrorResult (_context, HttpStatusCode.BadRequest, null, "INVALID_METHOD"); 81 90 return; 82 91 } 83 92 } catch (Exception e) { 84 SendE nvelopedResult (_context, null, HttpStatusCode.InternalServerError, jsonBody, "ERROR_PROCESSING", e);93 SendErrorResult (_context, HttpStatusCode.InternalServerError, jsonInputData, "ERROR_PROCESSING", e); 85 94 } 86 95 } 87 96 88 private static readonly JsonArray emptyData = new JsonArray (); 89 90 protected void SendEnvelopedResult (RequestContext _context, JsonNode _resultData, HttpStatusCode _statusCode = HttpStatusCode.OK, 91 JsonNode _jsonInputBody = null, string _errorCode = null, Exception _exception = null) { 92 JsonObject meta = new JsonObject (); 93 94 meta.Add ("serverTime", new JsonString (DateTime.Now.ToString ("o"))); 95 if (!string.IsNullOrEmpty (_errorCode)) { 96 meta.Add ("requestMethod", new JsonString (_context.Request.HttpMethod)); 97 meta.Add ("requestSubpath", new JsonString (_context.RequestPath)); 98 meta.Add ("requestBody", new JsonString (_jsonInputBody?.ToString () ?? "-empty-")); 99 meta.Add ("errorCode", new JsonString (_errorCode)); 100 if (_exception != null) { 101 meta.Add ("exceptionMessage", new JsonString (_exception.Message)); 102 meta.Add ("exceptionTrace", new JsonString (_exception.StackTrace)); 103 } 104 } 105 106 JsonObject envelope = new JsonObject (); 107 envelope.Add ("meta", meta); 108 envelope.Add ("data", _resultData ?? emptyData); 109 110 WebUtils.WriteJson (_context.Response, envelope, _statusCode); 97 protected void SendErrorResult (RequestContext _context, HttpStatusCode _statusCode, byte[] _jsonInputData = null, string _errorCode = null, Exception _exception = null) { 98 PrepareEnvelopedResult (out JsonWriter writer); 99 writer.WriteRaw (JsonEmptyData); 100 SendEnvelopedResult (_context, ref writer, _statusCode, _jsonInputData, _errorCode, _exception); 111 101 } 112 102 113 protected bool TryGetJsonField (JsonObject _jsonObject, string _fieldName, out int _value) { 103 static AbsRestApi () { 104 JsonWriter writer = new JsonWriter (); 105 writer.WriteBeginArray (); 106 writer.WriteEndArray (); 107 JsonEmptyData = writer.ToUtf8ByteArray (); 108 } 109 110 protected static readonly byte[] JsonEmptyData; 111 112 protected void PrepareEnvelopedResult (out JsonWriter _writer) { 113 WebUtils.PrepareEnvelopedResult (out _writer); 114 } 115 116 protected void SendEnvelopedResult (RequestContext _context, ref JsonWriter _writer, HttpStatusCode _statusCode = HttpStatusCode.OK, 117 byte[] _jsonInputData = null, string _errorCode = null, Exception _exception = null) { 118 119 WebUtils.SendEnvelopedResult (_context, ref _writer, _statusCode, _jsonInputData, _errorCode, _exception); 120 } 121 122 protected bool TryGetJsonField (IDictionary<string, object> _jsonObject, string _fieldName, out int _value) { 114 123 _value = default; 115 124 116 if (!_jsonObject.TryGetValue (_fieldName, out JsonNodefieldNode)) {125 if (!_jsonObject.TryGetValue (_fieldName, out object fieldNode)) { 117 126 return false; 118 127 } 119 128 120 if ( !(fieldNode is JsonValue valueField)) {129 if (fieldNode is not double value) { 121 130 return false; 122 131 } 123 132 124 133 try { 125 _value = valueField.AsInt;134 _value = (int)value; 126 135 return true; 127 136 } catch (Exception) { … … 130 139 } 131 140 132 protected bool TryGetJsonField ( JsonObject_jsonObject, string _fieldName, out double _value) {141 protected bool TryGetJsonField (IDictionary<string, object> _jsonObject, string _fieldName, out double _value) { 133 142 _value = default; 134 143 135 if (!_jsonObject.TryGetValue (_fieldName, out JsonNodefieldNode)) {144 if (!_jsonObject.TryGetValue (_fieldName, out object fieldNode)) { 136 145 return false; 137 146 } 138 147 139 if ( !(fieldNode is JsonValue valueField)) {148 if (fieldNode is not double value) { 140 149 return false; 141 150 } 142 151 143 152 try { 144 _value = value Field.AsDouble;153 _value = value; 145 154 return true; 146 155 } catch (Exception) { … … 149 158 } 150 159 151 protected bool TryGetJsonField ( JsonObject_jsonObject, string _fieldName, out string _value) {160 protected bool TryGetJsonField (IDictionary<string, object> _jsonObject, string _fieldName, out string _value) { 152 161 _value = default; 153 162 154 if (!_jsonObject.TryGetValue (_fieldName, out JsonNodefieldNode)) {163 if (!_jsonObject.TryGetValue (_fieldName, out object fieldNode)) { 155 164 return false; 156 165 } 157 166 158 if ( !(fieldNode is JsonValue valueField)) {167 if (fieldNode is not string value) { 159 168 return false; 160 169 } 161 170 162 171 try { 163 _value = value Field.AsString;172 _value = value; 164 173 return true; 165 174 } catch (Exception) { … … 168 177 } 169 178 170 protected abstract void HandleRestGet (RequestContext _context); 179 protected virtual void HandleRestGet (RequestContext _context) { 180 SendErrorResult (_context, HttpStatusCode.MethodNotAllowed, null, "Unsupported"); 181 } 171 182 172 protected abstract void HandleRestPost (RequestContext _context, JsonNode _jsonBody); 183 protected virtual void HandleRestPost (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) { 184 SendErrorResult (_context, HttpStatusCode.MethodNotAllowed, _jsonInputData, "Unsupported"); 185 } 173 186 174 protected abstract void HandleRestPut (RequestContext _context, JsonNode _jsonBody); 187 protected virtual void HandleRestPut (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) { 188 SendErrorResult (_context, HttpStatusCode.MethodNotAllowed, _jsonInputData, "Unsupported"); 189 } 175 190 176 protected abstract void HandleRestDelete (RequestContext _context); 191 protected virtual void HandleRestDelete (RequestContext _context) { 192 SendErrorResult (_context, HttpStatusCode.MethodNotAllowed, null, "Unsupported"); 193 } 177 194 } 178 195 }
Note:
See TracChangeset
for help on using the changeset viewer.