Пример #1
0
 protected override CefResourceHandler GetResourceHandler(CefBrowser browser, CefFrame frame, CefRequest request)
 {
     if (Guid.TryParse(request.GetHeaderByName("CefNet-Source"), out Guid sourceKey) &&
         _customSources.Remove(sourceKey, out CefResourceHandler resourceHandler))
     {
         return(resourceHandler);
     }
     return(base.GetResourceHandler(browser, frame, request));
 }
Пример #2
0
        protected override bool Open(CefRequest request, out bool handleRequest, CefCallback callback)
        {
            var uri     = new Uri(request.Url);
            var headers = request.GetHeaderMap();


            if (!string.IsNullOrEmpty(headers.Get("range")))
            {
                var rangeString = headers.Get("range");
                var group       = System.Text.RegularExpressions.Regex.Match(rangeString, @"(?<start>\d+)-(?<end>\d*)")?.Groups;
                if (group != null)
                {
                    if (!string.IsNullOrEmpty(group["start"].Value) && int.TryParse(group["start"].Value, out int startPos))
                    {
                        _buffStartPostition = startPos;
                    }

                    if (!string.IsNullOrEmpty(group["end"].Value) && int.TryParse(group["end"].Value, out int endPos))
                    {
                        _buffEndPostition = endPos;
                    }
                }
                _isPartContent = true;
            }

            _readStreamOffset = 0;

            if (_buffStartPostition.HasValue)
            {
                _readStreamOffset = _buffStartPostition.Value;
            }


            byte[] postData    = null;
            var    uploadFiles = new List <string>();

            if (request.PostData != null)
            {
                var items = request.PostData.GetElements();

                if (items != null && items.Length > 0)
                {
                    var bytes = new List <byte>();
                    foreach (var item in items)
                    {
                        var buffer = item.GetBytes();

                        //var size = (int)item.BytesCount;

                        switch (item.ElementType)
                        {
                        case CefPostDataElementType.Bytes:
                            bytes.AddRange(buffer);
                            break;

                        case CefPostDataElementType.File:
                            uploadFiles.Add(item.GetFile());
                            break;
                        }
                    }

                    postData = bytes.ToArray();
                    bytes    = null;
                }
            }

            var method = request.Method;

            var resourceRequest = new ResourceRequest(uri, method, headers, postData, uploadFiles.ToArray(), request);

            handleRequest = false;

            Task.Run(() =>
            {
                try
                {
                    Infos.Add($"[{request.Method}]");
                    Infos.Add($"{resourceRequest.RequestUrl}");

                    _resourceResponse = GetResourceResponse(resourceRequest);


                    if (_resourceResponse == null)
                    {
                        //callback.Cancel();
                        //return;

                        throw new NullReferenceException($"ResourceResponse should not be null.");
                    }


                    if (DisableCORS)
                    {
                        _resourceResponse.Headers.Set(ACCESS_CONTROL_ALLOW_HEADERS, "*");
                        _resourceResponse.Headers.Set(ACCESS_CONTROL_ALLOW_METHODS, "*");
                        _resourceResponse.Headers.Set(X_FRAME_OPTIONS, "ALLOWALL");

                        if (!string.IsNullOrEmpty(request.GetHeaderByName("origin")))
                        {
                            _resourceResponse.Headers.Set(ACCESS_CONTROL_ALLOW_ORIGIN, request.GetHeaderByName("origin"));
                            _resourceResponse.Headers.Set(ACCESS_CONTROL_MAX_AGE, "3600");
                        }
                        else
                        {
                            _resourceResponse.Headers.Set(ACCESS_CONTROL_ALLOW_ORIGIN, "*");
                        }
                    }



                    Infos.Add($"{(int)_resourceResponse.HttpStatus} {_resourceResponse.HttpStatus}");
                }
                catch (Exception ex)
                {
                    Logger.Error(ex, "ResourceHandler");
                    callback.Cancel();
                }
            }, _cancellationTokenSource.Token).ContinueWith(t => callback.Continue());

            return(true);
        }