void IEntryControllerBinding.SerializeToPath(IRouteAction action, IPathStack path)
		{
			if (Mappings != null)
				foreach (IMappingBinding mapping in this.Mappings)
					mapping.SerializeToPath(path);

			base.SerializeToPath(action, path);
		}
			public bool TryMapping(IHttpContext context, IPathStack path)
			{
				string[] actions = context.Request.Form.GetValues("action");
				if (actions != null)
					foreach (string action in actions)
						if (action.Equals(key, StringComparison.InvariantCultureIgnoreCase))
							return true;
				return context.Request.Form[key] != null;
			}
		public bool TryMapping(IHttpContext context, IPathStack path)
		{
			int index = path.Index;
			foreach (string keyword in keywords)
				if (!keyword.Equals(path.Pop(), StringComparison.CurrentCultureIgnoreCase))
				{
					path.ReverseToIndex(index);
					return false;
				}
			return true;
		}
		/*public override void Initialize(Type controllerType)
		{
			//if (Keyword == null) Keyword = new PlainPath(controllerType.Name.Length > 10 && controllerType.Name.EndsWith("Controller", StringComparison.InvariantCultureIgnoreCase) ? controllerType.Name.Substring(0, controllerType.Name.Length - 10) : controllerType.Name);

			base.Initialize(controllerType);
		}*/

		bool IEntryControllerBinding.TryBinding(IHttpContext context, IPathStack path, out IHttpHandler handler)
		{
			if (Mappings != null)
				foreach (IMappingBinding mapping in this.Mappings)
					if (!mapping.TryMapping(context, path))
					{
						handler = null;
						return false;
					}

			return TryBinding(context, path, out handler);
		}
        public override bool TryBinding(IHttpContext context, IPathStack path, object controller, out IHttpHandler handler)
        {
            if (path.IsAtEnd)
            {
                handler = null; return(false);
            }
            string method = path.Pop();

            if (!path.IsAtEnd)
            {
                handler = null; return(false);
            }

            IService service = controller as IService;

            if (service == null)
            {
                throw new Exception("ProtobufControllerAttribute can only be applied to classes implementing IService.");
            }

            var descriptor = service.DescriptorForType.FindMethodByName(method);

            if (descriptor == null)
            {
                handler = null; return(false);
            }

            IMessage request;

            string verb = context.Request.HttpMethod;

            if (verb == "GET")
            {
                request = null;
            }
            else if (verb == "POST" && context.Request.ContentType == CONTENT_TYPE)
            {
                request =
                    service.GetRequestPrototype(descriptor)
                    .WeakToBuilder()
                    .WeakMergeFrom(CodedInputStream.CreateInstance(context.Request.InputStream))
                    .WeakBuild();
            }
            else
            {
                handler = null;
                return(false);
            }

            handler = new ProtobufServiceHandler(service, descriptor, request);
            return(true);
        }
		/*public ParentActionAttribute() : base() { }
		protected ParentActionAttribute(IPathSection keyword) : base(keyword) { }
		public ParentActionAttribute(string keyword) : base(keyword) { }
		public ParentActionAttribute(string keywordResourceBaseName, string keywordResourceName) : base(keywordResourceBaseName, keywordResourceName) { }
		public ParentActionAttribute(string keywordResourceAssembly, string keywordResourceBaseName, string keywordResourceName) : base(keywordResourceAssembly, keywordResourceBaseName, keywordResourceName) { }
		*/
		public override bool TryBinding(IHttpContext context, IPathStack path)
		{
			object[] arguments;
			if (TryBinding(context, path, out arguments))
			{
				/*object controller = Activator.CreateInstance(controllerType);

				if (controllerExtensions != null)
					foreach (IControllerExtension ext in controllerExtensions)
						ext.OnPreAction(context);
				if (this.Extensions != null)
					foreach (IActionExtension ext in this.Extensions)
						ext.OnPreAction(context);

				try
				{
					controller = Method.Invoke(controller, arguments);
				}
				catch (Exception e)
				{
					bool catched = false;
					if (this.Extensions != null)
						foreach (IActionExtension ext in this.Extensions)
							if (!ext.OnError(context, e))
								catched = true;
					if (controllerExtensions != null)
						foreach (IControllerExtension ext in controllerExtensions)
							if (!ext.OnError(context, e))
								catched = true;
					if (!catched)
						throw e;
				}

				if (this.Extensions != null)
					foreach (IActionExtension ext in this.Extensions)
						ext.OnPostAction(context);
				if (controllerExtensions != null)
					foreach (IControllerExtension ext in controllerExtensions)
						ext.OnPostAction(context);
				*/

				/*
				Get ChildControllerAttribute
				Try Binding on it
				*/
				/*handler = null;
				return true;*/
			}
			handler = null;
			return false;
		}
		public bool TryBinding(IPathStack path, out IHttpHandler handler)
		{
			object value = Handler.ExecuteAction(Context, Controller, Arguments);
			
			if (value == null)
			{
				handler = null;
				return false;
			}
			else if (value is IRenderable || value is IViewTemplate)
			{
				handler = new RenderableHandler { Context = Context, Handler = Handler, Value = value };
			}
			else if ((handler = Context.Route.RoutingEngine.ParseRoute(Context, path, value)) == null)
			{
				return false;
			}
			return true;
		}
		public override bool TryBinding(IHttpContext context, IPathStack path, object controller, out IHttpHandler handler)
		{
			if (path.IsAtEnd){ handler = null; return false; }
			string method = path.Pop();
			if (!path.IsAtEnd){ handler = null; return false; }

			IService service = controller as IService;
			if (service == null) throw new Exception("ProtobufControllerAttribute can only be applied to classes implementing IService.");

			var descriptor = service.DescriptorForType.FindMethodByName(method);
			if (descriptor == null){ handler = null; return false; }

			IMessage request;

			string verb = context.Request.HttpMethod;
			if (verb == "GET")
			{
				request = null;
			}
			else if (verb == "POST" && context.Request.ContentType == CONTENT_TYPE)
			{
				request =
					service.GetRequestPrototype(descriptor)
					.WeakToBuilder()
					.WeakMergeFrom(CodedInputStream.CreateInstance(context.Request.InputStream))
					.WeakBuild();
			}
			else
			{
				handler = null;
				return false;
			}

			handler = new ProtobufServiceHandler(service, descriptor, request);
			return true;
		}
		public bool TryBinding(IHttpContext context, IPathStack path, out object obj, out int overloadWeight)
		{
			overloadWeight = 0;
			if (!TryBinding(context, out obj) && bindingType.IsValueType) obj = System.Activator.CreateInstance(bindingType);
			return true;
		}
		public void SerializeToPath(IPathStack path)
		{
			foreach (string keyword in keywords)
				path.Push(keyword);
		}
		public bool TryBinding(IHttpContext context, IPathStack path, out object obj, out int overloadWeight)
		{
			overloadWeight = 0;
			if (isPathStack) { obj = path; return true; }
			return TryBinding(context, out obj);
		}
