Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
DecompressionHandler.cs
Go to the documentation of this file.
2using System.IO;
7
8namespace System.Net.Http;
9
11{
12 private abstract class DecompressedContent : HttpContent
13 {
14 private readonly HttpContent _originalContent;
15
16 private bool _contentConsumed;
17
18 internal override bool AllowDuplex => false;
19
21 {
23 _contentConsumed = false;
24 base.Headers.AddHeaders(originalContent.Headers);
25 base.Headers.ContentLength = null;
26 base.Headers.ContentEncoding.Clear();
27 string text = null;
28 foreach (string item in originalContent.Headers.ContentEncoding)
29 {
30 if (text != null)
31 {
32 base.Headers.ContentEncoding.Add(text);
33 }
34 text = item;
35 }
36 }
37
39
45
47 {
49 }
50
62
64 {
65 return CreateContentReadStreamAsyncCore(async: false, cancellationToken).GetAwaiter().GetResult();
66 }
67
72
97
99 {
101 if (stream != null)
102 {
104 }
105 return null;
106 }
107
108 protected internal override bool TryComputeLength(out long length)
109 {
110 length = 0L;
111 return false;
112 }
113
114 protected override void Dispose(bool disposing)
115 {
116 if (disposing)
117 {
119 }
120 base.Dispose(disposing);
121 }
122 }
123
125 {
130
132 {
133 return new GZipStream(originalStream, CompressionMode.Decompress);
134 }
135 }
136
138 {
139 private sealed class ZLibOrDeflateStream : HttpBaseStream
140 {
142 {
143 private enum FirstByteStatus : byte
144 {
145 None,
146 Available,
148 }
149
150 private readonly Stream _stream;
151
152 private byte _firstByte;
153
155
156 public override bool CanRead => true;
157
158 public override bool CanWrite => false;
159
164
165 protected override void Dispose(bool disposing)
166 {
167 if (disposing)
168 {
170 }
171 base.Dispose(disposing);
172 }
173
178
179 public int PeekFirstByte()
180 {
181 int num = _stream.ReadByte();
182 if (num == -1)
183 {
185 return -1;
186 }
187 _firstByte = (byte)num;
189 return num;
190 }
191
193 {
194 byte[] buffer = new byte[1];
196 {
198 return -1;
199 }
200 _firstByte = buffer[0];
202 return buffer[0];
203 }
204
205 public override int Read(Span<byte> buffer)
206 {
207 if (_firstByteStatus == FirstByteStatus.Available)
208 {
209 if (buffer.Length != 0)
210 {
211 buffer[0] = _firstByte;
213 return 1;
214 }
215 return 0;
216 }
217 return _stream.Read(buffer);
218 }
219
221 {
222 if (_firstByteStatus == FirstByteStatus.Available)
223 {
224 if (buffer.Length != 0)
225 {
226 buffer.Span[0] = _firstByte;
228 return new ValueTask<int>(1);
229 }
230 return new ValueTask<int>(0);
231 }
233 }
234
245 }
246
248
250
251 public override bool CanRead => true;
252
253 public override bool CanWrite => false;
254
259
260 protected override void Dispose(bool disposing)
261 {
262 if (disposing)
263 {
266 }
267 base.Dispose(disposing);
268 }
269
274
275 public override int Read(Span<byte> buffer)
276 {
277 if (_decompressionStream == null)
278 {
279 int firstByte = _stream.PeekFirstByte();
281 }
283 }
284
286 {
287 if (_decompressionStream == null)
288 {
290 }
293 {
294 thisRef._decompressionStream = CreateDecompressionStream(await thisRef._stream.PeekFirstByteAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext: false), thisRef._stream);
295 return await thisRef._decompressionStream.ReadAsync(buffer, cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
296 }
297 }
298
312
313 private static Stream CreateDecompressionStream(int firstByte, Stream stream)
314 {
315 if ((firstByte & 0xF) != 8)
316 {
317 return new DeflateStream(stream, CompressionMode.Decompress);
318 }
319 return new ZLibStream(stream, CompressionMode.Decompress);
320 }
321 }
322
327
329 {
331 }
332 }
333
346
348
350
352
354
356
358
360
362
368
370 {
372 {
373 if (string.Equals(item.Value, encoding, StringComparison.OrdinalIgnoreCase))
374 {
375 return true;
376 }
377 }
378 return false;
379 }
380
382 {
383 if (GZipEnabled && !EncodingExists(request.Headers.AcceptEncoding, "gzip"))
384 {
385 request.Headers.AcceptEncoding.Add(s_gzipHeaderValue);
386 }
387 if (DeflateEnabled && !EncodingExists(request.Headers.AcceptEncoding, "deflate"))
388 {
389 request.Headers.AcceptEncoding.Add(s_deflateHeaderValue);
390 }
391 if (BrotliEnabled && !EncodingExists(request.Headers.AcceptEncoding, "br"))
392 {
393 request.Headers.AcceptEncoding.Add(s_brotliHeaderValue);
394 }
396 ICollection<string> contentEncoding = httpResponseMessage.Content.Headers.ContentEncoding;
397 if (contentEncoding.Count > 0)
398 {
399 string text = null;
400 foreach (string item in contentEncoding)
401 {
402 text = item;
403 }
404 if (GZipEnabled && text == "gzip")
405 {
406 httpResponseMessage.Content = new GZipDecompressedContent(httpResponseMessage.Content);
407 }
408 else if (DeflateEnabled && text == "deflate")
409 {
410 httpResponseMessage.Content = new DeflateDecompressedContent(httpResponseMessage.Content);
411 }
412 else if (BrotliEnabled && text == "br")
413 {
414 httpResponseMessage.Content = new BrotliDecompressedContent(httpResponseMessage.Content);
415 }
416 }
417 return httpResponseMessage;
418 }
419
420 protected override void Dispose(bool disposing)
421 {
422 if (disposing)
423 {
425 }
426 base.Dispose(disposing);
427 }
428}
void CopyTo(KeyValuePair< TKey, TValue >[] array, int index)
void Add(TKey key, TValue value)
virtual int ReadByte()
Definition Stream.cs:994
static void ValidateCopyToArguments(Stream destination, int bufferSize)
Definition Stream.cs:1060
int Read(byte[] buffer, int offset, int count)
void Dispose()
Definition Stream.cs:639
Task< int > ReadAsync(byte[] buffer, int offset, int count)
Definition Stream.cs:762
Task CopyToAsync(Stream destination)
Definition Stream.cs:571
override Task< Stream > CreateContentReadStreamAsync(CancellationToken cancellationToken)
async ValueTask< Stream > CreateContentReadStreamAsyncCore(bool async, CancellationToken cancellationToken)
override async Task SerializeToStreamAsync(Stream stream, TransportContext context, CancellationToken cancellationToken)
override Stream CreateContentReadStream(CancellationToken cancellationToken)
override void SerializeToStream(Stream stream, TransportContext context, CancellationToken cancellationToken)
override Task SerializeToStreamAsync(Stream stream, TransportContext context)
override ValueTask WriteAsync(ReadOnlyMemory< byte > buffer, CancellationToken cancellationToken)
override ValueTask< int > ReadAsync(Memory< byte > buffer, CancellationToken cancellationToken)
override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
override ValueTask WriteAsync(ReadOnlyMemory< byte > buffer, CancellationToken cancellationToken)
override async Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
override ValueTask< int > ReadAsync(Memory< byte > buffer, CancellationToken cancellationToken)
override async ValueTask< HttpResponseMessage > SendAsync(HttpRequestMessage request, bool async, CancellationToken cancellationToken)
override void Dispose(bool disposing)
static readonly StringWithQualityHeaderValue s_gzipHeaderValue
readonly HttpMessageHandlerStage _innerHandler
static readonly StringWithQualityHeaderValue s_brotliHeaderValue
static bool EncodingExists(HttpHeaderValueCollection< StringWithQualityHeaderValue > acceptEncodingHeader, string encoding)
DecompressionHandler(DecompressionMethods decompressionMethods, HttpMessageHandlerStage innerHandler)
readonly DecompressionMethods _decompressionMethods
static readonly StringWithQualityHeaderValue s_deflateHeaderValue
virtual Task< Stream > CreateContentReadStreamAsync()
Task< Stream > ReadAsStreamAsync()
virtual void Dispose(bool disposing)
override Task< HttpResponseMessage > SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
virtual void Dispose(bool disposing)
static string net_http_content_stream_already_read
Definition SR.cs:42
Definition SR.cs:7
new ConfiguredTaskAwaitable< TResult > ConfigureAwait(bool continueOnCapturedContext)
Definition Task.cs:226