-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
CompressedResult.cs
77 lines (58 loc) · 2.24 KB
/
CompressedResult.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using ServiceStack.Service;
using ServiceStack.ServiceHost;
namespace ServiceStack.Common.Web
{
public class CompressedResult
: IStreamWriter, IHttpResult
{
public const int Adler32ChecksumLength = 4;
public const string DefaultContentType = MimeTypes.Xml;
public byte[] Contents { get; private set; }
public string ContentType { get; set; }
public Dictionary<string, string> Headers { get; private set; }
public int Status { get; set; }
public HttpStatusCode StatusCode
{
get { return (HttpStatusCode)Status; }
set { Status = (int)value; }
}
public string StatusDescription { get; set; }
public object Response
{
get { return this.Contents; }
set { throw new NotImplementedException(); }
}
public IContentTypeWriter ResponseFilter { get; set; }
public IRequestContext RequestContext { get; set; }
public IDictionary<string, string> Options
{
get { return this.Headers; }
}
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 either 'deflate' or 'gzip'", compressionType);
}
this.StatusCode = HttpStatusCode.OK;
this.ContentType = contentMimeType;
this.Contents = contents;
this.Headers = new Dictionary<string, string> {
{ HttpHeaders.ContentEncoding, compressionType },
};
}
public void WriteTo(Stream responseStream)
{
responseStream.Write(this.Contents, 0, this.Contents.Length);
//stream.Write(this.Contents, Adler32ChecksumLength, this.Contents.Length - Adler32ChecksumLength);
}
}
}