Exemple #1
0
        /// <summary>
        /// Get the reassembled cookie. Non chunked cookies are returned normally.
        /// Cookies with missing chunks just have their "chunks:XX" header returned.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="key"></param>
        /// <returns>The reassembled cookie, if any, or null.</returns>
        public string GetRequestCookie(IOwinContext context, string key)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            var webContext = context.Get <HttpContextBase>(typeof(HttpContextBase).FullName);

            if (webContext == null)
            {
                return(Fallback.GetRequestCookie(context, key));
            }

            var requestCookies = webContext.Request.Cookies;
            var escapedKey     = Uri.EscapeDataString(key);
            var cookie         = requestCookies[escapedKey];

            if (cookie == null)
            {
                return(null);
            }

            var value       = cookie.Value;
            int chunksCount = ParseChunksCount(value);

            if (chunksCount > 0)
            {
                bool     quoted = false;
                string[] chunks = new string[chunksCount];
                for (int chunkId = 1; chunkId <= chunksCount; chunkId++)
                {
                    cookie = requestCookies[escapedKey + "C" + chunkId.ToString(CultureInfo.InvariantCulture)];
                    if (cookie == null)
                    {
                        if (ThrowForPartialCookies)
                        {
                            int totalSize = 0;
                            for (int i = 0; i < chunkId - 1; i++)
                            {
                                totalSize += chunks[i].Length;
                            }
                            throw new FormatException(
                                      string.Format(CultureInfo.CurrentCulture, Resources.Exception_ImcompleteChunkedCookie, chunkId - 1, chunksCount, totalSize));
                        }
                        // Missing chunk, abort by returning the original cookie value. It may have been a false positive?
                        return(Uri.UnescapeDataString(value));
                    }
                    string chunk = cookie.Value;
                    if (IsQuoted(chunk))
                    {
                        // Note: Since we assume these cookies were generated by our code, then we can assume that if one cookie has quotes then they all do.
                        quoted = true;
                        chunk  = RemoveQuotes(chunk);
                    }
                    chunks[chunkId - 1] = chunk;
                }
                string merged = string.Join(string.Empty, chunks);
                if (quoted)
                {
                    merged = Quote(merged);
                }
                return(Uri.UnescapeDataString(merged));
            }
            return(Uri.UnescapeDataString(value));
        }