예제 #1
0
        public static IJsonRpcHandlerBuilder UseSecurityExceptionHandler(this IJsonRpcHandlerBuilder builder)
        {
            return(builder.Use(async(context, next) =>
            {
                try
                {
                    await next();
                }
                catch (Exception exception)
                {
                    var securityException = exception.Unwrap <SecurityException>();
                    if (securityException != null)
                    {
                        // This is wrong:
                        // 1) we are using exceptions for flow control
                        // 2) sematically `SecurityException` means more `Forbidden` than `Unauthorized`
                        // However - this get's the job done and I can't think of any better alternative without
                        // introducing ton of boilerplate code.

                        context.SetResponse(JsonRpcError.UNAUTHORIZED, errorData: securityException.ToDiagnosticString());
                    }
                    else
                    {
                        throw;
                    }
                }
            }));
        }
예제 #2
0
        public static IJsonRpcHandlerBuilder UseBlobHandler(this IJsonRpcHandlerBuilder builder)
        {
            return(builder.Use(async(context, next) =>
            {
                var httpContextAccessor = context.AppServices.GetRequiredService <IHttpContextAccessor>();

                IBlob HandleBlob(string path, string name)
                {
                    var form = httpContextAccessor.HttpContext?.Request?.Form;
                    if (form == null)
                    {
                        return null;
                    }

                    var file = form.Files[name];
                    if (file == null)
                    {
                        return null;
                    }

                    return new FormFileBlob(file);
                }

                using (new BlobReadHandler(HandleBlob))
                {
                    await next();
                }
            }));
        }
예제 #3
0
 public static IJsonRpcHandlerBuilder Use(this IJsonRpcHandlerBuilder builder, Func <JsonRpcContext, Func <Task>, Task> middleware)
 {
     return(builder.Use(next =>
     {
         return context =>
         {
             return middleware(context, () => next(context));
         };
     }));
 }
예제 #4
0
        public static void Run(this IJsonRpcHandlerBuilder builder, JsonRpcRequestDelegate handler)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            builder.Use(_ => handler);
        }
예제 #5
0
        public static IJsonRpcHandlerBuilder UseBlobHandler(this IJsonRpcHandlerBuilder builder)
        {
            return(builder.Use(async(context, next) =>
            {
#pragma warning disable CS0618

                var httpContextAccessor = context.AppServices.GetRequiredService <IHttpContextAccessor>();

                IBlob HandleBlob(string path, string name)
                {
                    var form = httpContextAccessor.HttpContext?.Request?.Form;
                    if (form == null)
                    {
                        return null;
                    }

                    var file = form.Files[name];
                    if (file == null)
                    {
                        return null;
                    }

                    return new FormFileBlob(file);
                }

                IStreamReference HandleReference(string path, string name)
                {
                    var form = httpContextAccessor.HttpContext?.Request?.Form;
                    if (form == null)
                    {
                        return null;
                    }

                    var file = form.Files[name];
                    if (file == null)
                    {
                        return null;
                    }

                    return new FormFileStreamReference(file);
                }

                using (new BlobReadHandler(HandleBlob))
                    using (new StreamReferenceReadHandler(HandleReference))
                    {
                        await next();
                    }

#pragma warning restore CS0618
            }));
        }