-
Notifications
You must be signed in to change notification settings - Fork 463
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for reading Chunked encoded responses over UDS (#1089)
* Add Http Chunked stream reader * Remove Http response content * Add test * Fix tests
- Loading branch information
1 parent
5bf5324
commit 83118b2
Showing
3 changed files
with
172 additions
and
23 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
edge-util/src/Microsoft.Azure.Devices.Edge.Util/uds/HttpChunkedStreamReader.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,86 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
namespace Microsoft.Azure.Devices.Edge.Util.Uds | ||
{ | ||
using System; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
class HttpChunkedStreamReader : Stream | ||
{ | ||
readonly HttpBufferedStream stream; | ||
int chunkBytes; | ||
bool eos; | ||
|
||
public HttpChunkedStreamReader(HttpBufferedStream stream) | ||
{ | ||
this.stream = Preconditions.CheckNotNull(stream, nameof(stream)); | ||
} | ||
|
||
public override bool CanRead => true; | ||
|
||
public override bool CanSeek => false; | ||
|
||
public override bool CanWrite => false; | ||
|
||
public override long Length => throw new NotSupportedException(); | ||
|
||
public override long Position | ||
{ | ||
get => throw new NotSupportedException(); | ||
set => throw new NotSupportedException(); | ||
} | ||
|
||
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
{ | ||
if (this.eos) | ||
{ | ||
return 0; | ||
} | ||
|
||
if (this.chunkBytes == 0) | ||
{ | ||
string line = await this.stream.ReadLineAsync(cancellationToken); | ||
if (!int.TryParse(line, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out this.chunkBytes)) | ||
{ | ||
throw new IOException($"Cannot parse chunk header - {line}"); | ||
} | ||
} | ||
|
||
int bytesRead = 0; | ||
if (this.chunkBytes > 0) | ||
{ | ||
int bytesToRead = Math.Min(count, this.chunkBytes); | ||
bytesRead = await this.stream.ReadAsync(buffer, offset, bytesToRead, cancellationToken); | ||
if (bytesToRead == 0) | ||
{ | ||
throw new EndOfStreamException(); | ||
} | ||
|
||
this.chunkBytes -= bytesToRead; | ||
} | ||
|
||
if (this.chunkBytes == 0) | ||
{ | ||
await this.stream.ReadLineAsync(cancellationToken); | ||
if (bytesRead == 0) | ||
{ | ||
this.eos = true; | ||
} | ||
} | ||
|
||
return bytesRead; | ||
} | ||
|
||
public override void Flush() => throw new NotImplementedException(); | ||
|
||
public override int Read(byte[] buffer, int offset, int count) => throw new NotImplementedException(); | ||
|
||
public override long Seek(long offset, SeekOrigin origin) => throw new NotImplementedException(); | ||
|
||
public override void SetLength(long value) => throw new NotImplementedException(); | ||
|
||
public override void Write(byte[] buffer, int offset, int count) => throw new NotImplementedException(); | ||
} | ||
} |
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