MTSC  1.0.5
Build TCP servers out of modules with handlers for communication, exception and logging.
Cookie.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 
5 namespace MTSC.Common.Http
6 {
10  public class Cookie
11  {
15  public string Key { get; private set; }
19  public string Value { get; private set; }
23  public Dictionary<string, string> Attributes { get; private set; }
29  public Cookie(string key, string value)
30  {
31  Attributes = new Dictionary<string, string>();
32  this.Key = key;
33  this.Value = value;
34  }
39  public Cookie(string cookieString)
40  {
41  Attributes = new Dictionary<string, string>();
42  string[] cookieTokens = cookieString.Split(';');
43  Key = cookieTokens[0].Split('=')[0].Trim();
44  Value = cookieTokens[1].Split('=')[1].Trim();
45  for(int i = 1; i < cookieTokens.Length; i++)
46  {
47  string[] attributeTokens = cookieTokens[i].Split('=');
48  Attributes[attributeTokens[0].Trim()] = attributeTokens.Length > 1 ? attributeTokens[1].Trim() : string.Empty;
49  }
50  }
55  public string BuildCookieString()
56  {
57  StringBuilder sb = new StringBuilder();
58  sb.Append(Key).Append('=').Append(Value);
59  if(Attributes.Count > 0)
60  {
61  foreach(KeyValuePair<string, string> attribute in Attributes)
62  {
63  sb.Append(';').Append(attribute.Key);
64  if (!string.IsNullOrWhiteSpace(attribute.Value))
65  {
66  sb.Append('=').Append(attribute.Value);
67  }
68  }
69  }
70  return sb.ToString();
71  }
76  public byte[] BuildCookieBytes()
77  {
78  return ASCIIEncoding.ASCII.GetBytes(BuildCookieString());
79  }
80  }
81 }