Example #1
0
    static string Dump(Uri url)
    {
        var fetcher = new WebUriFetcher();
        var result  = fetcher.Load(url);

        if (result.IsHtml)
        {
            try
            {
                var tmp = Path.GetTempFileName();
                File.WriteAllText(tmp, result.Page.Content);

                return(string.Format("Successfully dumped HTML contents of {0} to {1}", url, tmp));
            }
            catch (IOException ex)
            {
                return(ex.Message);
            }
        }
        else
        {
            if (result.Bytes.Success)
            {
                return(string.Format("Not HTML ({0}): {1}", result.Bytes.ContentType, url));
            }
            else
            {
                return(result.Bytes.Exception.Message);
            }
        }
    }
Example #2
0
        public TitlingResult WebInfo(TitlingRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            // TitlingRequest ensures that what we get passed is an absolute URI with a scheme we support. Most
            // importantly this relieves the individual handlers of checking for those conditions.


            foreach (var handler in preHtmlHandlers)
            {
                var result = handler(request);
                if (result != null)
                {
                    return(result);
                }
            }

            foreach (var instruction in urlInstructions)
            {
                if (instruction.Match(request.Uri))
                {
                    // Set options as per instructions.
                    urlFetcher.MaxSizeHtml         = instruction.FetchSize;
                    urlFetcher.FollowMetaRefreshes = instruction.FollowMetaRefreshes;

                    var result = urlFetcher.Load(request.Uri);
                    request.Resource = result.Page;

                    // HTML handling.
                    if (result.IsHtml)
                    {
                        return(HandleHtml(
                                   request, result.Page,
                                   instruction.Handler ?? GenericHandler
                                   ));
                    }
                    // Media/Binary handling.
                    if (ParseMedia && result.Bytes.Success)
                    {
                        return(BinaryHandler.BinaryToIrc(request, result.Bytes));
                    }

                    return(request.CreateResult(false));
                }
            }

            throw new InvalidOperationException(
                      "Reached end of method, this should not happen. There should be a catch-all in urlInstructions.");
        }