コード例 #1
0
        private void OnOpen(object sender, Chromium.Event.CfxOpenEventArgs e)
        {
            var request  = e.Request;
            var callback = e.Callback;
            var uri      = new Uri(request.Url);


            var headers = request.GetHeaderMap().Select(x => new { Key = x[0], Value = x[1] }).ToList();

            var contentRange = headers.FirstOrDefault(x => x.Key.ToLower() == "range");


            if (contentRange != null)
            {
                var group = System.Text.RegularExpressions.Regex.Match(contentRange.Value, @"(?<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 (e.Request.PostData != null && e.Request.PostData.Elements != null)
            {
                var items = e.Request.PostData.Elements;
                var bytes = new List <byte>();
                foreach (var item in items)
                {
                    var size = (int)item.BytesCount;

                    var buffer = new byte[size];

                    var ptr = Marshal.AllocHGlobal(buffer.Length);


                    item.GetBytes(item.BytesCount, ptr);

                    Marshal.Copy(ptr, buffer, 0, size);



                    switch (item.Type)
                    {
                    case CfxPostdataElementType.Empty:
                        break;

                    case CfxPostdataElementType.Bytes:
                        bytes.AddRange(buffer);
                        break;

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

                    Marshal.FreeHGlobal(ptr);
                }
                postData = bytes.ToArray();
                bytes    = null;
            }

            var method = request.Method;

            var requestHeaders = new Dictionary <string, List <string> >();

            foreach (var header in headers)
            {
                if (!requestHeaders.ContainsKey(header.Key) || requestHeaders[header.Key] == null)
                {
                    requestHeaders[header.Key] = new List <string>();
                }
                var stringValues = requestHeaders[header.Key];
                stringValues.Add(header.Value);
            }

            var contentType = request.GetHeaderByName("Content-Type");


            var formiumRequest = new FormiumRequest(uri, method, requestHeaders.ToDictionary(k => k.Key.ToLower(), v => v.Value.ToArray()), postData, uploadFiles.ToArray());

            e.SetReturnValue(true);
            e.HandleRequest = false;

            cancellationTokenSource = new CancellationTokenSource();


            Task.Factory.StartNew(() =>
            {
                try
                {
                    Info.Add($" [{request.Method}] ".Magenta());
                    Info.Add($"{formiumRequest.RequestUrl}".Gray());

                    formiumResponse = GetResponse(formiumRequest);

                    if (formiumResponse.Status != (int)System.Net.HttpStatusCode.OK)
                    {
                        Info.Add($" [ERR]:{formiumResponse.Status}".Red());
                    }
                    else
                    {
                        Info.Add($"...".Gray());
                    }
                }
                catch
                {
                }
            }, cancellationTokenSource.Token).ContinueWith(t => callback.Continue());

            return;
        }
コード例 #2
0
        //abstract protected WebResourceStream GetResourceStream(string relativePath/*, Uri requestUri, string method, string postData*/);

        abstract protected FormiumResponse GetResponse(FormiumRequest request);