Exemplo n.º 1
0
 /// <summary>
 /// Appends the specified <paramref name="cookie"/> to the cookies sent with the response.
 /// </summary>
 /// <param name="cookie">
 /// A <see cref="Cookie"/> to append.
 /// </param>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="cookie"/> is <see langword="null"/>.
 /// </exception>
 /// <exception cref="InvalidOperationException">
 /// The response has already been sent.
 /// </exception>
 /// <exception cref="ObjectDisposedException">
 /// This object is closed.
 /// </exception>
 public void AppendCookie (Cookie cookie)
 {
   checkDisposedOrHeadersSent ();
   Cookies.Add (cookie);
 }
Exemplo n.º 2
0
    /// <summary>
    /// Adds or updates a <paramref name="cookie"/> in the cookies sent with the response.
    /// </summary>
    /// <param name="cookie">
    /// A <see cref="Cookie"/> to set.
    /// </param>
    /// <exception cref="ArgumentException">
    /// <paramref name="cookie"/> already exists in the cookies and couldn't be replaced.
    /// </exception>
    /// <exception cref="ArgumentNullException">
    /// <paramref name="cookie"/> is <see langword="null"/>.
    /// </exception>
    /// <exception cref="InvalidOperationException">
    /// The response has already been sent.
    /// </exception>
    /// <exception cref="ObjectDisposedException">
    /// This object is closed.
    /// </exception>
    public void SetCookie (Cookie cookie)
    {
      checkDisposedOrHeadersSent ();
      if (cookie == null)
        throw new ArgumentNullException ("cookie");

      if (!canAddOrUpdate (cookie))
        throw new ArgumentException ("Cannot be replaced.", "cookie");

      Cookies.Add (cookie);
    }
Exemplo n.º 3
0
    private bool canAddOrUpdate (Cookie cookie)
    {
      if (_cookies == null || _cookies.Count == 0)
        return true;

      var found = findCookie (cookie).ToList ();
      if (found.Count == 0)
        return true;

      var ver = cookie.Version;
      foreach (var c in found)
        if (c.Version == ver)
          return true;

      return false;
    }
Exemplo n.º 4
0
 private IEnumerable<Cookie> findCookie (Cookie cookie)
 {
   var name = cookie.Name;
   var domain = cookie.Domain;
   var path = cookie.Path;
   if (_cookies != null)
     foreach (Cookie c in _cookies)
       if (c.Name.Equals (name, StringComparison.OrdinalIgnoreCase) &&
           c.Domain.Equals (domain, StringComparison.OrdinalIgnoreCase) &&
           c.Path.Equals (path, StringComparison.Ordinal))
         yield return c;
 }
Exemplo n.º 5
0
    /// <summary>
    /// Adds the specified <paramref name="cookie"/> to the collection.
    /// </summary>
    /// <param name="cookie">
    /// A <see cref="Cookie"/> to add.
    /// </param>
    /// <exception cref="ArgumentNullException">
    /// <paramref name="cookie"/> is <see langword="null"/>.
    /// </exception>
    public void Add (Cookie cookie) 
    {
      if (cookie == null)
        throw new ArgumentNullException ("cookie");

      var pos = searchCookie (cookie);
      if (pos == -1) {
        _list.Add (cookie);
        return;
      }

      _list[pos] = cookie;
    }
Exemplo n.º 6
0
    /// <summary>
    /// Copies the elements of the collection to the specified array of <see cref="Cookie"/>,
    /// starting at the specified <paramref name="index"/> in the <paramref name="array"/>.
    /// </summary>
    /// <param name="array">
    /// An array of <see cref="Cookie"/> that represents the destination of the elements
    /// copied from the collection.
    /// </param>
    /// <param name="index">
    /// An <see cref="int"/> that represents the zero-based index in <paramref name="array"/>
    /// at which copying begins.
    /// </param>
    /// <exception cref="ArgumentNullException">
    /// <paramref name="array"/> is <see langword="null"/>.
    /// </exception>
    /// <exception cref="ArgumentOutOfRangeException">
    /// <paramref name="index"/> is less than zero.
    /// </exception>
    /// <exception cref="ArgumentException">
    /// The number of elements in the collection is greater than the available space from
    /// <paramref name="index"/> to the end of the destination <paramref name="array"/>.
    /// </exception>
    public void CopyTo (Cookie[] array, int index)
    {
      if (array == null)
        throw new ArgumentNullException ("array");

      if (index < 0)
        throw new ArgumentOutOfRangeException ("index", "Less than zero.");

      if (array.Length - index < _list.Count)
        throw new ArgumentException (
          "The number of elements in this collection is greater than the available space of the destination array.");

      _list.CopyTo (array, index);
    }
Exemplo n.º 7
0
    private int searchCookie (Cookie cookie)
    {
      var name = cookie.Name;
      var path = cookie.Path;
      var domain = cookie.Domain;
      var ver = cookie.Version;

      for (var i = _list.Count - 1; i >= 0; i--) {
        var c = _list[i];
        if (c.Name.Equals (name, StringComparison.InvariantCultureIgnoreCase) &&
            c.Path.Equals (path, StringComparison.InvariantCulture) &&
            c.Domain.Equals (domain, StringComparison.InvariantCultureIgnoreCase) &&
            c.Version == ver)
          return i;
      }

      return -1;
    }
Exemplo n.º 8
0
    internal void SetOrRemove (Cookie cookie)
    {
      var pos = searchCookie (cookie);
      if (pos == -1) {
        if (!cookie.Expired)
          _list.Add (cookie);

        return;
      }

      if (!cookie.Expired) {
        _list[pos] = cookie;
        return;
      }

      _list.RemoveAt (pos);
    }
