-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCookiesUtils.cs
38 lines (33 loc) · 1.19 KB
/
CookiesUtils.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SuperMemoAssistant.Services.HTML
{
public static class CookiesUtils
{
/// <summary>
/// Parse a cookies string and return individual values
/// Original : https://github.com/NancyFx/Nancy/blob/master/src/Nancy/Request.cs
/// </summary>
/// <param name="inlineCookies">Cookies string</param>
/// <param name="urlEncode">Escapes invalid url characters</param>
/// <returns>Name-value pairs</returns>
public static IDictionary<string, string> ParseCookies(string inlineCookies, bool urlEncode = false)
{
var cookieDictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
var values = inlineCookies.TrimEnd(';').Split(';');
foreach (var parts in values.Select(c => c.Split(new[] { '=' }, 2)))
{
var cookieName = parts[0].Trim();
var cookieValue = parts.Length == 1 ?
string.Empty
: HttpUtility.UrlDecode(parts[1]);
if (urlEncode)
cookieValue = HttpUtility.UrlEncode(cookieValue);
cookieDictionary[cookieName] = cookieValue;
}
return cookieDictionary;
}
}
}