| [391] | 1 | using System;
 | 
|---|
| [404] | 2 | using System.Collections.Specialized;
 | 
|---|
| [402] | 3 | using System.Diagnostics.CodeAnalysis;
 | 
|---|
| [391] | 4 | using System.Net;
 | 
|---|
 | 5 | using System.Text;
 | 
|---|
| [402] | 6 | using Utf8Json;
 | 
|---|
| [425] | 7 | using Webserver.WebAPI;
 | 
|---|
| [391] | 8 | using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;
 | 
|---|
 | 9 | using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;
 | 
|---|
 | 10 | 
 | 
|---|
 | 11 | namespace Webserver {
 | 
|---|
 | 12 |         public static class WebUtils {
 | 
|---|
| [402] | 13 |                 // ReSharper disable once MemberCanBePrivate.Global
 | 
|---|
| [391] | 14 |                 public const string MimePlain = "text/plain";
 | 
|---|
 | 15 |                 public const string MimeHtml = "text/html";
 | 
|---|
| [402] | 16 |                 // ReSharper disable once MemberCanBePrivate.Global
 | 
|---|
| [391] | 17 |                 public const string MimeJson = "application/json";
 | 
|---|
 | 18 |                 
 | 
|---|
| [402] | 19 |                 private static readonly UnityEngine.Profiling.CustomSampler envelopeBuildSampler = UnityEngine.Profiling.CustomSampler.Create ("JSON_EnvelopeBuilding");
 | 
|---|
| [391] | 20 |                 private static readonly UnityEngine.Profiling.CustomSampler netWriteSampler = UnityEngine.Profiling.CustomSampler.Create ("JSON_Write");
 | 
|---|
 | 21 | 
 | 
|---|
 | 22 |                 public static void WriteText (HttpListenerResponse _resp, string _text, HttpStatusCode _statusCode = HttpStatusCode.OK, string _mimeType = null) {
 | 
|---|
 | 23 |                         _resp.StatusCode = (int)_statusCode;
 | 
|---|
 | 24 |                         _resp.ContentType = _mimeType ?? MimePlain;
 | 
|---|
 | 25 |                         _resp.ContentEncoding = Encoding.UTF8;
 | 
|---|
 | 26 | 
 | 
|---|
| [422] | 27 |                         if (string.IsNullOrEmpty (_text)) {
 | 
|---|
 | 28 |                                 _resp.ContentLength64 = 0;
 | 
|---|
 | 29 |                         } else {
 | 
|---|
 | 30 |                                 byte[] buf = Encoding.UTF8.GetBytes (_text);
 | 
|---|
 | 31 |                                 _resp.ContentLength64 = buf.Length;
 | 
|---|
 | 32 |                                 _resp.OutputStream.Write (buf, 0, buf.Length);
 | 
|---|
 | 33 |                         }
 | 
|---|
| [391] | 34 |                 }
 | 
|---|
 | 35 | 
 | 
|---|
 | 36 |                 public static bool IsSslRedirected (HttpListenerRequest _req) {
 | 
|---|
 | 37 |                         string proto = _req.Headers ["X-Forwarded-Proto"];
 | 
|---|
 | 38 |                         return !string.IsNullOrEmpty (proto) && proto.Equals ("https", StringComparison.OrdinalIgnoreCase);
 | 
|---|
 | 39 |                 }
 | 
|---|
 | 40 |                 
 | 
|---|
 | 41 |                 public static string GenerateGuid () {
 | 
|---|
 | 42 |                         return Guid.NewGuid ().ToString ();
 | 
|---|
 | 43 |                 }
 | 
|---|
| [402] | 44 | 
 | 
|---|
 | 45 |                 [SuppressMessage ("ReSharper", "MemberCanBePrivate.Global")]
 | 
|---|
 | 46 |                 public static void WriteJsonData (HttpListenerResponse _resp, ref JsonWriter _jsonWriter, HttpStatusCode _statusCode = HttpStatusCode.OK) {
 | 
|---|
 | 47 |                         ArraySegment<byte> jsonData = _jsonWriter.GetBuffer ();
 | 
|---|
 | 48 | 
 | 
|---|
 | 49 |                         netWriteSampler.Begin ();
 | 
|---|
 | 50 |                         _resp.StatusCode = (int)_statusCode;
 | 
|---|
 | 51 |                         _resp.ContentType = MimeJson;
 | 
|---|
 | 52 |                         _resp.ContentEncoding = Encoding.UTF8;
 | 
|---|
 | 53 |                         _resp.ContentLength64 = jsonData.Count;
 | 
|---|
 | 54 |                         _resp.OutputStream.Write (jsonData.Array!, 0, jsonData.Count);
 | 
|---|
 | 55 |                         netWriteSampler.End ();
 | 
|---|
 | 56 |                 }
 | 
|---|
 | 57 |                 
 | 
|---|
 | 58 |                 
 | 
|---|
 | 59 |                 private static readonly byte[] jsonRawDataKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("data"); // {"data":
 | 
|---|
 | 60 | 
 | 
|---|
 | 61 |                 private static readonly byte[] jsonRawMetaKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("meta"); // ,"meta":
 | 
|---|
 | 62 |                 private static readonly byte[] jsonRawMetaServertimeKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("serverTime"); // {"serverTime":
 | 
|---|
 | 63 |                 private static readonly byte[] jsonRawMetaRequestMethodKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("requestMethod"); // ,"requestMethod":
 | 
|---|
 | 64 |                 private static readonly byte[] jsonRawMetaRequestSubpathKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("requestSubpath"); // ,"requestSubpath":
 | 
|---|
 | 65 |                 private static readonly byte[] jsonRawMetaRequestBodyKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("requestBody"); // ,"requestBody":
 | 
|---|
 | 66 |                 private static readonly byte[] jsonRawMetaErrorCodeKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("errorCode"); // ,"errorCode":
 | 
|---|
 | 67 |                 private static readonly byte[] jsonRawMetaExceptionMessageKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("exceptionMessage"); // ,"exceptionMessage":
 | 
|---|
 | 68 |                 private static readonly byte[] jsonRawMetaExceptionTraceKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("exceptionTrace"); // ,"exceptionTrace":
 | 
|---|
 | 69 |                 
 | 
|---|
 | 70 |                 public static void PrepareEnvelopedResult (out JsonWriter _writer) {
 | 
|---|
 | 71 |                         _writer = new JsonWriter ();
 | 
|---|
 | 72 |                         _writer.WriteRaw (jsonRawDataKey);
 | 
|---|
 | 73 |                 }
 | 
|---|
 | 74 | 
 | 
|---|
 | 75 |                 public static void SendEnvelopedResult (RequestContext _context, ref JsonWriter _writer, HttpStatusCode _statusCode = HttpStatusCode.OK,
 | 
|---|
 | 76 |                         byte[] _jsonInputData = null, string _errorCode = null, Exception _exception = null) {
 | 
|---|
 | 77 |                         
 | 
|---|
 | 78 |                         envelopeBuildSampler.Begin ();
 | 
|---|
 | 79 |                         
 | 
|---|
 | 80 |                         _writer.WriteRaw (jsonRawMetaKey);
 | 
|---|
 | 81 | 
 | 
|---|
 | 82 |                         _writer.WriteRaw (jsonRawMetaServertimeKey);
 | 
|---|
| [425] | 83 |                         JsonCommons.WriteDateTime (ref _writer, DateTime.Now);
 | 
|---|
| [402] | 84 | 
 | 
|---|
 | 85 |                         if (!string.IsNullOrEmpty (_errorCode)) {
 | 
|---|
 | 86 |                                 _writer.WriteRaw (jsonRawMetaRequestMethodKey);
 | 
|---|
 | 87 |                                 _writer.WriteString (_context.Request.HttpMethod);
 | 
|---|
 | 88 | 
 | 
|---|
 | 89 |                                 _writer.WriteRaw (jsonRawMetaRequestSubpathKey);
 | 
|---|
 | 90 |                                 _writer.WriteString (_context.RequestPath);
 | 
|---|
 | 91 | 
 | 
|---|
 | 92 |                                 _writer.WriteRaw (jsonRawMetaRequestBodyKey);
 | 
|---|
 | 93 |                                 if (_jsonInputData != null) {
 | 
|---|
 | 94 |                                         _writer.WriteRaw (_jsonInputData);
 | 
|---|
 | 95 |                                 } else {
 | 
|---|
 | 96 |                                         _writer.WriteNull ();
 | 
|---|
 | 97 |                                 }
 | 
|---|
 | 98 | 
 | 
|---|
 | 99 |                                 _writer.WriteRaw (jsonRawMetaErrorCodeKey);
 | 
|---|
 | 100 |                                 _writer.WriteString (_errorCode);
 | 
|---|
 | 101 | 
 | 
|---|
 | 102 |                                 if (_exception != null) {
 | 
|---|
 | 103 |                                         _writer.WriteRaw (jsonRawMetaExceptionMessageKey);
 | 
|---|
 | 104 |                                         _writer.WriteString (_exception.Message);
 | 
|---|
 | 105 | 
 | 
|---|
 | 106 |                                         _writer.WriteRaw (jsonRawMetaExceptionTraceKey);
 | 
|---|
 | 107 |                                         _writer.WriteString (_exception.StackTrace);
 | 
|---|
 | 108 |                                 }
 | 
|---|
 | 109 |                         }
 | 
|---|
 | 110 |                         
 | 
|---|
 | 111 |                         _writer.WriteEndObject (); // End of "meta" object
 | 
|---|
 | 112 |                         _writer.WriteEndObject (); // End of overall result object
 | 
|---|
 | 113 |                         
 | 
|---|
 | 114 |                         envelopeBuildSampler.End ();
 | 
|---|
 | 115 | 
 | 
|---|
 | 116 |                         WriteJsonData (_context.Response, ref _writer, _statusCode);
 | 
|---|
 | 117 |                 }
 | 
|---|
| [404] | 118 | 
 | 
|---|
 | 119 |                 public static bool TryGetValue (this NameValueCollection _nameValueCollection, string _name, out string _result) {
 | 
|---|
 | 120 |                         _result = _nameValueCollection [_name];
 | 
|---|
 | 121 |                         return _result != null;
 | 
|---|
 | 122 |                 }
 | 
|---|
| [391] | 123 |         }
 | 
|---|
 | 124 | }
 | 
|---|