Example #12
0
		protected override void SerializePath(IPathStack path, object value)
		{
		}
Example #13
0
		public void Push(IPathStack path)
		{
			if (_readOnly)
				throw new InvalidOperationException("This PathStack is read only.");

			if (path == null)
				throw new InvalidOperationException("The PathStack cannot Push null.");

			int index = path.Index;
			path.ReverseToIndex(0);
			while (!path.IsAtEnd)
				this.Push(path.Pop());

			this._trailingSlash = path.TrailingSlash;

			path.ReverseToIndex(index);

			QueryString.Add(path.QueryString);
		}
Example #14
0
		private bool TryBinding(IPathStack path, out object obj)
		{
			if (path.IsAtEnd)
			{
				obj = null;
				return false;
			}
			int index = path.Index;
			if (_emptyPath != null && _emptyPath.TryMapping(null, path))
			{
				obj = _emptyValueSet ? _emptyValue : (DefaultValue != null ? DefaultValue : (BindingTargetType.IsValueType ? Activator.CreateInstance(BindingTargetType) : null));
				return true;
			}
			path.ReverseToIndex(index);
			if (_deserializer != null)
			{
				return _deserializer(path, out obj);
			}
			else
			{
				string nextPath = path.Peek();
				if (Extension != null)
				{
					if (!nextPath.EndsWith(Extension, StringComparison.CurrentCultureIgnoreCase))
					{
						obj = null;
						return false;
					}
					nextPath = nextPath.Substring(0, nextPath.Length - Extension.Length);
				}
				bool f = SerializationHelper.TryDeserialize(nextPath, BindingTargetType, out obj);
				if (f)
				{
					path.Pop();
					return true;
				}
				else
					return false;
			}
		}
		private void SerializePath(IRouteAction action, IPathStack path, bool requireEntry)
		{
			IControllerBinding[] bindings = GetControllerBindings(action.ControllerType);
			if (bindings == null || bindings.Length == 0) throw new BindingException(String.Format("Type \"{0}\" is not bindable.", action.ControllerType.FullName));
			IPathStack bestStack = null;
			foreach (IControllerBinding b in bindings)
				if (!requireEntry || b is IEntryControllerBinding)
				{
					IPathStack trialStack = new PathStack(false);
					if (requireEntry)
						((IEntryControllerBinding)b).SerializeToPath(action, trialStack);
					else
						b.SerializeToPath(action, trialStack);

					IRouteAction childAction = action.ChildAction;
					if (childAction != null)
					{
						SerializePath(childAction, trialStack, false);
					}

					if (bestStack == null || trialStack.Index > bestStack.Index || (trialStack.Index == bestStack.Index && trialStack.QueryString.Count > bestStack.QueryString.Count))
						bestStack = trialStack;
				}
			if (bestStack != null)
			{
				path.Push(bestStack);
			}
			else
				throw new BindingException(String.Format("Type \"{0}\" is not a bindable EntryController.", action.ControllerType.FullName));
		}
