source: TFP-WebServer/WebServer/src/WebAPI/APIs/LogApi.cs@ 477

Last change on this file since 477 was 471, checked in by alloc, 15 months ago

Fixed: Possibly multiple parsing of query string

File size: 3.2 KB
RevLine 
[391]1using System.Collections.Generic;
2using JetBrains.Annotations;
[402]3using Utf8Json;
[391]4
[402]5namespace Webserver.WebAPI.APIs {
[391]6 [UsedImplicitly]
[459]7 public class LogApi : AbsRestApi {
[402]8 private const int maxCount = 1000;
9
10 private static readonly byte[] jsonKeyEntries = JsonWriter.GetEncodedPropertyNameWithBeginObject ("entries");
11 private static readonly byte[] jsonKeyFirstLine = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("firstLine");
12 private static readonly byte[] jsonKeyLastLine = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("lastLine");
13
[467]14 private static readonly byte[] jsonIdKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("id");
15 private static readonly byte[] jsonMsgKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("msg");
[402]16 private static readonly byte[] jsonTypeKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("type");
17 private static readonly byte[] jsonTraceKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("trace");
18 private static readonly byte[] jsonIsotimeKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("isotime");
19 private static readonly byte[] jsonUptimeKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("uptime");
[459]20
21 public LogApi () : base ("Log") {
22 }
23
[402]24 protected override void HandleRestGet (RequestContext _context) {
[471]25 if (_context.QueryParameters ["count"] == null || !int.TryParse (_context.QueryParameters ["count"], out int count)) {
[391]26 count = 50;
27 }
28
29 if (count == 0) {
30 count = 1;
31 }
32
[402]33 if (count > maxCount) {
34 count = maxCount;
[391]35 }
36
[402]37 if (count < -maxCount) {
38 count = -maxCount;
[391]39 }
40
[471]41 if (_context.QueryParameters ["firstLine"] == null || !int.TryParse (_context.QueryParameters ["firstLine"], out int firstLine)) {
[391]42 firstLine = count > 0 ? LogBuffer.Instance.OldestLine : LogBuffer.Instance.LatestLine;
43 }
[402]44
45 PrepareEnvelopedResult (out JsonWriter writer);
46
47 writer.WriteRaw (jsonKeyEntries);
48
49 List<LogBuffer.LogEntry> logEntries = LogBuffer.Instance.GetRange (ref firstLine, count, out int lastLine);
[391]50
[402]51 writer.WriteBeginArray ();
[391]52
[408]53 for (int i = 0; i < logEntries.Count; i++) {
54 LogBuffer.LogEntry logEntry = logEntries [i];
55
56 if (i > 0) {
[402]57 writer.WriteValueSeparator ();
58 }
[391]59
[467]60 WriteLogMessageObject (ref writer, logEntry);
[391]61 }
[408]62
[402]63 writer.WriteEndArray ();
[391]64
[402]65 writer.WriteRaw (jsonKeyFirstLine);
66 writer.WriteInt32 (firstLine);
67
68 writer.WriteRaw (jsonKeyLastLine);
69 writer.WriteInt32 (lastLine);
70
71 writer.WriteEndObject ();
[391]72
[402]73 SendEnvelopedResult (_context, ref writer);
[391]74 }
[467]75
76 public static void WriteLogMessageObject (ref JsonWriter _writer, LogBuffer.LogEntry _logEntry) {
77 _writer.WriteRaw (jsonIdKey);
78 _writer.WriteInt32 (_logEntry.MessageId);
79
80 _writer.WriteRaw (jsonMsgKey);
81 _writer.WriteString (_logEntry.Message);
82
83 _writer.WriteRaw (jsonTypeKey);
84 _writer.WriteString (_logEntry.Type.ToStringCached ());
85
86 _writer.WriteRaw (jsonTraceKey);
87 _writer.WriteString (_logEntry.Trace);
88
89 _writer.WriteRaw (jsonIsotimeKey);
90 _writer.WriteString (_logEntry.IsoTime);
91
92 _writer.WriteRaw (jsonUptimeKey);
93 _writer.WriteString (_logEntry.Uptime.ToString ());
94
95 _writer.WriteEndObject ();
96 }
[391]97 }
98}
Note: See TracBrowser for help on using the repository browser.