//
// System.Web.HttpVaryByParams.cs 
//
// Author:
//	Chris Toshok (toshok@novell.com)
//

//
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System.Collections;
using System.Security.Permissions;
using System.Text;

namespace System.Web {

	// CAS - no InheritanceDemand here as the class is sealed
	[AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
	public sealed class HttpCacheVaryByParams {

		bool ignore_parms;
		Hashtable parms;

		internal HttpCacheVaryByParams ()
		{
			/* the parameter names are meant to be case insensitive */
#if NET_2_0
			parms = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
#else
			parms = new Hashtable(CaseInsensitiveHashCodeProvider.Default,
					      CaseInsensitiveComparer.Default);
#endif
		}

		internal string[] GetParamNames ()
		{
			string[] names;

			names = new string [parms.Count];

			parms.Keys.CopyTo (names, 0);

			return names;
		}

		internal BaseResponseHeader GetResponseHeader ()
		{
			StringBuilder builder = new StringBuilder ("");

			foreach (string parm in parms.Keys) {
				builder.Append (parm);
				builder.Append ("; ");
			}

			if (builder.Length == 0)
				return null;

			return new UnknownResponseHeader ("Vary", builder.ToString());
		}

		public bool IgnoreParams {
			get {
				return ignore_parms;
			}
			set {
				ignore_parms = value;
			}
		}

		public bool this [ string param ] {
			get {
				if (param == null)
					throw new ArgumentNullException ();

				return parms.Contains (param);
			}
			set {
				if (param == null)
					throw new ArgumentNullException ();

				ignore_parms = false;
				if (value)
					if (!parms.Contains (param))
						parms.Add (param, true);
				else
					parms.Remove (param);
			}
		}
	}

}