Example #16
0
		public bool TryMapping(IHttpContext context, IPathStack path)
		{
			return String.Equals(context.Request.Headers[header], value);
		}
Example #17
0
			public void SerializeToPath(IPathStack path) { }
		public void SerializeAbsoutePath(IRouteAction action, IPathStack path)
		{
			SerializePath(action, path, true);
		}
 public override void SerializeToPath(IRouteAction action, IPathStack path)
 {
 }
		void IActionBinding.SerializePath(IPathStack path, object[] parameters)
		{
			if (Mappings != null)
				foreach (IMappingBinding mapping in Mappings)
					mapping.SerializeToPath(path);

			if (Bindings == null) return;

			int l = Bindings.Length;

			if (parameters.Length != l)
				throw new BindingException("Wrong number of parameters.");

			for (int i = 0; i < l; i++)
			{
				ParamBindings pb = Bindings[i];
				foreach(IParameterBinding b in pb.Bindings)
				{
					int index = path.Index;
					int count = path.QueryString.Count;
					b.SerializePath(path, parameters[i]);
					if (path.Index > index || path.QueryString.Count > count)
						break;
				}
			}
		}
		bool IActionBinding.TryBinding(IHttpContext context, IPathStack path, out object[] parameters, out int overloadWeight)
		{
			foreach (IMappingBinding mapping in this.Mappings)
				if (!mapping.TryMapping(context, path))
				{
					parameters = null;
					overloadWeight = 0;
					return false;
				}

			// Allow IExtension to affect mapping?
			//foreach (IExtension extension in this.Extensions)
			//    if (!extension.TryMapping(context, path))
			//        return false;

			overloadWeight = Mappings.Count * 10000;

			if (Bindings == null)
			{
				parameters = new object[0];
				return true;
			}

			int l = Bindings.Length;
			parameters = new object[Bindings.Length];
			for (int i = 0; i < l; i++)
			{
				ParamBindings pb = Bindings[i];
				if (pb.IsIn)
				{
					bool bound = false;
					int index = path.Index;
					foreach (IParameterBinding bindable in pb.Bindings)
					{
						object obj;
						int weight;
						if (bindable.TryBinding(context, path, out obj, out weight))
						{
							bool constrained = false;
							if (pb.Constraints != null)
								foreach (IBindingConstraint constraint in pb.Constraints)
									if (!constraint.TryConstraint(context, obj))
									{
										constrained = true;
										path.ReverseToIndex(index);
										break;
									}
							if (!constrained)
							{
								overloadWeight += weight;
								parameters[i] = obj;
								bound = true;
								break;
							}
						}
						else
						{
							path.ReverseToIndex(index);
						}
					}
					if (!bound)
					{
						overloadWeight = 0;
						parameters = null;
						return false;
					}
				}
			}
			return true;
		}
		public bool TryBinding(IHttpContext context, IPathStack path, out object obj, out int overloadWeight)
		{
			overloadWeight = 10;
			return TryBinding(context, out obj);
		}
