source: binary-improvements2/MapRendering/Web/API/WebAPI.cs@ 385

Last change on this file since 385 was 382, checked in by alloc, 3 years ago

Switched to use SpaceWizards.HttpListener

File size: 1.8 KB
RevLine 
[230]1using System.Text;
[325]2using AllocsFixes.JSON;
[382]3using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;
4using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;
[230]5
[325]6namespace AllocsFixes.NetConnections.Servers.Web.API {
7 public abstract class WebAPI {
[326]8 public readonly string Name;
9
[367]10 protected WebAPI (string _name = null) {
11 Name = _name ?? GetType ().Name;
[326]12 }
13
[332]14#if ENABLE_PROFILER
[367]15 private static readonly UnityEngine.Profiling.CustomSampler jsonSerializeSampler = UnityEngine.Profiling.CustomSampler.Create ("JSON_Serialize");
16 private static readonly UnityEngine.Profiling.CustomSampler netWriteSampler = UnityEngine.Profiling.CustomSampler.Create ("JSON_Write");
[332]17#endif
18
[351]19 public static void WriteJSON (HttpListenerResponse _resp, JSONNode _root) {
[332]20#if ENABLE_PROFILER
21 jsonSerializeSampler.Begin ();
22#endif
[309]23 StringBuilder sb = new StringBuilder ();
[351]24 _root.ToString (sb);
[332]25#if ENABLE_PROFILER
26 jsonSerializeSampler.End ();
27 netWriteSampler.Begin ();
28#endif
[309]29 byte[] buf = Encoding.UTF8.GetBytes (sb.ToString ());
[351]30 _resp.ContentLength64 = buf.Length;
31 _resp.ContentType = "application/json";
32 _resp.ContentEncoding = Encoding.UTF8;
33 _resp.OutputStream.Write (buf, 0, buf.Length);
[332]34#if ENABLE_PROFILER
35 netWriteSampler.End ();
36#endif
[230]37 }
38
[325]39 public static void WriteText (HttpListenerResponse _resp, string _text) {
[306]40 byte[] buf = Encoding.UTF8.GetBytes (_text);
41 _resp.ContentLength64 = buf.Length;
42 _resp.ContentType = "text/plain";
43 _resp.ContentEncoding = Encoding.UTF8;
44 _resp.OutputStream.Write (buf, 0, buf.Length);
45 }
46
[382]47 public abstract void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp,
48 WebConnection _user, int _permissionLevel);
[279]49
50 public virtual int DefaultPermissionLevel () {
51 return 0;
52 }
[230]53 }
[325]54}
Note: See TracBrowser for help on using the repository browser.