Simplify reading a body of a request

This commit is contained in:
Slendy 2023-04-03 01:04:32 -05:00
parent d6788f0965
commit c6156d85cd
No known key found for this signature in database
GPG key ID: 7288D68361B91428
3 changed files with 7 additions and 29 deletions

View file

@ -33,7 +33,7 @@ public static partial class ControllerExtensions
// Separate method because Span/ReadOnlySpan cannot be used in async methods
ReadOnlySpan<byte> span = readOnlySequence.IsSingleSegment
? readOnlySequence.First.Span
: readOnlySequence.ToArray().AsSpan();
: readOnlySequence.ToArray();
builder.Append(Encoding.UTF8.GetString(span));
}
@ -47,32 +47,13 @@ public static partial class ControllerExtensions
ReadResult readResult = await controller.Request.BodyReader.ReadAsync();
ReadOnlySequence<byte> buffer = readResult.Buffer;
SequencePosition? position;
do
if (buffer.Length > 0)
{
// Look for a EOL in the buffer
position = buffer.PositionOf((byte)'\n');
if (position == null) continue;
ReadOnlySequence<byte> readOnlySequence = buffer.Slice(0, position.Value);
AddStringToBuilder(builder, in readOnlySequence);
// Skip the line + the \n character (basically position)
buffer = buffer.Slice(buffer.GetPosition(1, position.Value));
}
while (position != null);
if (readResult.IsCompleted && buffer.Length > 0)
{
AddStringToBuilder(builder, in buffer);
AddStringToBuilder(builder, buffer);
}
controller.Request.BodyReader.AdvanceTo(buffer.Start, buffer.End);
controller.Request.BodyReader.AdvanceTo(buffer.End);
// At this point, buffer will be updated to point one byte after the last
// \n character.
if (readResult.IsCompleted)
{
break;

View file

@ -32,7 +32,7 @@ public static class LighthouseSerializer
public static CustomXmlSerializer GetSerializer(Type type, XmlRootAttribute? rootAttribute = null)
{
if (serializerCache.TryGetValue((type, rootAttribute), out CustomXmlSerializer? value)) return value;
if (serializerCache.TryGetValue((type, rootAttribute), out CustomXmlSerializer? value)) return value;
CustomXmlSerializer serializer = new(type, rootAttribute);

View file

@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Xml.Serialization;
using LBPUnion.ProjectLighthouse.Serialization;
namespace LBPUnion.ProjectLighthouse.Types.Serialization;
@ -9,7 +8,7 @@ public struct GenericSlotResponse : ILbpSerializable, IHasCustomRoot
{
public GenericSlotResponse() { }
public GenericSlotResponse(string rootElement, List<SlotBase> slots, int total, int hintStart)
public GenericSlotResponse(string rootElement, List<SlotBase> slots, int total = 0, int hintStart = 0)
{
this.RootTag = rootElement;
this.Slots = slots;
@ -17,14 +16,12 @@ public struct GenericSlotResponse : ILbpSerializable, IHasCustomRoot
this.HintStart = hintStart;
}
public GenericSlotResponse(string rootElement, List<SlotBase> slots) : this(rootElement, slots, 0, 0) { }
public GenericSlotResponse(List<SlotBase> slots) : this("slots", slots) { }
public GenericSlotResponse(List<SlotBase> slots, int total, int hintStart) : this("slots", slots, total, hintStart) { }
[XmlIgnore]
public string RootTag { get; set; }
private string RootTag { get; }
[XmlElement("slot")]
public List<SlotBase> Slots { get; set; }