From 2dd82aad13c5396da37180411403fef5c8b58719 Mon Sep 17 00:00:00 2001 From: Glax Date: Wed, 31 Jan 2024 18:46:37 +0100 Subject: [PATCH] https://datatracker.ietf.org/doc/html/rfc2616 --- Tranga/MangaConnectors/HttpDownloadClient.cs | 5 +---- Tranga/Server.cs | 7 +++++-- Tranga/TrangaSettings.cs | 10 ++++++++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Tranga/MangaConnectors/HttpDownloadClient.cs b/Tranga/MangaConnectors/HttpDownloadClient.cs index d5843f3..e2d9e3d 100644 --- a/Tranga/MangaConnectors/HttpDownloadClient.cs +++ b/Tranga/MangaConnectors/HttpDownloadClient.cs @@ -14,10 +14,7 @@ internal class HttpDownloadClient : DownloadClient public HttpDownloadClient(GlobalBase clone, Dictionary rateLimitRequestsPerMinute) : base(clone, rateLimitRequestsPerMinute) { - if (settings.customUserAgent is null || settings.customUserAgent.Length < 1) - Client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Tranga", "1.0")); - else - Client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", settings.customUserAgent); + Client.DefaultRequestHeaders.UserAgent.Add(settings.userAgent); } protected override RequestResult MakeRequestInternal(string url, string? referrer = null, string? clickButton = null) diff --git a/Tranga/Server.cs b/Tranga/Server.cs index bacb1da..6aa518f 100644 --- a/Tranga/Server.cs +++ b/Tranga/Server.cs @@ -200,6 +200,9 @@ public class Server : GlobalBase case "Settings": SendResponse(HttpStatusCode.OK, response, settings); break; + case "Settings/userAgent": + SendResponse(HttpStatusCode.OK, response, settings.userAgent); + break; case "NotificationConnectors": SendResponse(HttpStatusCode.OK, response, notificationConnectors); break; @@ -384,13 +387,13 @@ public class Server : GlobalBase settings.UpdateWorkingDirectory(workingDirectory); SendResponse(HttpStatusCode.Accepted, response); break;*/ - case "Settings/customUserAgent": + case "Settings/userAgent": if(!requestVariables.TryGetValue("userAgent", out string? customUserAgent)) { SendResponse(HttpStatusCode.BadRequest, response); break; } - settings.customUserAgent = customUserAgent; + settings.UpdateUserAgent(customUserAgent); SendResponse(HttpStatusCode.Accepted, response); break; case "Settings/customRequestLimit": diff --git a/Tranga/TrangaSettings.cs b/Tranga/TrangaSettings.cs index ad03f9b..d9bffd3 100644 --- a/Tranga/TrangaSettings.cs +++ b/Tranga/TrangaSettings.cs @@ -1,4 +1,5 @@ -using System.Runtime.InteropServices; +using System.Net.Http.Headers; +using System.Runtime.InteropServices; using Newtonsoft.Json; using Tranga.LibraryConnectors; using Tranga.NotificationConnectors; @@ -12,7 +13,7 @@ public class TrangaSettings public string workingDirectory { get; private set; } public int apiPortNumber { get; init; } public string styleSheet { get; private set; } - public string? customUserAgent { get; set; } = null; + public ProductInfoHeaderValue userAgent { get; set; } = new ("Tranga", "1.0"); [JsonIgnore] public string settingsFilePath => Path.Join(workingDirectory, "settings.json"); [JsonIgnore] public string libraryConnectorsFilePath => Path.Join(workingDirectory, "libraryConnectors.json"); [JsonIgnore] public string notificationConnectorsFilePath => Path.Join(workingDirectory, "notificationConnectors.json"); @@ -120,6 +121,11 @@ public class TrangaSettings ExportSettings(); } + public void UpdateUserAgent(string customUserAgent) + { + this.userAgent = new ProductInfoHeaderValue(ProductHeaderValue.Parse(customUserAgent)); + } + public void ExportSettings() { if (File.Exists(settingsFilePath))