source: binary-improvements2/WebServer/src/WebUtils.cs@ 415

Last change on this file since 415 was 404, checked in by alloc, 21 months ago

Latest state including reworking to the permissions system

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