mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-06-10 01:51:27 +00:00
Simplify reading a body of a request
This commit is contained in:
parent
d6788f0965
commit
c6156d85cd
3 changed files with 7 additions and 29 deletions
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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; }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue