// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.ComponentModel;
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 HttpResponseHeadersExtensions
{
private const string SetCookie = "Set-Cookie";
///
/// Adds cookies to a response. Each Set-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 state1 and state2 respectively. Further, each cookie state contains two name/value
/// pairs (name1/value1 and name2/value2) and (name3/value3 and name4/value4).
///
/// Set-Cookie: state1:name1=value1&name2=value2; state2:name3=value3&name4=value4; domain=domain1; path=path1;
///
///
/// The response headers
/// The cookie values to add to the response.
public static void AddCookies(this HttpResponseHeaders headers, IEnumerable cookies)
{
if (headers == null)
{
throw Error.ArgumentNull("headers");
}
if (cookies == null)
{
throw Error.ArgumentNull("cookies");
}
foreach (CookieHeaderValue cookie in cookies)
{
if (cookie == null)
{
throw Error.Argument("cookies", Properties.Resources.CookieNull);
}
headers.TryAddWithoutValidation(SetCookie, cookie.ToString());
}
}
}
}