-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hsm Authentication with Http over Unix domain sockets
- Loading branch information
1 parent
2ad6775
commit 128e147
Showing
11 changed files
with
803 additions
and
34 deletions.
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
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
108 changes: 108 additions & 0 deletions
108
iothub/device/src/netstandard20/HsmAuthentication/Transport/HttpBufferedStream.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,108 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.IO; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Azure.Devices.Client.HsmAuthentication.Transport | ||
{ | ||
internal class HttpBufferedStream : Stream | ||
{ | ||
private const char CR = '\r'; | ||
private const char LF = '\n'; | ||
private BufferedStream _innerStream; | ||
|
||
public HttpBufferedStream(Stream stream) | ||
{ | ||
_innerStream = new BufferedStream(stream); | ||
} | ||
|
||
public override void Flush() | ||
{ | ||
_innerStream.Flush(); | ||
} | ||
|
||
public override Task FlushAsync(CancellationToken cancellationToken) | ||
{ | ||
return _innerStream.FlushAsync(cancellationToken); | ||
} | ||
|
||
public override int Read(byte[] buffer, int offset, int count) | ||
{ | ||
return _innerStream.Read(buffer, offset, count); | ||
} | ||
|
||
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
{ | ||
return _innerStream.ReadAsync(buffer, offset, count, cancellationToken); | ||
} | ||
|
||
public async Task<string> ReadLineAsync(CancellationToken cancellationToken) | ||
{ | ||
int position = 0; | ||
byte[] buffer = new byte[1]; | ||
bool crFound = false; | ||
var builder = new StringBuilder(); | ||
while (true) | ||
{ | ||
var length = await _innerStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken) | ||
.ConfigureAwait(false); | ||
|
||
if (length == 0) | ||
{ | ||
throw new IOException("Unexpected end of stream."); | ||
} | ||
|
||
if (crFound && (char) buffer[position] == LF) | ||
{ | ||
builder.Remove(builder.Length - 1, 1); | ||
return builder.ToString(); | ||
} | ||
|
||
builder.Append((char) buffer[position]); | ||
crFound = (char) buffer[position] == CR; | ||
} | ||
} | ||
|
||
public override long Seek(long offset, SeekOrigin origin) | ||
{ | ||
return _innerStream.Seek(offset, origin); | ||
} | ||
|
||
public override void SetLength(long value) | ||
{ | ||
_innerStream.SetLength(value); | ||
} | ||
|
||
public override void Write(byte[] buffer, int offset, int count) | ||
{ | ||
_innerStream.Write(buffer, offset, count); | ||
} | ||
|
||
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
{ | ||
return _innerStream.WriteAsync(buffer, offset, count, cancellationToken); | ||
} | ||
|
||
public override bool CanRead => _innerStream.CanRead; | ||
|
||
public override bool CanSeek => _innerStream.CanSeek; | ||
|
||
public override bool CanWrite => _innerStream.CanWrite; | ||
|
||
public override long Length => _innerStream.Length; | ||
|
||
public override long Position | ||
{ | ||
get => _innerStream.Position; | ||
set => _innerStream.Position = value; | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
_innerStream.Dispose(); | ||
} | ||
} | ||
} |
Oops, something went wrong.