This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added some APIs needed by my AzCopy Sample (#2106)
- Loading branch information
1 parent
1783bd6
commit 9ca2c3b
Showing
8 changed files
with
296 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace System.Buffers.Transformations | ||
{ | ||
public struct WritableBytes : IWritable | ||
{ | ||
readonly ReadOnlyMemory<byte> _bytes; | ||
|
||
public WritableBytes(ReadOnlyMemory<byte> bytes) | ||
{ | ||
_bytes = bytes; | ||
} | ||
|
||
public bool TryWrite(Span<byte> buffer, out int written, StandardFormat format = default) | ||
{ | ||
if (format != default) throw new InvalidOperationException(); | ||
|
||
if (!_bytes.Span.TryCopyTo(buffer)) | ||
{ | ||
written = 0; | ||
return false; | ||
} | ||
written = _bytes.Length; | ||
return true; | ||
} | ||
} | ||
|
||
public class RemoveTransformation : IBufferTransformation | ||
{ | ||
byte _value; | ||
public RemoveTransformation(byte valueToRemove) | ||
{ | ||
_value = valueToRemove; | ||
} | ||
public OperationStatus Execute(ReadOnlySpan<byte> input, Span<byte> output, out int consumed, out int written) | ||
{ | ||
written = 0; | ||
for (consumed = 0; consumed < input.Length; consumed++) | ||
{ | ||
if (input[consumed] == _value) continue; | ||
if (written >= output.Length) return OperationStatus.DestinationTooSmall; | ||
output[written++] = input[consumed]; | ||
} | ||
return OperationStatus.Done; | ||
} | ||
|
||
public OperationStatus Transform(Span<byte> buffer, int dataLength, out int written) | ||
{ | ||
written = 0; | ||
for (int consumed = 0; consumed < dataLength; consumed++) | ||
{ | ||
if (buffer[consumed] == _value) continue; | ||
if (written >= buffer.Length) return OperationStatus.DestinationTooSmall; | ||
buffer[written++] = buffer[consumed]; | ||
} | ||
return OperationStatus.Done; | ||
} | ||
} | ||
} | ||
|
34 changes: 34 additions & 0 deletions
34
src/System.Buffers.Experimental/System/Buffers/Text/BufferWriter_ints.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
namespace System.Buffers.Text | ||
{ | ||
public ref partial struct BufferWriter | ||
{ | ||
public bool TryWrite(int value, StandardFormat format = default) | ||
{ | ||
if (!Utf8Formatter.TryFormat(value, Free, out int written, format)) return false; | ||
_written += written; | ||
return true; | ||
} | ||
|
||
public void Write(int value, StandardFormat format = default) | ||
{ | ||
while (!TryWrite(value, format)) Resize(); | ||
} | ||
|
||
public bool TryWriteLine(long value, StandardFormat format = default) | ||
{ | ||
if (!Utf8Formatter.TryFormat(value, Free, out int written, format)) return false; | ||
if (!NewLine.TryCopyTo(Free.Slice(written))) return false; | ||
_written += written + NewLine.Length; | ||
return true; | ||
} | ||
|
||
public void WriteLine(long value, StandardFormat format = default) | ||
{ | ||
while (!TryWriteLine(value, format)) Resize(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace System.Text.Http.Parser | ||
{ | ||
public interface IHttpResponseLineHandler | ||
{ | ||
void OnStartLine(Http.Version version, ushort code, ReadOnlySpan<byte> status); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/System.Text.Http/System/Text/Http/BufferWriterHttpExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Buffers; | ||
using System.Buffers.Text; | ||
|
||
namespace System.Text.Http.Formatter | ||
{ | ||
public static class BufferWriterHttpExtensions | ||
{ | ||
public static readonly byte[] s_httpNewline = new byte[] { 13, 10 }; | ||
public static readonly byte[] s_eoh = new byte[] { 13, 10, 13, 10 }; | ||
static byte[] s_Get = new byte[] { (byte)'G', (byte)'E', (byte)'T' }; | ||
static byte[] s_Put = new byte[] { (byte)'P', (byte)'U', (byte)'T' }; | ||
|
||
public static BufferWriter AsHttpWriter(this Span<byte> buffer) | ||
{ | ||
var writer = BufferWriter.Create(buffer); | ||
writer.NewLine = s_httpNewline; | ||
return writer; | ||
} | ||
|
||
public static void WriteRequestLine(ref this BufferWriter writer, Parser.Http.Method verb, string path) | ||
{ | ||
writer.WriteBytes(verb.AsBytes()); | ||
writer.Write(" /"); | ||
|
||
writer.Write(path); | ||
writer.WriteLine(" HTTP/1.1"); | ||
} | ||
|
||
public static void WriteHeader(ref this BufferWriter writer, string headerName, string headerValue) | ||
{ | ||
writer.Write(headerName); | ||
writer.Write(":"); | ||
writer.WriteLine(headerValue); | ||
} | ||
|
||
public static void WriteHeader(ref this BufferWriter writer, string headerName, int headerValue) | ||
{ | ||
writer.Write(headerName); | ||
writer.Write(":"); | ||
// TODO (Pri 0): this allocation needs to be eliminated | ||
writer.WriteLine(headerValue); | ||
} | ||
|
||
public static void WriteHeader(ref this BufferWriter writer, string headerName, long headerValue) | ||
{ | ||
writer.Write(headerName); | ||
writer.Write(":"); | ||
|
||
writer.WriteLine(headerValue); | ||
} | ||
|
||
public static void WriteHeader(ref this BufferWriter writer, string headerName, DateTime headerValue, StandardFormat format) | ||
{ | ||
writer.Write(headerName); | ||
writer.Write(":"); | ||
writer.WriteLine(headerValue, format); | ||
} | ||
|
||
public static void WriteHeader<T>(ref this BufferWriter writer, string headerName, T headerValue, StandardFormat format) | ||
where T : IWritable | ||
{ | ||
writer.Write(headerName); | ||
writer.Write(":"); | ||
writer.WriteBytes(headerValue, format); | ||
writer.WriteLine(""); | ||
} | ||
|
||
public static void WriteEoh(ref this BufferWriter writer) | ||
{ | ||
writer.WriteLine(""); | ||
} | ||
|
||
static ReadOnlySpan<byte> AsBytes(this Parser.Http.Method verb) | ||
{ | ||
if (verb == Parser.Http.Method.Get) return s_Get; | ||
if (verb == Parser.Http.Method.Put) return s_Put; | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |