Ejemplo n.º 1
0
        private HttpContent GenerateFlareSolverrRequest(HttpRequestMessage request)
        {
            FlareSolverrRequest req;

            var url       = request.RequestUri.ToString();
            var userAgent = request.Headers.UserAgent.ToString();

            if (string.IsNullOrWhiteSpace(userAgent))
            {
                userAgent = null;
            }

            if (request.Method == HttpMethod.Get)
            {
                req = new FlareSolverrRequestGet
                {
                    Cmd        = "request.get",
                    Url        = url,
                    MaxTimeout = MaxTimeout,
                    UserAgent  = userAgent
                };
            }
            else if (request.Method == HttpMethod.Post)
            {
                var contentTypeType = request.Content.GetType();

                if (contentTypeType == typeof(FormUrlEncodedContent))
                {
                    var contentTypeValue = request.Content.Headers.ContentType.ToString();
                    var postData         = request.Content.ReadAsStringAsync().Result;

                    req = new FlareSolverrRequestPostUrlEncoded
                    {
                        Cmd      = "request.post",
                        Url      = url,
                        PostData = postData,
                        Headers  = new HeadersPost
                        {
                            ContentType = contentTypeValue,
                            // ContentLength will be filled automatically in Chrome
                            ContentLength = null
                        },
                        MaxTimeout = MaxTimeout,
                        UserAgent  = userAgent
                    };
                }
                else if (contentTypeType == typeof(MultipartFormDataContent))
                {
                    //TODO Implement - check if we just need to pass the content-type with the relevant headers
                    throw new FlareSolverrException("Unimplemented POST Content-Type: " + request.Content.Headers.ContentType);
                }
                else if (contentTypeType == typeof(StringContent))
                {
                    //TODO Implement - check if we just need to pass the content-type with the relevant headers
                    throw new FlareSolverrException("Unimplemented POST Content-Type: " + request.Content.Headers.ContentType);
                }
                else
                {
                    throw new FlareSolverrException("Unsupported POST Content-Type: " + request.Content.Headers.ContentType);
                }
            }
            else
            {
                throw new FlareSolverrException("Unsupported HttpMethod: " + request.Method);
            }

            var payload = JsonConvert.SerializeObject(req, new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            });
            HttpContent content = new StringContent(payload, Encoding.UTF8, "application/json");

            return(content);
        }
Ejemplo n.º 2
0
        private HttpRequest GenerateFlareSolverrRequest(HttpRequest request)
        {
            FlareSolverrRequest req;

            var url        = request.Url.ToString();
            var userAgent  = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36";
            var maxTimeout = Settings.RequestTimeout * 1000;

            if (request.Method == HttpMethod.Get)
            {
                req = new FlareSolverrRequestGet
                {
                    Cmd        = "request.get",
                    Url        = url,
                    MaxTimeout = maxTimeout,
                    UserAgent  = userAgent
                };
            }
            else if (request.Method == HttpMethod.Post)
            {
                var contentTypeType = request.Headers.ContentType;

                if (contentTypeType == "application/x-www-form-urlencoded")
                {
                    var contentTypeValue = request.Headers.ContentType.ToString();
                    var postData         = request.GetContent();

                    req = new FlareSolverrRequestPostUrlEncoded
                    {
                        Cmd      = "request.post",
                        Url      = url,
                        PostData = postData,
                        Headers  = new HeadersPost
                        {
                            ContentType   = contentTypeValue,
                            ContentLength = null
                        },
                        MaxTimeout = maxTimeout,
                        UserAgent  = userAgent
                    };
                }
                else if (contentTypeType.Contains("multipart/form-data"))
                {
                    //TODO Implement - check if we just need to pass the content-type with the relevant headers
                    throw new FlareSolverrException("Unimplemented POST Content-Type: " + request.Headers.ContentType);
                }
                else
                {
                    throw new FlareSolverrException("Unsupported POST Content-Type: " + request.Headers.ContentType);
                }
            }
            else
            {
                throw new FlareSolverrException("Unsupported HttpMethod: " + request.Method);
            }

            var apiUrl     = string.Format("{0}/v1", Settings.Host.TrimEnd('/'));
            var newRequest = new HttpRequest(apiUrl, HttpAccept.Json);

            newRequest.Headers.ContentType = "application/json";
            newRequest.Method = HttpMethod.Post;
            newRequest.SetContent(req.ToJson());

            _logger.Debug("Applying FlareSolverr Proxy {0} to request {1}", Name, request.Url);

            return(newRequest);
        }