Exemplo n.º 9
0
    private static CookieCollection parseResponse (string value)
    {
      var cookies = new CookieCollection ();

      Cookie cookie = null;
      var pairs = splitCookieHeaderValue (value);
      for (var i = 0; i < pairs.Length; i++) {
        var pair = pairs[i].Trim ();
        if (pair.Length == 0)
          continue;

        if (pair.StartsWith ("version", StringComparison.InvariantCultureIgnoreCase)) {
          if (cookie != null)
            cookie.Version = Int32.Parse (pair.GetValue ('=', true));
        }
        else if (pair.StartsWith ("expires", StringComparison.InvariantCultureIgnoreCase)) {
          var buff = new StringBuilder (pair.GetValue ('='), 32);
          if (i < pairs.Length - 1)
            buff.AppendFormat (", {0}", pairs[++i].Trim ());

          DateTime expires;
          if (!DateTime.TryParseExact (
            buff.ToString (),
            new[] { "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'", "r" },
            CultureInfo.CreateSpecificCulture ("en-US"),
            DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal,
            out expires))
            expires = DateTime.Now;

          if (cookie != null && cookie.Expires == DateTime.MinValue)
            cookie.Expires = expires.ToLocalTime ();
        }
        else if (pair.StartsWith ("max-age", StringComparison.InvariantCultureIgnoreCase)) {
          var max = Int32.Parse (pair.GetValue ('=', true));
          var expires = DateTime.Now.AddSeconds ((double) max);
          if (cookie != null)
            cookie.Expires = expires;
        }
        else if (pair.StartsWith ("path", StringComparison.InvariantCultureIgnoreCase)) {
          if (cookie != null)
            cookie.Path = pair.GetValue ('=');
        }
        else if (pair.StartsWith ("domain", StringComparison.InvariantCultureIgnoreCase)) {
          if (cookie != null)
            cookie.Domain = pair.GetValue ('=');
        }
        else if (pair.StartsWith ("port", StringComparison.InvariantCultureIgnoreCase)) {
          var port = pair.Equals ("port", StringComparison.InvariantCultureIgnoreCase)
                     ? "\"\""
                     : pair.GetValue ('=');

          if (cookie != null)
            cookie.Port = port;
        }
        else if (pair.StartsWith ("comment", StringComparison.InvariantCultureIgnoreCase)) {
          if (cookie != null)
            cookie.Comment = pair.GetValue ('=').UrlDecode ();
        }
        else if (pair.StartsWith ("commenturl", StringComparison.InvariantCultureIgnoreCase)) {
          if (cookie != null)
            cookie.CommentUri = pair.GetValue ('=', true).ToUri ();
        }
        else if (pair.StartsWith ("discard", StringComparison.InvariantCultureIgnoreCase)) {
          if (cookie != null)
            cookie.Discard = true;
        }
        else if (pair.StartsWith ("secure", StringComparison.InvariantCultureIgnoreCase)) {
          if (cookie != null)
            cookie.Secure = true;
        }
        else if (pair.StartsWith ("httponly", StringComparison.InvariantCultureIgnoreCase)) {
          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;
    }
Exemplo n.º 10
0
    private static CookieCollection parseRequest (string value)
    {
      var cookies = new CookieCollection ();

      Cookie cookie = null;
      var ver = 0;
      var pairs = splitCookieHeaderValue (value);
      for (var i = 0; i < pairs.Length; i++) {
        var pair = pairs[i].Trim ();
        if (pair.Length == 0)
          continue;

        if (pair.StartsWith ("$version", StringComparison.InvariantCultureIgnoreCase)) {
          ver = Int32.Parse (pair.GetValue ('=', true));
        }
        else if (pair.StartsWith ("$path", StringComparison.InvariantCultureIgnoreCase)) {
          if (cookie != null)
            cookie.Path = pair.GetValue ('=');
        }
        else if (pair.StartsWith ("$domain", StringComparison.InvariantCultureIgnoreCase)) {
          if (cookie != null)
            cookie.Domain = pair.GetValue ('=');
        }
        else if (pair.StartsWith ("$port", StringComparison.InvariantCultureIgnoreCase)) {
          var port = pair.Equals ("$port", StringComparison.InvariantCultureIgnoreCase)
                     ? "\"\""
                     : pair.GetValue ('=');

          if (cookie != null)
            cookie.Port = port;
        }
        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 (ver != 0)
            cookie.Version = ver;
        }
      }

      if (cookie != null)
        cookies.Add (cookie);

      return cookies;
    }
Exemplo n.º 11
0
 private static int compareCookieWithinSorted (Cookie x, Cookie y)
 {
   var ret = 0;
   return (ret = x.Version - y.Version) != 0
          ? ret
          : (ret = x.Name.CompareTo (y.Name)) != 0
            ? ret
            : y.Path.Length - x.Path.Length;
 }
Exemplo n.º 12
0
 private static int compareCookieWithinSort (Cookie x, Cookie y)
 {
   return (x.Name.Length + x.Value.Length) - (y.Name.Length + y.Value.Length);
 }
Exemplo n.º 13
0
    /// <summary>
    /// Sets an HTTP <paramref name="cookie"/> to send with the WebSocket connection request to
    /// the server.
    /// </summary>
    /// <param name="cookie">
    /// A <see cref="Cookie"/> that represents the cookie to send.
    /// </param>
    public void SetCookie (Cookie cookie)
    {
      lock (_forConn) {
        var msg = checkIfAvailable (false, false) ?? (cookie == null ? "'cookie' is null." : null);
        if (msg != null) {
          _logger.Error (msg);
          error ("An error has occurred in setting the cookie.", null);

          return;
        }

        lock (_cookies.SyncRoot)
          _cookies.SetOrRemove (cookie);
      }
    }