@@ -17,6 +17,8 @@ public class ChunkingCookieManager : ICookieManager
1717 {
1818 public ChunkingCookieManager ( )
1919 {
20+ // Lowest common denominator. Safari has the lowest known limit (4093), and we leave little extra just in case.
21+ // See http://browsercookielimits.x64.me/.
2022 ChunkSize = 4090 ;
2123 ThrowForPartialCookies = true ;
2224 }
@@ -40,9 +42,8 @@ private static int ParseChunksCount(string value)
4042 {
4143 if ( value != null && value . StartsWith ( "chunks:" , StringComparison . Ordinal ) )
4244 {
43- string chunksCountString = value . Substring ( "chunks:" . Length ) ;
44- int chunksCount ;
45- if ( int . TryParse ( chunksCountString , NumberStyles . None , CultureInfo . InvariantCulture , out chunksCount ) )
45+ var chunksCountString = value . Substring ( "chunks:" . Length ) ;
46+ if ( int . TryParse ( chunksCountString , NumberStyles . None , CultureInfo . InvariantCulture , out var chunksCount ) )
4647 {
4748 return chunksCount ;
4849 }
@@ -57,28 +58,23 @@ private static int ParseChunksCount(string value)
5758 /// <param name="context"></param>
5859 /// <param name="key"></param>
5960 /// <returns>The reassembled cookie, if any, or null.</returns>
60- public string GetRequestCookie ( HttpContext context , string key )
61+ public string GetRequestCookie ( [ NotNull ] HttpContext context , [ NotNull ] string key )
6162 {
62- if ( context == null )
63- {
64- throw new ArgumentNullException ( "context" ) ;
65- }
66-
67- IReadableStringCollection requestCookies = context . Request . Cookies ;
68- string value = requestCookies [ key ] ;
69- int chunksCount = ParseChunksCount ( value ) ;
63+ var requestCookies = context . Request . Cookies ;
64+ var value = requestCookies [ key ] ;
65+ var chunksCount = ParseChunksCount ( value ) ;
7066 if ( chunksCount > 0 )
7167 {
72- bool quoted = false ;
73- string [ ] chunks = new string [ chunksCount ] ;
74- for ( int chunkId = 1 ; chunkId <= chunksCount ; chunkId ++ )
68+ var quoted = false ;
69+ var chunks = new string [ chunksCount ] ;
70+ for ( var chunkId = 1 ; chunkId <= chunksCount ; chunkId ++ )
7571 {
76- string chunk = requestCookies [ key + "C" + chunkId . ToString ( CultureInfo . InvariantCulture ) ] ;
72+ var chunk = requestCookies [ key + "C" + chunkId . ToString ( CultureInfo . InvariantCulture ) ] ;
7773 if ( chunk == null )
7874 {
7975 if ( ThrowForPartialCookies )
8076 {
81- int totalSize = 0 ;
77+ var totalSize = 0 ;
8278 for ( int i = 0 ; i < chunkId - 1 ; i ++ )
8379 {
8480 totalSize += chunks [ i ] . Length ;
@@ -97,7 +93,7 @@ public string GetRequestCookie(HttpContext context, string key)
9793 }
9894 chunks [ chunkId - 1 ] = chunk ;
9995 }
100- string merged = string . Join ( string . Empty , chunks ) ;
96+ var merged = string . Join ( string . Empty , chunks ) ;
10197 if ( quoted )
10298 {
10399 merged = Quote ( merged ) ;
@@ -119,25 +115,16 @@ public string GetRequestCookie(HttpContext context, string key)
119115 /// <param name="key"></param>
120116 /// <param name="value"></param>
121117 /// <param name="options"></param>
122- public void AppendResponseCookie ( HttpContext context , string key , string value , CookieOptions options )
118+ public void AppendResponseCookie ( [ NotNull ] HttpContext context , [ NotNull ] string key , string value , [ NotNull ] CookieOptions options )
123119 {
124- if ( context == null )
125- {
126- throw new ArgumentNullException ( "context" ) ;
127- }
128- if ( options == null )
129- {
130- throw new ArgumentNullException ( "options" ) ;
131- }
120+ var domainHasValue = ! string . IsNullOrEmpty ( options . Domain ) ;
121+ var pathHasValue = ! string . IsNullOrEmpty ( options . Path ) ;
122+ var expiresHasValue = options . Expires . HasValue ;
132123
133- bool domainHasValue = ! string . IsNullOrEmpty ( options . Domain ) ;
134- bool pathHasValue = ! string . IsNullOrEmpty ( options . Path ) ;
135- bool expiresHasValue = options . Expires . HasValue ;
124+ var escapedKey = Uri . EscapeDataString ( key ) ;
125+ var prefix = escapedKey + "=" ;
136126
137- string escapedKey = Uri . EscapeDataString ( key ) ;
138- string prefix = escapedKey + "=" ;
139-
140- string suffix = string . Concat (
127+ var suffix = string . Concat (
141128 ! domainHasValue ? null : "; domain=" ,
142129 ! domainHasValue ? null : options . Domain ,
143130 ! pathHasValue ? null : "; path=" ,
@@ -148,19 +135,19 @@ public void AppendResponseCookie(HttpContext context, string key, string value,
148135 ! options . HttpOnly ? null : "; HttpOnly" ) ;
149136
150137 value = value ?? string . Empty ;
151- bool quoted = false ;
138+ var quoted = false ;
152139 if ( IsQuoted ( value ) )
153140 {
154141 quoted = true ;
155142 value = RemoveQuotes ( value ) ;
156143 }
157- string escapedValue = Uri . EscapeDataString ( value ) ;
144+ var escapedValue = Uri . EscapeDataString ( value ) ;
158145
159146 // Normal cookie
160- IHeaderDictionary responseHeaders = context . Response . Headers ;
147+ var responseHeaders = context . Response . Headers ;
161148 if ( ! ChunkSize . HasValue || ChunkSize . Value > prefix . Length + escapedValue . Length + suffix . Length + ( quoted ? 2 : 0 ) )
162149 {
163- string setCookieValue = string . Concat (
150+ var setCookieValue = string . Concat (
164151 prefix ,
165152 quoted ? Quote ( escapedValue ) : escapedValue ,
166153 suffix ) ;
@@ -180,18 +167,18 @@ public void AppendResponseCookie(HttpContext context, string key, string value,
180167 // Set-Cookie: CookieNameC1="Segment1"; path=/
181168 // Set-Cookie: CookieNameC2="Segment2"; path=/
182169 // Set-Cookie: CookieNameC3="Segment3"; path=/
183- int dataSizePerCookie = ChunkSize . Value - prefix . Length - suffix . Length - ( quoted ? 2 : 0 ) - 3 ; // Budget 3 chars for the chunkid.
184- int cookieChunkCount = ( int ) Math . Ceiling ( escapedValue . Length * 1.0 / dataSizePerCookie ) ;
170+ var dataSizePerCookie = ChunkSize . Value - prefix . Length - suffix . Length - ( quoted ? 2 : 0 ) - 3 ; // Budget 3 chars for the chunkid.
171+ var cookieChunkCount = ( int ) Math . Ceiling ( escapedValue . Length * 1.0 / dataSizePerCookie ) ;
185172
186173 responseHeaders . AppendValues ( Constants . Headers . SetCookie , prefix + "chunks:" + cookieChunkCount . ToString ( CultureInfo . InvariantCulture ) + suffix ) ;
187-
188- string [ ] chunks = new string [ cookieChunkCount ] ;
189- int offset = 0 ;
190- for ( int chunkId = 1 ; chunkId <= cookieChunkCount ; chunkId ++ )
174+
175+ var chunks = new string [ cookieChunkCount ] ;
176+ var offset = 0 ;
177+ for ( var chunkId = 1 ; chunkId <= cookieChunkCount ; chunkId ++ )
191178 {
192- int remainingLength = escapedValue . Length - offset ;
193- int length = Math . Min ( dataSizePerCookie , remainingLength ) ;
194- string segment = escapedValue . Substring ( offset , length ) ;
179+ var remainingLength = escapedValue . Length - offset ;
180+ var length = Math . Min ( dataSizePerCookie , remainingLength ) ;
181+ var segment = escapedValue . Substring ( offset , length ) ;
195182 offset += length ;
196183
197184 chunks [ chunkId - 1 ] = string . Concat (
@@ -215,34 +202,25 @@ public void AppendResponseCookie(HttpContext context, string key, string value,
215202 /// <param name="context"></param>
216203 /// <param name="key"></param>
217204 /// <param name="options"></param>
218- public void DeleteCookie ( HttpContext context , string key , CookieOptions options )
205+ public void DeleteCookie ( [ NotNull ] HttpContext context , [ NotNull ] string key , [ NotNull ] CookieOptions options )
219206 {
220- if ( context == null )
221- {
222- throw new ArgumentNullException ( "context" ) ;
223- }
224- if ( options == null )
225- {
226- throw new ArgumentNullException ( "options" ) ;
227- }
228-
229- string escapedKey = Uri . EscapeDataString ( key ) ;
230- List < string > keys = new List < string > ( ) ;
207+ var escapedKey = Uri . EscapeDataString ( key ) ;
208+ var keys = new List < string > ( ) ;
231209 keys . Add ( escapedKey + "=" ) ;
232210
233- string requestCookie = context . Request . Cookies [ key ] ;
234- int chunks = ParseChunksCount ( requestCookie ) ;
211+ var requestCookie = context . Request . Cookies [ key ] ;
212+ var chunks = ParseChunksCount ( requestCookie ) ;
235213 if ( chunks > 0 )
236214 {
237215 for ( int i = 1 ; i <= chunks + 1 ; i ++ )
238216 {
239- string subkey = escapedKey + "C" + i . ToString ( CultureInfo . InvariantCulture ) ;
217+ var subkey = escapedKey + "C" + i . ToString ( CultureInfo . InvariantCulture ) ;
240218 keys . Add ( subkey + "=" ) ;
241219 }
242220 }
243221
244- bool domainHasValue = ! string . IsNullOrEmpty ( options . Domain ) ;
245- bool pathHasValue = ! string . IsNullOrEmpty ( options . Path ) ;
222+ var domainHasValue = ! string . IsNullOrEmpty ( options . Domain ) ;
223+ var pathHasValue = ! string . IsNullOrEmpty ( options . Path ) ;
246224
247225 Func < string , bool > rejectPredicate ;
248226 Func < string , bool > predicate = value => keys . Any ( k => value . StartsWith ( k , StringComparison . OrdinalIgnoreCase ) ) ;
@@ -259,8 +237,8 @@ public void DeleteCookie(HttpContext context, string key, CookieOptions options)
259237 rejectPredicate = value => predicate ( value ) ;
260238 }
261239
262- IHeaderDictionary responseHeaders = context . Response . Headers ;
263- IList < string > existingValues = responseHeaders . GetValues ( Constants . Headers . SetCookie ) ;
240+ var responseHeaders = context . Response . Headers ;
241+ var existingValues = responseHeaders . GetValues ( Constants . Headers . SetCookie ) ;
264242 if ( existingValues != null )
265243 {
266244 responseHeaders . SetValues ( Constants . Headers . SetCookie , existingValues . Where ( value => ! rejectPredicate ( value ) ) . ToArray ( ) ) ;
@@ -270,7 +248,7 @@ public void DeleteCookie(HttpContext context, string key, CookieOptions options)
270248 context ,
271249 key ,
272250 string . Empty ,
273- new CookieOptions
251+ new CookieOptions ( )
274252 {
275253 Path = options . Path ,
276254 Domain = options . Domain ,
@@ -283,7 +261,7 @@ public void DeleteCookie(HttpContext context, string key, CookieOptions options)
283261 context ,
284262 key + "C" + i . ToString ( CultureInfo . InvariantCulture ) ,
285263 string . Empty ,
286- new CookieOptions
264+ new CookieOptions ( )
287265 {
288266 Path = options . Path ,
289267 Domain = options . Domain ,
@@ -292,17 +270,17 @@ public void DeleteCookie(HttpContext context, string key, CookieOptions options)
292270 }
293271 }
294272
295- private static bool IsQuoted ( string value )
273+ private static bool IsQuoted ( [ NotNull ] string value )
296274 {
297275 return value . Length >= 2 && value [ 0 ] == '"' && value [ value . Length - 1 ] == '"' ;
298276 }
299277
300- private static string RemoveQuotes ( string value )
278+ private static string RemoveQuotes ( [ NotNull ] string value )
301279 {
302280 return value . Substring ( 1 , value . Length - 2 ) ;
303281 }
304282
305- private static string Quote ( string value )
283+ private static string Quote ( [ NotNull ] string value )
306284 {
307285 return '"' + value + '"' ;
308286 }
0 commit comments