-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathLineSeparatedHttpContent.cs
211 lines (164 loc) · 7.62 KB
/
LineSeparatedHttpContent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System.Net;
using System.Net.Http;
namespace k8s
{
internal sealed class LineSeparatedHttpContent : HttpContent
{
private readonly HttpContent _originContent;
private readonly CancellationToken _cancellationToken;
private Stream _originStream;
public LineSeparatedHttpContent(HttpContent originContent, CancellationToken cancellationToken)
{
_originContent = originContent;
_cancellationToken = cancellationToken;
}
public TextReader StreamReader { get; private set; }
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
_originStream = await _originContent.ReadAsStreamAsync().ConfigureAwait(false);
var reader = new PeekableStreamReader(new CancelableStream(_originStream, _cancellationToken));
StreamReader = reader;
var firstLine = await reader.PeekLineAsync().ConfigureAwait(false);
var writer = new StreamWriter(stream);
await writer.WriteAsync(firstLine).ConfigureAwait(false);
await writer.FlushAsync().ConfigureAwait(false);
}
protected override bool TryComputeLength(out long length)
{
length = 0;
return false;
}
internal sealed class CancelableStream : Stream
{
private readonly Stream _innerStream;
private readonly CancellationToken _cancellationToken;
public CancelableStream(Stream innerStream, CancellationToken cancellationToken)
{
_innerStream = innerStream;
_cancellationToken = cancellationToken;
}
public override void Flush() =>
_innerStream.FlushAsync(_cancellationToken).GetAwaiter().GetResult();
public override async Task FlushAsync(CancellationToken cancellationToken)
{
using (var cancellationTokenSource = CreateCancellationTokenSource(cancellationToken))
{
await _innerStream.FlushAsync(cancellationTokenSource.Token).ConfigureAwait(false);
}
}
public override int Read(byte[] buffer, int offset, int count) =>
_innerStream.ReadAsync(buffer, offset, count, _cancellationToken).GetAwaiter().GetResult();
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count,
CancellationToken cancellationToken)
{
using (var cancellationTokenSource = CreateCancellationTokenSource(cancellationToken))
{
return await _innerStream.ReadAsync(buffer, offset, count, cancellationTokenSource.Token)
.ConfigureAwait(false);
}
}
public override long Seek(long offset, SeekOrigin origin) => _innerStream.Seek(offset, origin);
public override void SetLength(long value) => _innerStream.SetLength(value);
public override void Write(byte[] buffer, int offset, int count) =>
_innerStream.WriteAsync(buffer, offset, count, _cancellationToken).GetAwaiter().GetResult();
public override async Task WriteAsync(byte[] buffer, int offset, int count,
CancellationToken cancellationToken)
{
using (var cancellationTokenSource = CreateCancellationTokenSource(cancellationToken))
{
await _innerStream.WriteAsync(buffer, offset, count, cancellationTokenSource.Token)
.ConfigureAwait(false);
}
}
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)
{
if (disposing)
{
_innerStream.Dispose();
}
base.Dispose(disposing);
}
private LinkedCancellationTokenSource CreateCancellationTokenSource(CancellationToken userCancellationToken)
{
return new LinkedCancellationTokenSource(_cancellationToken, userCancellationToken);
}
private readonly struct LinkedCancellationTokenSource : IDisposable
{
private readonly CancellationTokenSource _cancellationTokenSource;
public LinkedCancellationTokenSource(CancellationToken token1, CancellationToken token2)
{
if (token1.CanBeCanceled && token2.CanBeCanceled)
{
_cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(token1, token2);
Token = _cancellationTokenSource.Token;
}
else
{
_cancellationTokenSource = null;
Token = token1.CanBeCanceled ? token1 : token2;
}
}
public CancellationToken Token { get; }
public void Dispose()
{
_cancellationTokenSource?.Dispose();
}
}
}
internal sealed class PeekableStreamReader : TextReader
{
private readonly Queue<string> _buffer;
private readonly StreamReader _inner;
public PeekableStreamReader(Stream stream)
{
_buffer = new Queue<string>();
_inner = new StreamReader(stream);
}
public override string ReadLine() => throw new NotImplementedException();
public override Task<string> ReadLineAsync()
{
if (_buffer.Count > 0)
{
return Task.FromResult(_buffer.Dequeue());
}
return _inner.ReadLineAsync();
}
public async Task<string> PeekLineAsync()
{
var line = await ReadLineAsync().ConfigureAwait(false);
if (line == null)
{
throw new EndOfStreamException();
}
_buffer.Enqueue(line);
return line;
}
public override int Read() => throw new NotImplementedException();
public override int Read(char[] buffer, int index, int count) => throw new NotImplementedException();
public override Task<int> ReadAsync(char[] buffer, int index, int count) =>
throw new NotImplementedException();
public override int ReadBlock(char[] buffer, int index, int count) => throw new NotImplementedException();
public override Task<int> ReadBlockAsync(char[] buffer, int index, int count) =>
throw new NotImplementedException();
public override string ReadToEnd() => throw new NotImplementedException();
public override Task<string> ReadToEndAsync() => throw new NotImplementedException();
protected override void Dispose(bool disposing)
{
if (disposing)
{
_inner.Dispose();
}
base.Dispose(disposing);
}
}
}
}