Beispiel #1
0
        private bool ProcessAssmblyEmbeddedFile(string url, string file, string fileAbsolutePath)
        {
            var scheme = _requestSchemeHandlerProvider?.GetScheme(url);

            if (scheme == null ||
                scheme.AssemblyOptions == null ||
                scheme.AssemblyOptions.TargetAssembly == null ||
                scheme.UrlSchemeType != UrlSchemeType.AssemblyResource)
            {
                return(false);
            }

            var option       = scheme.AssemblyOptions;
            var manifestName = string.Join(".", option.DefaultNamespace, option.RootFolder, _regex.Replace(fileAbsolutePath, ".")).Replace("..", ".").Replace("..", ".");
            var stream       = option.TargetAssembly.GetManifestResourceStream(manifestName);

            if (stream != null && stream.Length > 0)
            {
                stream.CopyTo(_chromelyResource.Content);
                string extension = Path.GetExtension(file);
                _chromelyResource.MimeType = MimeMapper.GetMimeType(extension);
                return(true);
            }

            return(false);
        }
        /// <summary>
        /// The process request async.
        /// </summary>
        /// <param name="request">
        /// The request.
        /// </param>
        /// <param name="callback">
        /// The callback.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public override CefReturnValue ProcessRequestAsync(IRequest request, ICallback callback)
        {
            var scheme = _requestSchemeHandlerProvider?.GetScheme(request.Url);

            if (scheme != null && scheme.UrlSchemeType == UrlSchemeType.LocalRquest)
            {
                _stream = null;
                var uri  = new Uri(request.Url);
                var path = uri.LocalPath;
                _mimeType = "application/json";

                bool isRequestAsync = _routeProvider.IsActionRouteAsync(path);
                if (isRequestAsync)
                {
                    ProcessRequestAsync(path);
                }
                else
                {
                    ProcessRequest(path);
                }
            }

            return(CefReturnValue.ContinueAsync);

            #region Process Request

            void ProcessRequest(string path)
            {
                Task.Run(() =>
                {
                    using (callback)
                    {
                        try
                        {
                            var response = new ChromelyResponse();
                            if (string.IsNullOrEmpty(path))
                            {
                                response.ReadyState = (int)ReadyState.ResponseIsReady;
                                response.Status     = (int)System.Net.HttpStatusCode.BadRequest;
                                response.StatusText = "Bad Request";

                                _chromelyResponse = response;
                            }
                            else
                            {
                                var parameters = request.Url.GetParameters();
                                var postData   = request.GetPostData();

                                _chromelyResponse = _requestTaskRunner.Run(path, parameters, postData);
                                string jsonData   = _serializerUtil.EnsureResponseDataIsJson(_chromelyResponse.Data);
                                var content       = Encoding.UTF8.GetBytes(jsonData);
                                _stream           = new MemoryStream();
                                _stream.Write(content, 0, content.Length);
                            }
                        }
                        catch (Exception exception)
                        {
                            _stream = null;
                            Logger.Instance.Log.LogError(exception, exception.Message);

                            _chromelyResponse =
                                new ChromelyResponse
                            {
                                Status = (int)HttpStatusCode.BadRequest,
                                Data   = "An error occured."
                            };
                        }

                        if (_stream == null)
                        {
                            callback.Cancel();
                        }
                        else
                        {
                            SetResponseInfoOnSuccess();
                            callback.Continue();
                        }
                    }
                });
            }

            #endregion

            #region Process Request Async

            void ProcessRequestAsync(string path)
            {
                Task.Run(async() =>
                {
                    using (callback)
                    {
                        try
                        {
                            var response = new ChromelyResponse();
                            if (string.IsNullOrEmpty(path))
                            {
                                response.ReadyState = (int)ReadyState.ResponseIsReady;
                                response.Status     = (int)System.Net.HttpStatusCode.BadRequest;
                                response.StatusText = "Bad Request";

                                _chromelyResponse = response;
                            }
                            else
                            {
                                var parameters = request.Url.GetParameters();
                                var postData   = request.GetPostData();

                                _chromelyResponse = await _requestTaskRunner.RunAsync(path, parameters, postData);
                                string jsonData   = _serializerUtil.EnsureResponseDataIsJson(_chromelyResponse.Data);
                                var content       = Encoding.UTF8.GetBytes(jsonData);
                                _stream           = new MemoryStream();
                                _stream.Write(content, 0, content.Length);
                            }
                        }
                        catch (Exception exception)
                        {
                            _stream = null;
                            Logger.Instance.Log.LogError(exception, exception.Message);

                            _chromelyResponse =
                                new ChromelyResponse
                            {
                                Status = (int)HttpStatusCode.BadRequest,
                                Data   = "An error occured."
                            };
                        }

                        if (_stream == null)
                        {
                            callback.Cancel();
                        }
                        else
                        {
                            SetResponseInfoOnSuccess();
                            callback.Continue();
                        }
                    }
                });
            }

            #endregion
        }