Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

updated some more APIs that AzCopyCore sample depends on #2119

Merged
merged 1 commit into from
Feb 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public struct StorageAuthorizationHeader : IWritable
private static TransformationFormat s_removeCR = new TransformationFormat(new RemoveTransformation(13));

public Sha256 Hash;
public string HttpVerb;
public ReadOnlyMemory<byte> HttpVerb;
public string AccountName;
public string CanonicalizedResource;
public WritableBytes CanonicalizedHeaders;
Expand All @@ -34,15 +34,15 @@ public bool TryWrite(Span<byte> buffer, out int written, StandardFormat format =

int signatureStart = writer.WrittenCount;

writer.Write(HttpVerb);
writer.WriteBytes(HttpVerb);
if (ContentLength == 0)
{
writer.Write("\n\n\n\n\n\n\n\n\n\n\n\n");
}
else
{
writer.Write("\n\n\n");
writer.Write(ContentLength.ToString());
writer.Write(ContentLength);
writer.Write("\n\n\n\n\n\n\n\n\n");
}
writer.WriteBytes(CanonicalizedHeaders, s_removeCR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,17 @@ public void WriteLine(long value, StandardFormat format = default)
{
while (!TryWriteLine(value, format)) Resize();
}

public void Write(long value, StandardFormat format = default)
{
while (!TryWrite(value, format)) Resize();
}

public bool TryWrite(long value, StandardFormat format = default)
{
if (!Utf8Formatter.TryFormat(value, Free, out int written, format)) return false;
_written += written;
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void WriteBytes(ReadOnlyMemory<byte> bytes)
#endregion

#region IWritable
public bool TryWriteBytes<T>(T value, StandardFormat format) where T : IWritable
public bool TryWriteBytes<T>(T value, StandardFormat format = default) where T : IWritable
{
if (!value.TryWrite(Free, out int written, format))
{
Expand All @@ -48,7 +48,7 @@ public bool TryWriteBytes<T>(T value, StandardFormat format) where T : IWritable
return true;
}

public void WriteBytes<T>(T value, StandardFormat format) where T : IWritable
public void WriteBytes<T>(T value, StandardFormat format = default) where T : IWritable
{
while (!TryWriteBytes(value, format)) Resize();
}
Expand Down Expand Up @@ -76,7 +76,7 @@ public void WriteBytes<T>(T value, TransformationFormat format) where T : IWrita
#endregion

#region IBufferFormattable
public bool TryWrite<T>(T value, StandardFormat format) where T : IBufferFormattable
public bool TryWrite<T>(T value, StandardFormat format = default) where T : IBufferFormattable
{
if (value.TryFormat(Free, out int written, format, SymbolTable.InvariantUtf8))
{
Expand All @@ -86,7 +86,7 @@ public bool TryWrite<T>(T value, StandardFormat format) where T : IBufferFormatt
return false;
}

public void Write<T>(T value, StandardFormat format) where T : IBufferFormattable
public void Write<T>(T value, StandardFormat format = default) where T : IBufferFormattable
{
while (!TryWrite(value, format)) Resize();
}
Expand Down
64 changes: 53 additions & 11 deletions src/System.Text.Http.Parser/HttpParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ public class HttpParser : IHttpParser
private const byte ByteTab = (byte)'\t';
private const byte ByteQuestionMark = (byte)'?';
private const byte BytePercentage = (byte)'%';
private const long maxRequestLineLength = 1024;

static readonly byte[] s_Eol = Encoding.UTF8.GetBytes("\r\n");
static readonly byte[] s_http11 = Encoding.UTF8.GetBytes("HTTP/1.1");
static readonly byte[] s_http10 = Encoding.UTF8.GetBytes("HTTP/1.0");
static readonly byte[] s_http20 = Encoding.UTF8.GetBytes("HTTP/2.0");

private readonly bool _showErrorDetails;

Expand Down Expand Up @@ -476,24 +481,61 @@ public unsafe bool ParseHeaders<T>(ref T handler, ReadOnlySequence<byte> buffer,
}
}

public bool ParseResponseLine<T>(ref T handler, ref ReadOnlySequence<byte> buffer, out int consumedBytes) where T : IHttpResponseLineHandler
public static bool ParseResponseLine<T>(ref T handler, ref ReadOnlySequence<byte> buffer, out int consumedBytes) where T : IHttpResponseLineHandler
{
var first = buffer.First.Span;
var eol = first.IndexOf(s_Eol);
if (eol == -1)
var line = buffer.First.Span;
var lf = line.IndexOf(ByteLF);
if (lf >= 0)
{
line = line.Slice(0, lf + 1);
}
else if (buffer.IsSingleSegment)
{
consumedBytes = 0;
return false;
}
else
{
long index = Sequence.IndexOf(buffer, ByteLF);
if (index < 0)
{
consumedBytes = 0;
return false;
}
if (index > maxRequestLineLength)
{
throw new Exception("invalid response (LF too far)");
}
line = line.Slice(0, lf + 1);
}

if (line[lf - 1] != ByteCR)
{
throw new NotImplementedException();
throw new Exception("invalid response (no CR)");
}
first = first.Slice(0, eol);
int codeStart = first.IndexOf((byte)' ') + 1;
var codeSlice = first.Slice(codeStart);

Http.Version version;
if (line.StartsWith(s_http11)) { version = Http.Version.Http11; }
else if (line.StartsWith(s_http20)) { version = Http.Version.Http20; }
else if (line.StartsWith(s_http10)) { version = Http.Version.Http10; }
else
{
throw new Exception("invalid response (version)");
}

int codeStart = line.IndexOf((byte)' ') + 1;
var codeSlice = line.Slice(codeStart);
if (!Utf8Parser.TryParse(codeSlice, out ushort code, out consumedBytes))
{
throw new Exception("no status code");
throw new Exception("invalid response (status code)");
}

handler.OnStatusLine(Http.Version.Http11, code, codeSlice.Slice(consumedBytes + 1));
consumedBytes = eol + s_Eol.Length;
var reasonStart = consumedBytes + 1;
var reason = codeSlice.Slice(reasonStart, codeSlice.Length - reasonStart - 2);
consumedBytes = lf + s_Eol.Length;

handler.OnStatusLine(version, code, reason);

return true;
}

Expand Down
3 changes: 2 additions & 1 deletion src/System.Text.Http.Parser/HttpVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public enum Version
{
Unknown = -1,
Http10 = 0,
Http11 = 1
Http11 = 1,
Http20 = 2,
}
}
}