public IHttpHandler GetHandler(HttpContext httpContext, string requestType, string url, string pathTranslated)
        {
            ExTraceGlobals.OehCallTracer.TraceDebug(0L, "OwaEventHandlerFactory.GetHandler");
            string          queryStringParameter  = Utilities.GetQueryStringParameter(httpContext.Request, "ns");
            string          queryStringParameter2 = Utilities.GetQueryStringParameter(httpContext.Request, "ev");
            ISessionContext sessionContext        = OwaContext.Get(httpContext).SessionContext;

            ExTraceGlobals.OehDataTracer.TraceDebug <string, string>(0L, "Request namespace: '{0}', event: '{1}'", queryStringParameter, queryStringParameter2);
            OwaEventNamespaceAttribute owaEventNamespaceAttribute = OwaEventRegistry.FindNamespaceInfo(queryStringParameter);

            if (owaEventNamespaceAttribute == null)
            {
                throw new OwaInvalidRequestException(string.Format(CultureInfo.InvariantCulture, "Namespace '{0}' doesn't exist", new object[]
                {
                    queryStringParameter
                }), null, this);
            }
            if (sessionContext != null && !sessionContext.IsProxy && !sessionContext.AreFeaturesEnabled(owaEventNamespaceAttribute.SegmentationFlags))
            {
                Utilities.EndResponse(httpContext, HttpStatusCode.Forbidden);
                return(null);
            }
            OwaEventAttribute owaEventAttribute = owaEventNamespaceAttribute.FindEventInfo(queryStringParameter2);

            if (owaEventAttribute == null)
            {
                throw new OwaInvalidRequestException(string.Format(CultureInfo.InvariantCulture, "Event '{0}' doesn't exist", new object[]
                {
                    queryStringParameter2
                }), null, this);
            }
            if (Globals.OwaVDirType == OWAVDirType.Calendar && !owaEventAttribute.AllowAnonymousAccess)
            {
                Utilities.EndResponse(httpContext, HttpStatusCode.BadRequest);
                return(null);
            }
            if (sessionContext != null && !sessionContext.IsProxy && !sessionContext.AreFeaturesEnabled(owaEventAttribute.SegmentationFlags))
            {
                Utilities.EndResponse(httpContext, HttpStatusCode.Forbidden);
                return(null);
            }
            OwaEventVerb owaEventVerb = OwaEventVerbAttribute.Parse(httpContext.Request.HttpMethod);

            ExTraceGlobals.OehDataTracer.TraceDebug <string>(0L, "Request verb: {0}", httpContext.Request.HttpMethod);
            if ((owaEventAttribute.AllowedVerbs & owaEventVerb) == OwaEventVerb.Unsupported)
            {
                ExTraceGlobals.OehTracer.TraceDebug <OwaEventVerb, OwaEventVerb>(0L, "Verb is not allowed, returning 405. Actual verb: {0}. Allowed: {1}.", owaEventVerb, owaEventAttribute.AllowedVerbs);
                Utilities.EndResponse(httpContext, HttpStatusCode.MethodNotAllowed);
                return(null);
            }
            OwaEventHandlerBase owaEventHandlerBase = (OwaEventHandlerBase)Activator.CreateInstance(owaEventNamespaceAttribute.HandlerType);

            owaEventHandlerBase.EventInfo  = owaEventAttribute;
            owaEventHandlerBase.OwaContext = OwaContext.Current;
            owaEventHandlerBase.Verb       = owaEventVerb;
            if (Globals.CollectPerRequestPerformanceStats)
            {
                OwaContext.Current.OwaPerformanceData.SetOehRequestType(owaEventNamespaceAttribute.HandlerType.Name, owaEventAttribute.IsAsync ? owaEventAttribute.BeginMethodInfo.Name : owaEventAttribute.MethodInfo.Name);
            }
            if (owaEventAttribute.IsAsync)
            {
                OwaContext.Current.TryReleaseBudgetAndStopTiming();
                ExTraceGlobals.OehTracer.TraceDebug(0L, "Created async HTTP handler to server OEH request");
                OwaContext.Current.IsAsyncRequest = true;
                return(new OwaEventAsyncHttpHandler(owaEventHandlerBase));
            }
            ExTraceGlobals.OehTracer.TraceDebug(0L, "Created sync HTTP handler to serve OEH request");
            return(new OwaEventHttpHandler(owaEventHandlerBase));
        }
