public object HandleAsync(string methodName, byte[] methodHash, object[] args)
        {
            var methodContext = context.GetMethodContext(methodHash.ToByteArray());

            if (methodContext == null)
            {
                throw new ArgumentNullException(nameof(methodContext));
            }

            var    methodInfo = typeof(TActAs).GetRuntimeMethod(methodName, args.Select(a => a.GetType()).ToArray());
            string result     = "";

            result += methodInfo.Name;
            foreach (var a in args)
            {
                result += a.ToString();
            }

            result += handlerArgument.Arg;

            var tcs = new TaskCompletionSource <string>();

            tcs.SetResult(result);

            var task = tcs.GetType().GetRuntimeMethod("get_Task", new Type[0]).Invoke(tcs, null);

            return(task);
        }
Example #2
0
        private static bool TryMatch(HttpContext context, IUriRouter uriRouter, bool logHttpRequests, IPAddress advertiseAsAddress, int advertiseAsPort)
        {
            var tcs        = new TaskCompletionSource <bool>();
            var httpEntity = new HttpEntity(context, logHttpRequests, advertiseAsAddress, advertiseAsPort,
                                            () => tcs.TrySetResult(true));

            var request = httpEntity.Request;

            try {
                var allMatches = uriRouter.GetAllUriMatches(request.Url);
                if (allMatches.Count == 0)
                {
                    NotFound(httpEntity);
                    return(false);
                }

                var allowedMethods = GetAllowedMethods(allMatches);

                if (request.HttpMethod.Equals(HttpMethod.Options, StringComparison.OrdinalIgnoreCase))
                {
                    RespondWithOptions(httpEntity, allowedMethods);
                    return(false);
                }

                var match = allMatches.LastOrDefault(
                    m => m.ControllerAction.HttpMethod.Equals(request.HttpMethod, StringComparison.OrdinalIgnoreCase));
                if (match == null)
                {
                    MethodNotAllowed(httpEntity, allowedMethods);
                    return(false);
                }

                ICodec requestCodec           = null;
                var    supportedRequestCodecs = match.ControllerAction.SupportedRequestCodecs;
                if (supportedRequestCodecs != null && supportedRequestCodecs.Length > 0)
                {
                    requestCodec = SelectRequestCodec(request.HttpMethod, request.ContentType, supportedRequestCodecs);
                    if (requestCodec == null)
                    {
                        BadContentType(httpEntity, "Invalid or missing Content-Type");
                        return(false);
                    }
                }

                ICodec responseCodec = SelectResponseCodec(request,
                                                           request.AcceptTypes,
                                                           match.ControllerAction.SupportedResponseCodecs,
                                                           match.ControllerAction.DefaultResponseCodec);
                if (responseCodec == null)
                {
                    BadCodec(httpEntity, "Requested URI is not available in requested format");
                    return(false);
                }
                try {
                    var manager =
                        httpEntity.CreateManager(requestCodec, responseCodec, allowedMethods, satisfied => { });
                    context.Items.Add(manager.GetType(), manager);
                    context.Items.Add(match.GetType(), match);
                    context.Items.Add(tcs.GetType(), tcs);
                    return(true);
                } catch (Exception exc) {
                    Log.Error(exc, "Error while handling HTTP request '{url}'.", request.Url);
                    InternalServerError(httpEntity);
                }
            } catch (Exception exc) {
                Log.Error(exc, "Unhandled exception while processing HTTP request at {url}.",
                          httpEntity.RequestedUrl);
                InternalServerError(httpEntity);
            }
            return(false);
        }