aboutsummaryrefslogtreecommitdiff
path: root/SocketHttpListener/Net/CookieHelper.cs
diff options
context:
space:
mode:
authorClaus Vium <clausvium@gmail.com>2019-02-27 07:32:36 +0100
committerClaus Vium <clausvium@gmail.com>2019-02-27 07:32:36 +0100
commit77addb22835478a32c1133cfd69ae0da2ec5edea (patch)
treedbe33961dd1a2b911e992f698495df453b8285af /SocketHttpListener/Net/CookieHelper.cs
parent848cfc32cc89327e16ff6ea281dc1d9b96cc4f7a (diff)
Remove SocketHttpListener
Diffstat (limited to 'SocketHttpListener/Net/CookieHelper.cs')
-rw-r--r--SocketHttpListener/Net/CookieHelper.cs141
1 files changed, 0 insertions, 141 deletions
diff --git a/SocketHttpListener/Net/CookieHelper.cs b/SocketHttpListener/Net/CookieHelper.cs
deleted file mode 100644
index 3ad76ff23..000000000
--- a/SocketHttpListener/Net/CookieHelper.cs
+++ /dev/null
@@ -1,141 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Globalization;
-using System.Net;
-using System.Text;
-
-namespace SocketHttpListener.Net
-{
- public static class CookieHelper
- {
- internal static CookieCollection Parse(string value, bool response)
- {
- return response
- ? parseResponse(value)
- : null;
- }
-
- private static string[] splitCookieHeaderValue(string value)
- {
- return new List<string>(value.SplitHeaderValue(',', ';')).ToArray();
- }
-
- private static CookieCollection parseResponse(string value)
- {
- var cookies = new CookieCollection();
-
- Cookie cookie = null;
- var pairs = splitCookieHeaderValue(value);
- for (int i = 0; i < pairs.Length; i++)
- {
- var pair = pairs[i].Trim();
- if (pair.Length == 0)
- continue;
-
- if (pair.StartsWith("version", StringComparison.OrdinalIgnoreCase))
- {
- if (cookie != null)
- cookie.Version = int.Parse(pair.GetValueInternal("=").Trim('"'));
- }
- else if (pair.StartsWith("expires", StringComparison.OrdinalIgnoreCase))
- {
- var buffer = new StringBuilder(pair.GetValueInternal("="), 32);
- if (i < pairs.Length - 1)
- buffer.AppendFormat(", {0}", pairs[++i].Trim());
-
- if (!DateTime.TryParseExact(
- buffer.ToString(),
- new[] { "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'", "r" },
- new CultureInfo("en-US"),
- DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal,
- out var expires))
- expires = DateTime.Now;
-
- if (cookie != null && cookie.Expires == DateTime.MinValue)
- cookie.Expires = expires.ToLocalTime();
- }
- else if (pair.StartsWith("max-age", StringComparison.OrdinalIgnoreCase))
- {
- var max = int.Parse(pair.GetValueInternal("=").Trim('"'));
- var expires = DateTime.Now.AddSeconds((double)max);
- if (cookie != null)
- cookie.Expires = expires;
- }
- else if (pair.StartsWith("path", StringComparison.OrdinalIgnoreCase))
- {
- if (cookie != null)
- cookie.Path = pair.GetValueInternal("=");
- }
- else if (pair.StartsWith("domain", StringComparison.OrdinalIgnoreCase))
- {
- if (cookie != null)
- cookie.Domain = pair.GetValueInternal("=");
- }
- else if (pair.StartsWith("port", StringComparison.OrdinalIgnoreCase))
- {
- var port = pair.Equals("port", StringComparison.OrdinalIgnoreCase)
- ? "\"\""
- : pair.GetValueInternal("=");
-
- if (cookie != null)
- cookie.Port = port;
- }
- else if (pair.StartsWith("comment", StringComparison.OrdinalIgnoreCase))
- {
- if (cookie != null)
- cookie.Comment = pair.GetValueInternal("=").UrlDecode();
- }
- else if (pair.StartsWith("commenturl", StringComparison.OrdinalIgnoreCase))
- {
- if (cookie != null)
- cookie.CommentUri = pair.GetValueInternal("=").Trim('"').ToUri();
- }
- else if (pair.StartsWith("discard", StringComparison.OrdinalIgnoreCase))
- {
- if (cookie != null)
- cookie.Discard = true;
- }
- else if (pair.StartsWith("secure", StringComparison.OrdinalIgnoreCase))
- {
- if (cookie != null)
- cookie.Secure = true;
- }
- else if (pair.StartsWith("httponly", StringComparison.OrdinalIgnoreCase))
- {
- if (cookie != null)
- cookie.HttpOnly = true;
- }
- else
- {
- if (cookie != null)
- cookies.Add(cookie);
-
- string name;
- string val = string.Empty;
-
- var pos = pair.IndexOf('=');
- if (pos == -1)
- {
- name = pair;
- }
- else if (pos == pair.Length - 1)
- {
- name = pair.Substring(0, pos).TrimEnd(' ');
- }
- else
- {
- name = pair.Substring(0, pos).TrimEnd(' ');
- val = pair.Substring(pos + 1).TrimStart(' ');
- }
-
- cookie = new Cookie(name, val);
- }
- }
-
- if (cookie != null)
- cookies.Add(cookie);
-
- return cookies;
- }
- }
-}