|
4 | 4 | using System;
|
5 | 5 | using System.Collections.Generic;
|
6 | 6 | using System.Diagnostics.Contracts;
|
7 |
| -using System.Text; |
| 7 | +using System.Runtime.CompilerServices; |
8 | 8 |
|
9 | 9 | namespace Microsoft.Net.Http.Headers
|
10 | 10 | {
|
@@ -87,68 +87,157 @@ public string Value
|
87 | 87 | public bool HttpOnly { get; set; }
|
88 | 88 |
|
89 | 89 | // name="val ue"; expires=Sun, 06 Nov 1994 08:49:37 GMT; max-age=86400; domain=domain1; path=path1; secure; httponly
|
90 |
| - public override string ToString() |
| 90 | + public override unsafe string ToString() |
91 | 91 | {
|
92 |
| - StringBuilder header = new StringBuilder(); |
93 |
| - AppendToStringBuilder(header); |
| 92 | + const string equals = "="; |
| 93 | + const string separator = "; "; |
94 | 94 |
|
95 |
| - return header.ToString(); |
96 |
| - } |
| 95 | + var length = _name.Length + equals.Length + _value.Length; |
97 | 96 |
|
98 |
| - /// <summary> |
99 |
| - /// Append string representation of this <see cref="SetCookieHeaderValue"/> to given |
100 |
| - /// <paramref name="builder"/>. |
101 |
| - /// </summary> |
102 |
| - /// <param name="builder"> |
103 |
| - /// The <see cref="StringBuilder"/> to receive the string representation of this |
104 |
| - /// <see cref="SetCookieHeaderValue"/>. |
105 |
| - /// </param> |
106 |
| - public void AppendToStringBuilder(StringBuilder builder) |
107 |
| - { |
108 |
| - builder.Append(_name); |
109 |
| - builder.Append("="); |
110 |
| - builder.Append(_value); |
| 97 | + string expires = null; |
| 98 | + string maxAge = null; |
111 | 99 |
|
112 | 100 | if (Expires.HasValue)
|
113 | 101 | {
|
114 |
| - AppendSegment(builder, ExpiresToken, HeaderUtilities.FormatDate(Expires.Value)); |
| 102 | + expires = HeaderUtilities.FormatDate(Expires.Value); |
| 103 | + length += separator.Length + ExpiresToken.Length + equals.Length + expires.Length; |
115 | 104 | }
|
116 | 105 |
|
117 | 106 | if (MaxAge.HasValue)
|
118 | 107 | {
|
119 |
| - AppendSegment(builder, MaxAgeToken, HeaderUtilities.FormatInt64((long)MaxAge.Value.TotalSeconds)); |
| 108 | + maxAge = HeaderUtilities.FormatInt64((long)MaxAge.Value.TotalSeconds); |
| 109 | + length += separator.Length + MaxAgeToken.Length + equals.Length + maxAge.Length; |
120 | 110 | }
|
121 | 111 |
|
122 | 112 | if (Domain != null)
|
123 | 113 | {
|
124 |
| - AppendSegment(builder, DomainToken, Domain); |
| 114 | + length += separator.Length + DomainToken.Length + equals.Length + Domain.Length; |
125 | 115 | }
|
126 | 116 |
|
127 | 117 | if (Path != null)
|
128 | 118 | {
|
129 |
| - AppendSegment(builder, PathToken, Path); |
| 119 | + length += separator.Length + PathToken.Length + equals.Length + Path.Length; |
130 | 120 | }
|
131 | 121 |
|
132 | 122 | if (Secure)
|
133 | 123 | {
|
134 |
| - AppendSegment(builder, SecureToken, null); |
| 124 | + length += separator.Length + SecureToken.Length; |
135 | 125 | }
|
136 | 126 |
|
137 | 127 | if (HttpOnly)
|
138 | 128 | {
|
139 |
| - AppendSegment(builder, HttpOnlyToken, null); |
| 129 | + length += separator.Length + HttpOnlyToken.Length; |
140 | 130 | }
|
141 |
| - } |
142 | 131 |
|
143 |
| - private static void AppendSegment(StringBuilder builder, string name, string value) |
144 |
| - { |
145 |
| - builder.Append("; "); |
146 |
| - builder.Append(name); |
147 |
| - if (value != null) |
| 132 | + var header = new string('\0', length); |
| 133 | + |
| 134 | + fixed (char* pSeparator = separator) |
| 135 | + fixed (char* pEquals = equals) |
| 136 | + fixed (char* pHeader = header) |
148 | 137 | {
|
149 |
| - builder.Append("="); |
150 |
| - builder.Append(value); |
| 138 | + var cHeader = pHeader; |
| 139 | + fixed (char* pName = _name) |
| 140 | + { |
| 141 | + Unsafe.CopyBlock(cHeader, pName, (uint)_name.Length * 2); |
| 142 | + cHeader += _name.Length; |
| 143 | + } |
| 144 | + |
| 145 | + Unsafe.CopyBlock(cHeader, pEquals, (uint)equals.Length * 2); |
| 146 | + cHeader += equals.Length; |
| 147 | + |
| 148 | + fixed (char* pValue = _value) |
| 149 | + { |
| 150 | + Unsafe.CopyBlock(cHeader, pValue, (uint)_value.Length * 2); |
| 151 | + cHeader += _value.Length; |
| 152 | + } |
| 153 | + |
| 154 | + if (expires != null) |
| 155 | + { |
| 156 | + fixed (char* pExpires = expires) |
| 157 | + fixed (char* pExpiresToken = ExpiresToken) |
| 158 | + { |
| 159 | + Unsafe.CopyBlock(cHeader, pSeparator, (uint)separator.Length * 2); |
| 160 | + cHeader += separator.Length; |
| 161 | + Unsafe.CopyBlock(cHeader, pExpiresToken, (uint)ExpiresToken.Length * 2); |
| 162 | + cHeader += ExpiresToken.Length; |
| 163 | + Unsafe.CopyBlock(cHeader, pEquals, (uint)equals.Length * 2); |
| 164 | + cHeader += equals.Length; |
| 165 | + Unsafe.CopyBlock(cHeader, pExpires, (uint)expires.Length * 2); |
| 166 | + cHeader += expires.Length; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + if (maxAge != null) |
| 171 | + { |
| 172 | + fixed (char* pMaxAge = maxAge) |
| 173 | + fixed (char* pMaxAgeToken = MaxAgeToken) |
| 174 | + { |
| 175 | + Unsafe.CopyBlock(cHeader, pSeparator, (uint)separator.Length * 2); |
| 176 | + cHeader += separator.Length; |
| 177 | + Unsafe.CopyBlock(cHeader, pMaxAgeToken, (uint)MaxAgeToken.Length * 2); |
| 178 | + cHeader += MaxAgeToken.Length; |
| 179 | + Unsafe.CopyBlock(cHeader, pEquals, (uint)equals.Length * 2); |
| 180 | + cHeader += equals.Length; |
| 181 | + Unsafe.CopyBlock(cHeader, pMaxAge, (uint)maxAge.Length * 2); |
| 182 | + cHeader += maxAge.Length; |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + if (Domain != null) |
| 187 | + { |
| 188 | + fixed (char* pDomain = Domain) |
| 189 | + fixed (char* pDomainToken = DomainToken) |
| 190 | + { |
| 191 | + Unsafe.CopyBlock(cHeader, pSeparator, (uint)separator.Length * 2); |
| 192 | + cHeader += separator.Length; |
| 193 | + Unsafe.CopyBlock(cHeader, pDomainToken, (uint)DomainToken.Length * 2); |
| 194 | + cHeader += DomainToken.Length; |
| 195 | + Unsafe.CopyBlock(cHeader, pEquals, (uint)equals.Length * 2); |
| 196 | + cHeader += equals.Length; |
| 197 | + Unsafe.CopyBlock(cHeader, pDomain, (uint)Domain.Length * 2); |
| 198 | + cHeader += Domain.Length; |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + if (Path != null) |
| 203 | + { |
| 204 | + fixed (char* pPath = Path) |
| 205 | + fixed (char* pPathToken = PathToken) |
| 206 | + { |
| 207 | + Unsafe.CopyBlock(cHeader, pSeparator, (uint)separator.Length * 2); |
| 208 | + cHeader += separator.Length; |
| 209 | + Unsafe.CopyBlock(cHeader, pPathToken, (uint)PathToken.Length * 2); |
| 210 | + cHeader += PathToken.Length; |
| 211 | + Unsafe.CopyBlock(cHeader, pEquals, (uint)equals.Length * 2); |
| 212 | + cHeader += equals.Length; |
| 213 | + Unsafe.CopyBlock(cHeader, pPath, (uint)Path.Length * 2); |
| 214 | + cHeader += Path.Length; |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + if (Secure) |
| 219 | + { |
| 220 | + fixed (char* pSecureToken = SecureToken) |
| 221 | + { |
| 222 | + Unsafe.CopyBlock(cHeader, pSeparator, (uint)separator.Length * 2); |
| 223 | + cHeader += separator.Length; |
| 224 | + Unsafe.CopyBlock(cHeader, pSecureToken, (uint)SecureToken.Length * 2); |
| 225 | + cHeader += SecureToken.Length; |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + if (HttpOnly) |
| 230 | + { |
| 231 | + fixed (char* pHttpOnlyToken = HttpOnlyToken) |
| 232 | + { |
| 233 | + Unsafe.CopyBlock(cHeader, pSeparator, (uint)separator.Length * 2); |
| 234 | + cHeader += separator.Length; |
| 235 | + Unsafe.CopyBlock(cHeader, pHttpOnlyToken, (uint)HttpOnlyToken.Length * 2); |
| 236 | + } |
| 237 | + } |
151 | 238 | }
|
| 239 | + |
| 240 | + return header; |
152 | 241 | }
|
153 | 242 |
|
154 | 243 | public static SetCookieHeaderValue Parse(string input)
|
|
0 commit comments