123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
-
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Text;
- namespace Aliyun.OSS
- {
-
-
-
- public enum MatchMode
- {
-
-
-
- Unknown,
-
-
-
- Exact,
-
-
-
- StartWith,
-
-
-
- Range
- };
-
-
-
- internal enum TupleType
- {
- Unknown,
- Two,
- Three
- };
-
-
-
- internal abstract class AbstractConditionItem
- {
- public string Name { get; set; }
- public MatchMode MatchMode { get; set; }
- public TupleType TupleType { get; set; }
- protected AbstractConditionItem(string name, MatchMode matchMode, TupleType tupleType)
- {
- Name = name;
- MatchMode = matchMode;
- TupleType = tupleType;
- }
- public abstract string Jsonize();
- }
-
-
-
- internal class EqualConditionItem : AbstractConditionItem
- {
- public string Value { get; set; }
- public EqualConditionItem(string name, string value)
- : this(name, value, TupleType.Two)
- { }
- public EqualConditionItem(string name, string value, TupleType tupleType)
- : base(name, MatchMode.Exact, tupleType)
- {
- Value = value;
- }
- public override string Jsonize()
- {
- string jsonizedCond = null;
- switch (TupleType)
- {
- case TupleType.Two:
- jsonizedCond = String.Format("{{\"{0}\":\"{1}\"}},", Name, Value);
- break;
- case TupleType.Three:
- jsonizedCond = String.Format("[\"eq\",\"${0}\",\"{1}\"],", Name, Value);
- break;
- default:
- throw new InvalidEnumArgumentException("Invalid tuple type " + TupleType.ToString());
- }
- return jsonizedCond;
- }
- }
-
-
-
- internal class StartWithConditionItem : AbstractConditionItem
- {
- public string Value { get; set; }
- public StartWithConditionItem(string name, string value)
- : base(name, MatchMode.StartWith, TupleType.Three)
- {
- Value = value;
- }
- public override string Jsonize()
- {
- return String.Format("[\"starts-with\",\"${0}\",\"{1}\"],", Name, Value);
- }
- }
-
-
-
- internal class RangeConditionItem : AbstractConditionItem
- {
- public long Minimum { get; set; }
- public long Maximum { get; set; }
- public RangeConditionItem(string name, long min, long max)
- : base(name, MatchMode.Range, TupleType.Three)
- {
- Minimum = min;
- Maximum = max;
- }
- public override string Jsonize()
- {
- return String.Format("[\"content-length-range\",{0},{1}],", Minimum, Maximum);
- }
- }
-
-
-
- public class PolicyConditions
- {
-
-
-
- public const string CondContentLengthRange = "content-length-range";
-
-
-
- public const string CondCacheControl = "Cache-Control";
-
-
-
- public const string CondContentType = "Content-Type";
-
-
-
- public const string CondContentDisposition = "Content-Disposition";
-
-
-
- public const string CondContentEncoding = "Content-Encoding";
-
-
-
- public const string CondExpires = "Expires";
-
-
-
- public const string CondKey = "key";
-
-
-
- public const string CondSuccessActionRedirect = "success_action_redirect";
-
-
-
- public const string CondSuccessActionStatus = "success_action_status";
-
-
-
- public const string CondXOssMetaPrefix = "x-oss-meta-";
- private IList<AbstractConditionItem> _conds = new List<AbstractConditionItem>();
-
-
-
-
-
- public void AddConditionItem(string name, string value)
- {
- MatchRuleChecker.Check(MatchMode.Exact, name);
- _conds.Add(new EqualConditionItem(name, value));
- }
-
-
-
-
-
-
- public void AddConditionItem(MatchMode matchMode, string name, string value)
- {
- MatchRuleChecker.Check(matchMode, name);
- switch (matchMode)
- {
- case MatchMode.Exact:
- _conds.Add(new EqualConditionItem(name, value, TupleType.Three));
- break;
-
- case MatchMode.StartWith:
- _conds.Add(new StartWithConditionItem(name, value));
- break;
- default:
- throw new InvalidEnumArgumentException("Unsupported match mode " +
- matchMode);
- }
- }
-
-
-
-
-
-
- public void AddConditionItem(string name, long min, long max)
- {
- if (min > max)
- throw new ArgumentException(String.Format("Invalid range [{0}, {1}].", min, max));
- _conds.Add(new RangeConditionItem(name, min, max));
- }
- internal string Jsonize()
- {
- var jsonizedConds = new StringBuilder();
- jsonizedConds.Append("\"conditions\":[");
- foreach (var cond in _conds)
- jsonizedConds.Append(cond.Jsonize());
- if (_conds.Count > 0)
- jsonizedConds.Remove(jsonizedConds.Length - 1, 1);
- jsonizedConds.Append("]");
- return jsonizedConds.ToString();
- }
- }
- internal static class MatchRuleChecker
- {
- private static IDictionary<string, IList<MatchMode>> _supportedMatchRules
- = new Dictionary<string, IList<MatchMode>>();
- static MatchRuleChecker()
- {
- var ordinaryMatchModes = new List<MatchMode> {MatchMode.Exact, MatchMode.StartWith};
- var specialMatchModes = new List<MatchMode> {MatchMode.Range};
- _supportedMatchRules.Add(PolicyConditions.CondContentLengthRange, specialMatchModes);
- _supportedMatchRules.Add(PolicyConditions.CondCacheControl, ordinaryMatchModes);
- _supportedMatchRules.Add(PolicyConditions.CondContentType, ordinaryMatchModes);
- _supportedMatchRules.Add(PolicyConditions.CondContentDisposition, ordinaryMatchModes);
- _supportedMatchRules.Add(PolicyConditions.CondContentEncoding, ordinaryMatchModes);
- _supportedMatchRules.Add(PolicyConditions.CondExpires, ordinaryMatchModes);
- _supportedMatchRules.Add(PolicyConditions.CondKey, ordinaryMatchModes);
- _supportedMatchRules.Add(PolicyConditions.CondSuccessActionRedirect, ordinaryMatchModes);
- _supportedMatchRules.Add(PolicyConditions.CondSuccessActionStatus, ordinaryMatchModes);
- _supportedMatchRules.Add(PolicyConditions.CondXOssMetaPrefix, ordinaryMatchModes);
- }
- public static void Check(MatchMode matchMode, string condName)
- {
- if (_supportedMatchRules.ContainsKey(condName))
- {
- var mms = _supportedMatchRules[condName];
- if (!mms.Contains(matchMode))
- {
- throw new ArgumentException(
- String.Format("Unsupported match mode for condition item {0}", condName));
- }
- }
- }
- }
- }
|