Пример #1
0
        public static Task <UrlSearchResult> SearchAsync([NotNull] this WebResponse thisValue, string searchFor, UrlSearchFlags flags, IOSettings settings = null, CancellationToken token = default(CancellationToken))
        {
            token.ThrowIfCancellationRequested();
            bool hasTitleFlag  = flags.FastHasFlag(UrlSearchFlags.Title);
            bool hasBufferFlag = flags.FastHasFlag(UrlSearchFlags.Buffer);
            bool hasSearchFlag = !string.IsNullOrEmpty(searchFor);

            UrlSearchResult result = new UrlSearchResult();

            try
            {
                Func <char[], int, bool> onRead;

                if (hasTitleFlag || hasBufferFlag || hasSearchFlag)
                {
                    StringBuilder    sb         = new StringBuilder();
                    StringComparison comparison = flags.FastHasFlag(UrlSearchFlags.IgnoreCase)
                                                        ? StringComparison.InvariantCultureIgnoreCase
                                                        : StringComparison.InvariantCulture;
                    onRead = (c, _) =>
                    {
                        if (result.Status == UrlSearchStatus.Unknown)
                        {
                            result.Status = UrlSearchStatus.Success;
                        }
                        sb.Append(c);

                        if (hasBufferFlag)
                        {
                            result.Buffer = sb.ToString();
                            hasBufferFlag = false;
                        }

                        token.ThrowIfCancellationRequested();
                        if (!hasTitleFlag && !hasSearchFlag)
                        {
                            return(false);
                        }

                        string contents = sb.ToString();

                        if (hasTitleFlag)
                        {
                            Match m = WebResponseHelper.TitleCheckExpression.Match(contents);

                            if (m.Success)
                            {
                                result.Title = m.Groups[1].Value;
                                hasTitleFlag = false;
                            }

                            if (hasTitleFlag && contents.Contains("</head>", StringComparison.OrdinalIgnoreCase))
                            {
                                hasTitleFlag = false;
                            }
                        }

                        if (hasSearchFlag)
                        {
                            // ReSharper disable once AssignNullToNotNullAttribute
                            if (contents.Contains(searchFor, comparison))
                            {
                                result.Status = UrlSearchStatus.Found;
                                hasSearchFlag = false;
                            }
                        }

                        token.ThrowIfCancellationRequested();
                        return(hasTitleFlag || hasSearchFlag || hasBufferFlag);
                    };
                }
                else
                {
                    onRead = (_, _) =>
                    {
                        token.ThrowIfCancellationRequested();
                        if (result.Status == UrlSearchStatus.Unknown)
                        {
                            result.Status = UrlSearchStatus.Success;
                        }
                        return(false);
                    };
                }

                IOReadSettings readSettings = new IOReadSettings(settings, onRead);
                if (!ReadAsync(thisValue, readSettings, token).Execute())
                {
                    result.Status = UrlSearchStatus.Failed;
                }
            }
            catch (WebException wex)
            {
                result.Status    = UrlSearchStatus.Unauthorized;
                result.Exception = wex;
            }
            catch (Exception ex)
            {
                result.Status    = UrlSearchStatus.Error;
                result.Exception = ex;
            }

            return(Task.FromResult(result));
        }
Пример #2
0
 public static Task <UrlSearchResult> SearchAsync([NotNull] this WebResponse thisValue, UrlSearchFlags flags, IOSettings settings = null, CancellationToken token = default(CancellationToken))
 {
     return(SearchAsync(thisValue, null, flags, settings, token));
 }
Пример #3
0
        public static Task <UrlSearchResult> SearchAsync([NotNull] this WebRequest thisValue, string searchFor, UrlSearchFlags flags, IOResponseSettings settings = null, CancellationToken token = default(CancellationToken))
        {
            token.ThrowIfCancellationRequested();
            return(GetResponseAsync(thisValue, settings, token)
                   .ContinueWith(task =>
            {
                WebResponse response = task.Result;

                if (response == null)
                {
                    return null;
                }

                try
                {
                    if (token.IsCancellationRequested)
                    {
                        return null;
                    }

                    if (settings?.OnResponseReceived != null)
                    {
                        if (!settings.OnResponseReceived(response))
                        {
                            return new UrlSearchResult {
                                Status = UrlSearchStatus.Failed
                            }
                        }
                        ;
                    }

                    UrlSearchResult result = response.SearchAsync(searchFor, flags, settings, token).Execute();
                    token.ThrowIfCancellationRequested();
                    result.RedirectUri = thisValue.RequestUri != response.ResponseUri
                                                                                                ? response.ResponseUri.IsAbsoluteUri || !thisValue.RequestUri.IsAbsoluteUri
                                                                                                        ? response.ResponseUri
                                                                                                        : new Uri(thisValue.RequestUri, response.ResponseUri)
                                                                                                : thisValue.RequestUri;
                    return result;
                }
                catch (Exception ex)
                {
                    settings?.OnError?.Invoke(ex);
                    return new UrlSearchResult
                    {
                        Status = UrlSearchStatus.Error,
                        Exception = ex
                    };
                }
                finally
                {
                    ObjectHelper.Dispose(ref response);
                    thisValue.Abort();
                }
            }, token, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Default));
        }
Пример #4
0
 public static UrlSearchResult Search([NotNull] this WebResponse thisValue, UrlSearchFlags flags, IOSettings settings = null)
 {
     return(Search(thisValue, null, flags, settings));
 }
Пример #5
0
        public static UrlSearchResult Search([NotNull] this WebRequest thisValue, string searchFor, UrlSearchFlags flags, IOResponseSettings settings = null)
        {
            WebResponse response = GetResponse(thisValue, settings);

            if (response == null)
            {
                return new UrlSearchResult {
                           Status = UrlSearchStatus.Failed
                }
            }
            ;

            try
            {
                if (settings?.OnResponseReceived != null)
                {
                    if (!settings.OnResponseReceived(response))
                    {
                        return new UrlSearchResult {
                                   Status = UrlSearchStatus.Failed
                        }
                    }
                    ;
                }

                UrlSearchResult result = response.Search(searchFor, flags, settings);

                result.RedirectUri = thisValue.RequestUri != response.ResponseUri
                                        ? response.ResponseUri.IsAbsoluteUri || !thisValue.RequestUri.IsAbsoluteUri
                                                ? response.ResponseUri
                                                : new Uri(thisValue.RequestUri, response.ResponseUri)
                                        : thisValue.RequestUri;
                return(result);
            }
            catch (Exception ex)
            {
                settings?.OnError?.Invoke(ex);
                return(new UrlSearchResult
                {
                    Status = UrlSearchStatus.Error,
                    Exception = ex
                });
            }
            finally
            {
                ObjectHelper.Dispose(ref response);
                thisValue.Abort();
            }
        }