-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathCompressedResult.cs
More file actions
98 lines (74 loc) · 2.98 KB
/
Copy pathCompressedResult.cs
File metadata and controls
98 lines (74 loc) · 2.98 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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using ServiceStack.Web;
using ServiceStack.Text;
namespace ServiceStack
{
public class CompressedResult
: IStreamWriterAsync, IHttpResult
{
public const string DefaultContentType = MimeTypes.Xml;
public byte[] Contents { get; }
public string ContentType { get; set; }
public Dictionary<string, string> Headers { get; }
public List<Cookie> Cookies { get; }
public int Status { get; set; }
public HttpStatusCode StatusCode
{
get => (HttpStatusCode)Status;
set => Status = (int)value;
}
public string StatusDescription { get; set; }
public object Response
{
get => this.Contents;
set => throw new NotImplementedException();
}
public IContentTypeWriter ResponseFilter { get; set; }
public IRequest RequestContext { get; set; }
public int PaddingLength { get; set; }
public Func<IDisposable> ResultScope { get; set; }
public IDictionary<string, string> Options => this.Headers;
public DateTime? LastModified
{
set
{
if (value == null)
return;
this.Headers[HttpHeaders.LastModified] = value.Value.ToUniversalTime().ToString("r");
var feature = HostContext.GetPlugin<HttpCacheFeature>();
if (feature?.CacheControlForOptimizedResults != null)
this.Headers[HttpHeaders.CacheControl] = feature.CacheControlForOptimizedResults;
}
}
public CompressedResult(byte[] contents)
: this(contents, CompressionTypes.Deflate)
{ }
public CompressedResult(byte[] contents, string compressionType)
: this(contents, compressionType, DefaultContentType)
{ }
public CompressedResult(byte[] contents, string compressionType, string contentMimeType)
{
if (!CompressionTypes.IsValid(compressionType))
throw new ArgumentException("Must be " + string.Join(", ", CompressionTypes.AllCompressionTypes), compressionType);
this.StatusCode = HttpStatusCode.OK;
this.ContentType = contentMimeType;
this.Contents = contents;
this.Headers = new Dictionary<string, string> {
{ HttpHeaders.ContentEncoding, compressionType },
};
this.Cookies = new List<Cookie>();
}
public async Task WriteToAsync(Stream responseStream, CancellationToken token = new())
{
var response = RequestContext?.Response;
response?.SetContentLength(this.Contents.Length + PaddingLength);
await responseStream.WriteAsync(this.Contents, token).ConfigAwait();
await responseStream.FlushAsync(token).ConfigAwait();
}
}
}