Skip to content

Commit

Permalink
imp - Handle redirects
Browse files Browse the repository at this point in the history
---

Some websites, such as New York Times, may have URLs that link to their
RSS feeds of the latest articles. However, these URLs usually emit a 3xx
response, causing the RSS tool to have to get the URL that the redirection
points to. Before we've added a redirection handler, Nettify would emit
unhelpful errors, because the XML parser tried to parse an HTML document,
which is illegal. After the addition of the handler, URLs that generate
3xx responses now work.

---

Type: imp
Breaking: False
Doc Required: False
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Oct 23, 2024
1 parent c40271c commit 0ad10bb
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
69 changes: 69 additions & 0 deletions Nettify/Helpers/HttpGetRedirectingHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// Nettify Copyright (C) 2023-2024 Aptivi
//
// This file is part of Nettify
//
// Nettify is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Nettify is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace Nettify.Helpers
{
internal class HttpGetRedirectingHandler : DelegatingHandler
{
private static readonly HttpStatusCode[] redirectCodes =
[
HttpStatusCode.Moved,
HttpStatusCode.MovedPermanently,
HttpStatusCode.Found,
HttpStatusCode.Redirect,
HttpStatusCode.RedirectKeepVerb,
HttpStatusCode.RedirectMethod,
HttpStatusCode.SeeOther,
HttpStatusCode.TemporaryRedirect,
];

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
// Get a response.
var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);

// Check to see if we have a redirect
if (request.Method == HttpMethod.Get && redirectCodes.Contains(response.StatusCode))
{
// We are redirecting. Make a request on the new location
var location = response.Headers.Location;
HttpRequestMessage newRequest = new(HttpMethod.Get, location);
return await SendAsync(newRequest, cancellationToken);
}

// There is no redirect.
return response;
}

internal HttpGetRedirectingHandler() :
this(new HttpClientHandler())
{ }

internal HttpGetRedirectingHandler(HttpMessageHandler handler)
{
InnerHandler = handler;
}
}
}
3 changes: 2 additions & 1 deletion Nettify/Rss/RSSTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//

using HtmlAgilityPack;
using Nettify.Helpers;
using Nettify.Rss.Instance;
using System;
using System.Collections.Generic;
Expand All @@ -31,7 +32,7 @@ namespace Nettify.Rss
/// </summary>
public static class RSSTools
{
internal static HttpClient Client = new();
internal static HttpClient Client = new(new HttpGetRedirectingHandler());

/// <summary>
/// Make instances of RSS Article from feed node and type
Expand Down

0 comments on commit 0ad10bb

Please sign in to comment.