-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDefaultFullHttpRequest.cs
145 lines (119 loc) · 4.97 KB
/
DefaultFullHttpRequest.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
140
141
142
143
144
145
// 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.
// ReSharper disable ConvertToAutoPropertyWhenPossible
// ReSharper disable ConvertToAutoProperty
namespace UniNetty.Codecs.Http
{
using System.Diagnostics.Contracts;
using System.Text;
using UniNetty.Buffers;
using UniNetty.Common;
using UniNetty.Common.Utilities;
public class DefaultFullHttpRequest : DefaultHttpRequest, IFullHttpRequest
{
readonly IByteBuffer content;
readonly HttpHeaders trailingHeader;
// Used to cache the value of the hash code and avoid {@link IllegalReferenceCountException}.
int hash;
public DefaultFullHttpRequest(HttpVersion httpVersion, HttpMethod method, string uri)
: this(httpVersion, method, uri, Unpooled.Buffer(0))
{
}
public DefaultFullHttpRequest(HttpVersion httpVersion, HttpMethod method, string uri, IByteBuffer content)
: this(httpVersion, method, uri, content, true)
{
}
public DefaultFullHttpRequest(HttpVersion httpVersion, HttpMethod method, string uri, bool validateHeaders)
: this(httpVersion, method, uri, Unpooled.Buffer(0), validateHeaders)
{
}
public DefaultFullHttpRequest(HttpVersion httpVersion, HttpMethod method, string uri,
IByteBuffer content, bool validateHeaders)
: base(httpVersion, method, uri, validateHeaders)
{
Contract.Requires(content != null);
this.content = content;
this.trailingHeader = new DefaultHttpHeaders(validateHeaders);
}
public DefaultFullHttpRequest(HttpVersion httpVersion, HttpMethod method, string uri,
IByteBuffer content, HttpHeaders headers, HttpHeaders trailingHeader)
: base(httpVersion, method, uri, headers)
{
Contract.Requires(content != null);
Contract.Requires(trailingHeader != null);
this.content = content;
this.trailingHeader = trailingHeader;
}
public HttpHeaders TrailingHeaders => this.trailingHeader;
public IByteBuffer Content => this.content;
public int ReferenceCount => this.content.ReferenceCount;
public IReferenceCounted Retain()
{
this.content.Retain();
return this;
}
public IReferenceCounted Retain(int increment)
{
this.content.Retain(increment);
return this;
}
public IReferenceCounted Touch()
{
this.content.Touch();
return this;
}
public IReferenceCounted Touch(object hint)
{
this.content.Touch(hint);
return this;
}
public bool Release() => this.content.Release();
public bool Release(int decrement) => this.content.Release(decrement);
public IByteBufferHolder Copy() => this.Replace(this.content.Copy());
public IByteBufferHolder Duplicate() => this.Replace(this.content.Duplicate());
public IByteBufferHolder RetainedDuplicate() => this.Replace(this.content.RetainedDuplicate());
public IByteBufferHolder Replace(IByteBuffer newContent) =>
new DefaultFullHttpRequest(this.ProtocolVersion, this.Method, this.Uri, newContent, this.Headers, this.trailingHeader);
public override int GetHashCode()
{
// ReSharper disable NonReadonlyMemberInGetHashCode
int hashCode = this.hash;
if (hashCode == 0)
{
if (this.content.ReferenceCount != 0)
{
try
{
hashCode = 31 + this.content.GetHashCode();
}
catch (IllegalReferenceCountException)
{
// Handle race condition between checking refCnt() == 0 and using the object.
hashCode = 31;
}
}
else
{
hashCode = 31;
}
hashCode = 31 * hashCode + this.trailingHeader.GetHashCode();
hashCode = 31 * hashCode + base.GetHashCode();
this.hash = hashCode;
}
// ReSharper restore NonReadonlyMemberInGetHashCode
return hashCode;
}
public override bool Equals(object obj)
{
if (!(obj is DefaultFullHttpRequest other))
{
return false;
}
return base.Equals(other)
&& this.content.Equals(other.content)
&& this.trailingHeader.Equals(other.trailingHeader);
}
public override string ToString() => HttpMessageUtil.AppendFullRequest(new StringBuilder(256), this).ToString();
}
}