// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Net.Http.Headers;
using System.Web.Http;
namespace System.Net.Http
{
///
/// Provides extension methods for the class.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public static class HttpRequestHeadersExtensions
{
private const string Cookie = "Cookie";
///
/// Gets any cookie headers present in the request. Each Cookie header is
/// represented as one instance. A
/// contains information about the domain, path, and other cookie information as well as one or
/// more instances. Each instance contains
/// a cookie name and whatever cookie state is associate with that name. The state is in the form of a
/// which on the wire is encoded as HTML Form URL-encoded data.
/// This representation allows for multiple related "cookies" to be carried within the same
/// Cookie header while still providing separation between each cookie state. A sample
/// Cookie header is shown below. In this example, there are two
/// with names stateA and stateB respectively. Further, each cookie state contains two name/value
/// pairs (name1/value1 and name2/value2) and (name3/value3 and name4/value4).
///
/// Cookie: stateA=name1=value1&name2=value2; stateB=name3=value3&name4=value4; domain=domain1; path=path1;
///
///
/// The request headers
/// A collection of instances.
public static Collection GetCookies(this HttpRequestHeaders headers)
{
if (headers == null)
{
throw Error.ArgumentNull("headers");
}
Collection result = new Collection();
IEnumerable cookieHeaders;
if (headers.TryGetValues(Cookie, out cookieHeaders))
{
foreach (string cookieHeader in cookieHeaders)
{
CookieHeaderValue cookieHeaderValue;
if (CookieHeaderValue.TryParse(cookieHeader, out cookieHeaderValue))
{
result.Add(cookieHeaderValue);
}
}
}
return result;
}
///
/// Gets any cookie headers present in the request which contains a with
/// a name that matches the provided . For example, if there are two Cookie
/// header fields looking like this:
///
/// Cookie: stateA=name1=value1&name2=value2; stateB=name3=value3&name4=value4; domain=domain1; path=path1;
/// Cookie: stateC=name5=value5&name6=value6; stateD=name7=value7&name8=value8; domain=domain2; path=path2;
///
/// and name is stateD then only the second Cookie header will be returned.
///
/// The request headers
/// The name of the to match.
/// A collection of instances with a matching .
public static Collection GetCookies(this HttpRequestHeaders headers, string name)
{
if (name == null)
{
throw Error.ArgumentNull("name");
}
IEnumerable cookieHeaderValues = GetCookies(headers);
CookieHeaderValue[] matches = cookieHeaderValues.Where(header => header.Cookies.Any(state => String.Equals(state.Name, name, StringComparison.OrdinalIgnoreCase))).ToArray();
return new Collection(matches);
}
}
}