Example #23
0
		protected override bool TryBinding(IHttpContext context, IPathStack path, out object value, out int overloadWeight)
		{
			overloadWeight = -path.Index;
			if ((PathBinding == null || PathBinding.TryMapping(context, path)) &&
				TryBinding(path, out value))
			{
				if (value == null)
					overloadWeight = 50;
				else if (value.GetType() == typeof(string)) overloadWeight = 100;
				else if (value.GetType() == typeof(char[])) overloadWeight = 100;
				else if (value.GetType() == typeof(char)) overloadWeight = 101;
				else if (value.GetType() == typeof(bool)) overloadWeight = 107;
				else if (value.GetType() == typeof(int)) overloadWeight = 108;
				else if (value.GetType() == typeof(float)) overloadWeight = 109;
				else if (value.GetType() == typeof(double)) overloadWeight = 110;
				else if (value.GetType() == typeof(DateTime)) overloadWeight = 115;
				else
				{
					overloadWeight += path.Index;
					overloadWeight *= 110;
				}
				if (Extension != null) overloadWeight += Extension.Length;
				return true;
			}
			value = null;
			overloadWeight = 0;
			return false;
		}
		public void SerializePath(IPathStack path, object obj)
		{
			// Exclude	
		}
		public virtual void SerializeToPath(IRouteAction action, IPathStack path)
		{
			// TODO: child action binding

			if (_handlers == null) return;

			IPathStack bestStack = null;
			foreach (ActionHandler handler in _handlers)
			{
				if (handler.Action == action.Method)
				{
					IPathStack trialStack = new PathStack(false);
					handler.Binding.SerializePath(trialStack, action.Parameters);

					if (action.ChildAction != null)
					{
						if (!(handler is ParentActionHandler)) throw new BindingException("Method is not a parent action and can't handle further calls.");
					}
					else
					{
						if (handler is ParentActionHandler) throw new BindingException("Method is a parent action. You should add the default sub-action.");
					}

					if (trialStack.Index == 0) trialStack.TrailingSlash = !DisableTrailingSlash;

					if (bestStack == null || trialStack.Index > bestStack.Index || (trialStack.Index == bestStack.Index && trialStack.QueryString.Count > bestStack.QueryString.Count))
						bestStack = trialStack;
				}
			}
			if (bestStack != null)
			{
				path.Push(bestStack);
			}
			else
				throw new BindingException(String.Format("Method \"{0}\" is not bindable.", action.Method.Name));
		}
 public override bool TryBinding(IHttpContext context, IPathStack path, out IHttpHandler handler)
 {
     // Entry controller not yet supported
     handler = null;
     return(false);
 }
