-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHttpContentCompressor.cs
139 lines (124 loc) · 4.87 KB
/
HttpContentCompressor.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
// Copyright (c) Microsoft. All rights reserved.
// Copyright (c) Ikpil Choi ikpil@naver.com All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace UniNetty.Codecs.Http
{
using System;
using System.Diagnostics.Contracts;
using UniNetty.Codecs.Compression;
using UniNetty.Common.Utilities;
using UniNetty.Transport.Channels;
using UniNetty.Transport.Channels.Embedded;
public class HttpContentCompressor : HttpContentEncoder
{
static readonly AsciiString GZipString = AsciiString.Cached("gzip");
static readonly AsciiString DeflateString = AsciiString.Cached("deflate");
readonly int compressionLevel;
readonly int windowBits;
readonly int memLevel;
IChannelHandlerContext handlerContext;
public HttpContentCompressor() : this(6)
{
}
public HttpContentCompressor(int compressionLevel) : this(compressionLevel, 15, 8)
{
}
public HttpContentCompressor(int compressionLevel, int windowBits, int memLevel)
{
Contract.Requires(compressionLevel >= 0 && compressionLevel <= 9);
Contract.Requires(windowBits >= 9 && windowBits <= 15);
Contract.Requires(memLevel >= 1 && memLevel <= 9);
this.compressionLevel = compressionLevel;
this.windowBits = windowBits;
this.memLevel = memLevel;
}
public override void HandlerAdded(IChannelHandlerContext context) => this.handlerContext = context;
protected override Result BeginEncode(IHttpResponse headers, ICharSequence acceptEncoding)
{
if (headers.Headers.Contains(HttpHeaderNames.ContentEncoding))
{
// Content-Encoding was set, either as something specific or as the IDENTITY encoding
// Therefore, we should NOT encode here
return null;
}
ZlibWrapper? wrapper = this.DetermineWrapper(acceptEncoding);
if (wrapper == null)
{
return null;
}
ICharSequence targetContentEncoding;
switch (wrapper.Value)
{
case ZlibWrapper.Gzip:
targetContentEncoding = GZipString;
break;
case ZlibWrapper.Zlib:
targetContentEncoding = DeflateString;
break;
default:
throw new CodecException($"{wrapper.Value} not supported, only Gzip and Zlib are allowed.");
}
return new Result(targetContentEncoding,
new EmbeddedChannel(
this.handlerContext.Channel.Id,
this.handlerContext.Channel.Metadata.HasDisconnect,
this.handlerContext.Channel.Configuration,
ZlibCodecFactory.NewZlibEncoder(
wrapper.Value, this.compressionLevel, this.windowBits, this.memLevel)));
}
protected internal ZlibWrapper? DetermineWrapper(ICharSequence acceptEncoding)
{
float starQ = -1.0f;
float gzipQ = -1.0f;
float deflateQ = -1.0f;
ICharSequence[] parts = CharUtil.Split(acceptEncoding, ',');
foreach (ICharSequence encoding in parts)
{
float q = 1.0f;
int equalsPos = encoding.IndexOf('=');
if (equalsPos != -1)
{
try
{
q = float.Parse(encoding.ToString(equalsPos + 1));
}
catch (FormatException)
{
// Ignore encoding
q = 0.0f;
}
}
if (CharUtil.Contains(encoding, '*'))
{
starQ = q;
}
else if (AsciiString.Contains(encoding, GZipString) && q > gzipQ)
{
gzipQ = q;
}
else if (AsciiString.Contains(encoding, DeflateString) && q > deflateQ)
{
deflateQ = q;
}
}
if (gzipQ > 0.0f || deflateQ > 0.0f)
{
return gzipQ >= deflateQ ? ZlibWrapper.Gzip : ZlibWrapper.Zlib;
}
if (starQ > 0.0f)
{
// ReSharper disable CompareOfFloatsByEqualityOperator
if (gzipQ == -1.0f)
{
return ZlibWrapper.Gzip;
}
if (deflateQ == -1.0f)
{
return ZlibWrapper.Zlib;
}
// ReSharper restore CompareOfFloatsByEqualityOperator
}
return null;
}
}
}