forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgressContent.cs
More file actions
60 lines (51 loc) · 1.92 KB
/
Copy pathProgressContent.cs
File metadata and controls
60 lines (51 loc) · 1.92 KB
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Diagnostics.Contracts;
using System.IO;
using System.Threading.Tasks;
namespace System.Net.Http.Handlers
{
/// <summary>
/// Wraps an inner <see cref="HttpContent"/> in order to insert a <see cref="ProgressStream"/> on writing data.
/// </summary>
internal class ProgressContent : HttpContent
{
private readonly HttpContent _innerContent;
private readonly ProgressMessageHandler _handler;
private readonly HttpRequestMessage _request;
public ProgressContent(HttpContent innerContent, ProgressMessageHandler handler, HttpRequestMessage request)
{
Contract.Assert(innerContent != null);
Contract.Assert(handler != null);
Contract.Assert(request != null);
_innerContent = innerContent;
_handler = handler;
_request = request;
innerContent.Headers.CopyTo(Headers);
}
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
ProgressStream progressStream = new ProgressStream(stream, _handler, _request, response: null);
return _innerContent.CopyToAsync(progressStream);
}
protected override bool TryComputeLength(out long length)
{
long? contentLength = _innerContent.Headers.ContentLength;
if (contentLength.HasValue)
{
length = contentLength.Value;
return true;
}
length = -1;
return false;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
_innerContent.Dispose();
}
}
}
}