Changeset 402 for binary-improvements2/WebServer/src/WebUtils.cs
- Timestamp:
- Jan 27, 2023, 7:28:00 PM (22 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/WebServer/src/WebUtils.cs
r394 r402 1 1 using System; 2 using System.Diagnostics.CodeAnalysis; 2 3 using System.Net; 3 4 using System.Text; 4 using AllocsFixes.JSON;5 using Utf8Json; 5 6 using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest; 6 7 using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse; … … 8 9 namespace Webserver { 9 10 public static class WebUtils { 11 // ReSharper disable once MemberCanBePrivate.Global 10 12 public const string MimePlain = "text/plain"; 11 13 public const string MimeHtml = "text/html"; 14 // ReSharper disable once MemberCanBePrivate.Global 12 15 public const string MimeJson = "application/json"; 13 16 14 private static readonly UnityEngine.Profiling.CustomSampler jsonSerializeSampler = UnityEngine.Profiling.CustomSampler.Create ("JSON_Serialize");17 private static readonly UnityEngine.Profiling.CustomSampler envelopeBuildSampler = UnityEngine.Profiling.CustomSampler.Create ("JSON_EnvelopeBuilding"); 15 18 private static readonly UnityEngine.Profiling.CustomSampler netWriteSampler = UnityEngine.Profiling.CustomSampler.Create ("JSON_Write"); 16 17 public static void WriteJson (HttpListenerResponse _resp, JsonNode _root, HttpStatusCode _statusCode = HttpStatusCode.OK) {18 jsonSerializeSampler.Begin ();19 StringBuilder sb = new StringBuilder ();20 _root.ToString (sb);21 jsonSerializeSampler.End ();22 netWriteSampler.Begin ();23 WriteText (_resp, sb.ToString(), _statusCode, MimeJson);24 netWriteSampler.End ();25 }26 19 27 20 public static void WriteText (HttpListenerResponse _resp, string _text, HttpStatusCode _statusCode = HttpStatusCode.OK, string _mimeType = null) { … … 43 36 return Guid.NewGuid ().ToString (); 44 37 } 38 39 [SuppressMessage ("ReSharper", "MemberCanBePrivate.Global")] 40 public static void WriteJsonData (HttpListenerResponse _resp, ref JsonWriter _jsonWriter, HttpStatusCode _statusCode = HttpStatusCode.OK) { 41 ArraySegment<byte> jsonData = _jsonWriter.GetBuffer (); 42 43 netWriteSampler.Begin (); 44 _resp.StatusCode = (int)_statusCode; 45 _resp.ContentType = MimeJson; 46 _resp.ContentEncoding = Encoding.UTF8; 47 _resp.ContentLength64 = jsonData.Count; 48 _resp.OutputStream.Write (jsonData.Array!, 0, jsonData.Count); 49 netWriteSampler.End (); 50 } 51 52 53 private static readonly byte[] jsonRawDataKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("data"); // {"data": 54 55 private static readonly byte[] jsonRawMetaKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("meta"); // ,"meta": 56 private static readonly byte[] jsonRawMetaServertimeKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("serverTime"); // {"serverTime": 57 private static readonly byte[] jsonRawMetaRequestMethodKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("requestMethod"); // ,"requestMethod": 58 private static readonly byte[] jsonRawMetaRequestSubpathKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("requestSubpath"); // ,"requestSubpath": 59 private static readonly byte[] jsonRawMetaRequestBodyKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("requestBody"); // ,"requestBody": 60 private static readonly byte[] jsonRawMetaErrorCodeKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("errorCode"); // ,"errorCode": 61 private static readonly byte[] jsonRawMetaExceptionMessageKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("exceptionMessage"); // ,"exceptionMessage": 62 private static readonly byte[] jsonRawMetaExceptionTraceKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("exceptionTrace"); // ,"exceptionTrace": 63 64 public static void PrepareEnvelopedResult (out JsonWriter _writer) { 65 _writer = new JsonWriter (); 66 _writer.WriteRaw (jsonRawDataKey); 67 } 68 69 public static void SendEnvelopedResult (RequestContext _context, ref JsonWriter _writer, HttpStatusCode _statusCode = HttpStatusCode.OK, 70 byte[] _jsonInputData = null, string _errorCode = null, Exception _exception = null) { 71 72 envelopeBuildSampler.Begin (); 73 74 _writer.WriteRaw (jsonRawMetaKey); 75 76 _writer.WriteRaw (jsonRawMetaServertimeKey); 77 _writer.WriteString (DateTime.Now.ToString ("o")); 78 79 if (!string.IsNullOrEmpty (_errorCode)) { 80 _writer.WriteRaw (jsonRawMetaRequestMethodKey); 81 _writer.WriteString (_context.Request.HttpMethod); 82 83 _writer.WriteRaw (jsonRawMetaRequestSubpathKey); 84 _writer.WriteString (_context.RequestPath); 85 86 _writer.WriteRaw (jsonRawMetaRequestBodyKey); 87 if (_jsonInputData != null) { 88 _writer.WriteRaw (_jsonInputData); 89 } else { 90 _writer.WriteNull (); 91 } 92 93 _writer.WriteRaw (jsonRawMetaErrorCodeKey); 94 _writer.WriteString (_errorCode); 95 96 if (_exception != null) { 97 _writer.WriteRaw (jsonRawMetaExceptionMessageKey); 98 _writer.WriteString (_exception.Message); 99 100 _writer.WriteRaw (jsonRawMetaExceptionTraceKey); 101 _writer.WriteString (_exception.StackTrace); 102 } 103 } 104 105 _writer.WriteEndObject (); // End of "meta" object 106 _writer.WriteEndObject (); // End of overall result object 107 108 envelopeBuildSampler.End (); 109 110 WriteJsonData (_context.Response, ref _writer, _statusCode); 111 } 45 112 } 46 113 }
Note:
See TracChangeset
for help on using the changeset viewer.