mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-06-16 20: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
|
// Separate method because Span/ReadOnlySpan cannot be used in async methods
|
||||||
ReadOnlySpan<byte> span = readOnlySequence.IsSingleSegment
|
ReadOnlySpan<byte> span = readOnlySequence.IsSingleSegment
|
||||||
? readOnlySequence.First.Span
|
? readOnlySequence.First.Span
|
||||||
: readOnlySequence.ToArray().AsSpan();
|
: readOnlySequence.ToArray();
|
||||||
builder.Append(Encoding.UTF8.GetString(span));
|
builder.Append(Encoding.UTF8.GetString(span));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,32 +47,13 @@ public static partial class ControllerExtensions
|
||||||
ReadResult readResult = await controller.Request.BodyReader.ReadAsync();
|
ReadResult readResult = await controller.Request.BodyReader.ReadAsync();
|
||||||
ReadOnlySequence<byte> buffer = readResult.Buffer;
|
ReadOnlySequence<byte> buffer = readResult.Buffer;
|
||||||
|
|
||||||
SequencePosition? position;
|
if (buffer.Length > 0)
|
||||||
|
|
||||||
do
|
|
||||||
{
|
{
|
||||||
// Look for a EOL in the buffer
|
AddStringToBuilder(builder, 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
if (readResult.IsCompleted)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using LBPUnion.ProjectLighthouse.Serialization;
|
|
||||||
|
|
||||||
namespace LBPUnion.ProjectLighthouse.Types.Serialization;
|
namespace LBPUnion.ProjectLighthouse.Types.Serialization;
|
||||||
|
|
||||||
|
@ -9,7 +8,7 @@ public struct GenericSlotResponse : ILbpSerializable, IHasCustomRoot
|
||||||
{
|
{
|
||||||
public GenericSlotResponse() { }
|
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.RootTag = rootElement;
|
||||||
this.Slots = slots;
|
this.Slots = slots;
|
||||||
|
@ -17,14 +16,12 @@ public struct GenericSlotResponse : ILbpSerializable, IHasCustomRoot
|
||||||
this.HintStart = hintStart;
|
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) : this("slots", slots) { }
|
||||||
|
|
||||||
public GenericSlotResponse(List<SlotBase> slots, int total, int hintStart) : this("slots", slots, total, hintStart) { }
|
public GenericSlotResponse(List<SlotBase> slots, int total, int hintStart) : this("slots", slots, total, hintStart) { }
|
||||||
|
|
||||||
[XmlIgnore]
|
[XmlIgnore]
|
||||||
public string RootTag { get; set; }
|
private string RootTag { get; }
|
||||||
|
|
||||||
[XmlElement("slot")]
|
[XmlElement("slot")]
|
||||||
public List<SlotBase> Slots { get; set; }
|
public List<SlotBase> Slots { get; set; }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue