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

Last change on this file since 423 was 422, checked in by alloc, 20 months ago

Added release files to version control

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