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
RevLine 
[391]1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Net.Sockets;
5using System.Text;
6using UnityEngine;
[402]7using Utf8Json;
[391]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;
[487]19 private readonly string sourceName;
[391]20
[402]21 private readonly RequestContext context;
[391]22 private readonly ResultType responseType;
23
[402]24 public WebCommandResult (string _command, string _parameters, ResultType _responseType, RequestContext _context) {
25 context = _context;
[391]26 command = _command;
27 parameters = _parameters;
28 responseType = _responseType;
[487]29 sourceName = _context.Connection?.Username ?? "Unauth-PermLevel-" + _context.PermissionLevel;
[391]30 }
31
[402]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
[391]38 public void SendLines (List<string> _output) {
39 StringBuilder sb = new StringBuilder ();
40 foreach (string line in _output) {
41 sb.AppendLine (line);
42 }
43
[402]44 string commandOutput = sb.ToString ();
45
[391]46 try {
47 if (responseType == ResultType.Raw) {
[402]48 WebUtils.WriteText (context.Response, commandOutput);
[391]49 } else {
[402]50 WebUtils.PrepareEnvelopedResult (out JsonWriter writer);
51
[391]52 if (responseType == ResultType.ResultOnly) {
[402]53 writer.WriteRaw (jsonRawKey);
54 writer.WriteString (commandOutput);
55 writer.WriteEndObject ();
[391]56 } else {
[402]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 ();
[391]67 }
68
[402]69 WebUtils.SendEnvelopedResult (context, ref writer);
[391]70 }
71 } catch (IOException e) {
72 if (e.InnerException is SocketException) {
[402]73 Log.Warning ($"[Web] Error in WebCommandResult.SendLines(): Remote host closed connection: {e.InnerException.Message}");
[391]74 } else {
[402]75 Log.Warning ($"[Web] Error (IO) in WebCommandResult.SendLines(): {e}");
[391]76 }
77 } catch (Exception e) {
[402]78 Log.Warning ($"[Web] Error in WebCommandResult.SendLines(): {e}");
[391]79 } finally {
[402]80 context?.Response?.Close ();
[391]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 () {
[487]97 return $"WebCommandResult_for_{command}_by_{sourceName}";
[391]98 }
99 }
100}
Note: See TracBrowser for help on using the repository browser.