source: TFP-WebServer/WebServer/src/WebCommandResult.cs@ 487

Last change on this file since 487 was 487, checked in by alloc, 5 months ago

1.1.0.1 Release for V 1.0

File size: 3.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Net.Sockets;
5using System.Text;
6using UnityEngine;
7using Utf8Json;
8
9namespace Webserver {
10 public class WebCommandResult : IConsoleConnection {
11 public enum ResultType {
12 Full,
13 ResultOnly,
14 Raw
15 }
16
17 private readonly string command;
18 private readonly string parameters;
19 private readonly string sourceName;
20
21 private readonly RequestContext context;
22 private readonly ResultType responseType;
23
24 public WebCommandResult (string _command, string _parameters, ResultType _responseType, RequestContext _context) {
25 context = _context;
26 command = _command;
27 parameters = _parameters;
28 responseType = _responseType;
29 sourceName = _context.Connection?.Username ?? "Unauth-PermLevel-" + _context.PermissionLevel;
30 }
31
32 private static readonly byte[] jsonRawKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("resultRaw");
33
34 private static readonly byte[] jsonCommandKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("command");
35 private static readonly byte[] jsonParametersKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("parameters");
36 private static readonly byte[] jsonResultKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("result");
37
38 public void SendLines (List<string> _output) {
39 StringBuilder sb = new StringBuilder ();
40 foreach (string line in _output) {
41 sb.AppendLine (line);
42 }
43
44 string commandOutput = sb.ToString ();
45
46 try {
47 if (responseType == ResultType.Raw) {
48 WebUtils.WriteText (context.Response, commandOutput);
49 } else {
50 WebUtils.PrepareEnvelopedResult (out JsonWriter writer);
51
52 if (responseType == ResultType.ResultOnly) {
53 writer.WriteRaw (jsonRawKey);
54 writer.WriteString (commandOutput);
55 writer.WriteEndObject ();
56 } else {
57 writer.WriteRaw (jsonCommandKey);
58 writer.WriteString (command);
59
60 writer.WriteRaw (jsonParametersKey);
61 writer.WriteString (parameters);
62
63 writer.WriteRaw (jsonResultKey);
64 writer.WriteString (commandOutput);
65
66 writer.WriteEndObject ();
67 }
68
69 WebUtils.SendEnvelopedResult (context, ref writer);
70 }
71 } catch (IOException e) {
72 if (e.InnerException is SocketException) {
73 Log.Warning ($"[Web] Error in WebCommandResult.SendLines(): Remote host closed connection: {e.InnerException.Message}");
74 } else {
75 Log.Warning ($"[Web] Error (IO) in WebCommandResult.SendLines(): {e}");
76 }
77 } catch (Exception e) {
78 Log.Warning ($"[Web] Error in WebCommandResult.SendLines(): {e}");
79 } finally {
80 context?.Response?.Close ();
81 }
82 }
83
84 public void SendLine (string _text) {
85 //throw new NotImplementedException ();
86 }
87
88 public void SendLog (string _formattedMessage, string _plainMessage, string _trace, LogType _type, DateTime _timestamp, long _uptime) {
89 //throw new NotImplementedException ();
90 }
91
92 public void EnableLogLevel (LogType _type, bool _enable) {
93 //throw new NotImplementedException ();
94 }
95
96 public string GetDescription () {
97 return $"WebCommandResult_for_{command}_by_{sourceName}";
98 }
99 }
100}
Note: See TracBrowser for help on using the repository browser.