Ejemplo n.º 2
0
        private static void ScanHandlerAttributes(MethodInfo method, OwaEventAttribute eventInfo, Type objectIdType)
        {
            object[] customAttributes = method.GetCustomAttributes(typeof(OwaEventVerbAttribute), false);
            if (customAttributes != null && customAttributes.Length > 0)
            {
                OwaEventVerbAttribute owaEventVerbAttribute = (OwaEventVerbAttribute)customAttributes[0];
                eventInfo.AllowedVerbs = owaEventVerbAttribute.Verb;
            }
            else
            {
                eventInfo.AllowedVerbs = OwaEventVerb.Post;
            }
            customAttributes = method.GetCustomAttributes(typeof(OwaEventSegmentationAttribute), false);
            if (customAttributes != null && customAttributes.Length > 0)
            {
                OwaEventSegmentationAttribute owaEventSegmentationAttribute = (OwaEventSegmentationAttribute)customAttributes[0];
                eventInfo.SegmentationFlags = owaEventSegmentationAttribute.SegmentationFlags;
            }
            else
            {
                eventInfo.SegmentationFlags = 0UL;
            }
            ExTraceGlobals.OehDataTracer.TraceDebug <string, OwaEventVerb>(0L, "Event handler found. Name: '{0}'. Allowed verbs: '{1}'.", eventInfo.Name, eventInfo.AllowedVerbs);
            ulong num  = 0UL;
            int   num2 = 0;

            if (Globals.CanaryProtectionRequired)
            {
                eventInfo.AddParameterInfo(new OwaEventParameterAttribute("canary", typeof(string)));
            }
            customAttributes = method.GetCustomAttributes(typeof(OwaEventParameterAttribute), false);
            if (customAttributes != null && customAttributes.Length > 0)
            {
                if (customAttributes.Length >= 64)
                {
                    throw new OwaNotSupportedException("Event handler declares more parameters than allowed");
                }
                if ((eventInfo.AllowedVerbs & OwaEventVerb.Get) != OwaEventVerb.Unsupported && customAttributes.Length > 16)
                {
                    throw new OwaNotSupportedException("Event handler declares more parameters than allowed for a GET request");
                }
                foreach (OwaEventParameterAttribute owaEventParameterAttribute in customAttributes)
                {
                    if (objectIdType != null && owaEventParameterAttribute.Type == typeof(ObjectId))
                    {
                        owaEventParameterAttribute = new OwaEventParameterAttribute(owaEventParameterAttribute.Name, objectIdType, owaEventParameterAttribute.IsArray, owaEventParameterAttribute.IsOptional);
                    }
                    if (!eventInfo.IsInternal && !OwaEventRegistry.IsAllowedType(owaEventParameterAttribute.Type))
                    {
                        string message = string.Format("Event handler is using a type that is not supported method: '{0}' param type '{1}'", method.Name, owaEventParameterAttribute.Type);
                        throw new OwaNotSupportedException(message);
                    }
                    if (string.Equals(owaEventParameterAttribute.Name, "ns", StringComparison.Ordinal) || string.Equals(owaEventParameterAttribute.Name, "ev", StringComparison.Ordinal))
                    {
                        throw new OwaNotSupportedException("Handler is trying to use a reserve name for a parameter");
                    }
                    if (OwaEventRegistry.structTypeTable[owaEventParameterAttribute.Type] != null)
                    {
                        owaEventParameterAttribute.IsStruct = true;
                    }
                    owaEventParameterAttribute.ParameterMask = 1UL << num2;
                    if (!owaEventParameterAttribute.IsOptional)
                    {
                        num |= owaEventParameterAttribute.ParameterMask;
                    }
                    eventInfo.AddParameterInfo(owaEventParameterAttribute);
                    num2++;
                    ExTraceGlobals.OehDataTracer.TraceDebug(0L, "Event handler parameter found, name: '{0}', type: '{1}', isArray: '{2}', isOptional: '{3}', isStruct: '{4}'", new object[]
                    {
                        owaEventParameterAttribute.Name,
                        owaEventParameterAttribute.Type,
                        owaEventParameterAttribute.IsArray,
                        owaEventParameterAttribute.IsOptional,
                        owaEventParameterAttribute.IsStruct
                    });
                }
            }
            eventInfo.RequiredMask = num;
        }