Example #27
0
		protected override void SerializePath(IPathStack path, object value)
		{
			if (value == null || value as string == "" || value == DefaultValue)
			{
				_emptyPath.SerializeToPath(path);
				return;
			}

			IPathSerializable serializable = value as IPathSerializable;
			if (serializable != null)
				serializable.SerializeToPath(path);
			else
			{
				try
				{
					path.Push(SerializationHelper.Serialize(value) + Extension);
				}
				catch (Exception e)
				{
					throw new BindingException(e.Message, e);
				}
			}
		}
		public IHttpHandler ParseRoute(IHttpContext context, IPathStack path, object controller)
		{
			IControllerBinding[] bindings = GetControllerBindings(controller.GetType());
			if (bindings != null)
			{
				int index = path.Index;
				int controllerIndex = context.Route.Index;
				foreach (IControllerBinding binding in bindings)
				{
					IHttpHandler handler;
					if (binding.TryBinding(context, path, controller, out handler))
						return handler;
					else
					{
						context.Route.ReverseToIndex(controllerIndex);
						path.ReverseToIndex(index);
					}
				}
			}
			return null;
		}
		public virtual bool TryBinding(IHttpContext context, IPathStack path, out IHttpHandler handler)
		{
			return TryBinding(context, path, null, out handler);
		}
		public void SerializeRelativePath(IRouteAction action, IPathStack path)
		{
			SerializePath(action, path, false);
		}
		public virtual bool TryBinding(IHttpContext context, IPathStack path, object controller, out IHttpHandler handler)
		{
			if (_handlers == null)
			{
				handler = null;
				return false;
			}

			int bestIndex = -1;
			int bestOverloadWeight = -1;
			object[] bestParameters = null;
			ActionHandler bestHandler = null;

			int index = path.Index;

			object[] parameters;
			int overloadWeight;
			foreach (ActionHandler h in _handlers)
			{
				if (h.Binding.TryBinding(context, path, out parameters, out overloadWeight)
					&& (overloadWeight > bestOverloadWeight || (
						overloadWeight == bestOverloadWeight && parameters.Length < bestParameters.Length  //The fewer parameters the better if same overload weight
					)) && (path.IsAtEnd || h is ParentActionHandler))
				{
					bestIndex = path.Index;
					bestHandler = h;
					bestParameters = parameters;
					bestOverloadWeight = overloadWeight;
				}
				path.ReverseToIndex(index);
			}

			if (bestHandler == null)
			{
				handler = null;
				return false;
			}

			// shouldTrailSlash = bestIndex == index; I.e. the best mapping hasn't used a path
			if (bestIndex == index ^ path.TrailingSlash ^ DisableTrailingSlash && !(bestHandler is ParentActionHandler) && context.Request.HttpMethod == "GET")
			{
				// TODO: Make more efficient by simply extracting the final path section and use relative redirect: ../pathsection or ./pathsection/
				IPathStack newPath = new PathStack(false);
				newPath.Push(path);
				newPath.TrailingSlash = !path.TrailingSlash;
				context.Route.Dispose();
				context.Response.Redirect("~/" + newPath.ToString(), false);
				context.Response.StatusCode = 301;
				context.Response.End();
			}

			path.ReverseToIndex(bestIndex);

			if (controller == null) controller = _controllerCreator();

			context.Route.AddController(controller, index); // Controller is bound, add it to the route context

			handler = bestHandler.GetHttpHandler(context, controller, bestParameters);
			return true;
		}
		public IHttpHandler ParseRoute(IHttpContext context, IPathStack path)
        {
			if (_controllers == null)
				return null;

			foreach (IEntryControllerBinding c in _controllers)
			{
				IHttpHandler handler;
				int index = path.Index;
				context.Route.ReverseToIndex(-1);
				if (c.TryBinding(context, path, out handler))
				{
					while (handler is IParentActionHandler)
						if (!(handler as IParentActionHandler).TryBinding(path, out handler))
							return null;
					return handler;
				}
				else
					path.ReverseToIndex(index);
			}
			return null;
        }
		public override bool TryBinding(IHttpContext context, IPathStack path, out IHttpHandler handler)
		{
			// Entry controller not yet supported
			handler = null;
			return false;
		}