feat: add User-Agent and other headers

This commit is contained in:
Anna 2022-08-28 05:19:15 -04:00
parent 03b1942473
commit 3be3c1d1e8
Signed by: anna
GPG Key ID: 0B391D8F06FCD9E0
3 changed files with 27 additions and 3 deletions

View File

@ -45,5 +45,12 @@ namespace ExpandedSearchInfo.Providers {
/// <param name="response">HTTP response from a Uri</param>
/// <returns>null if search info could not be extracted or the search info as a string if it could</returns>
Task<ISearchInfoSection?> ExtractInfo(HttpResponseMessage response);
/// <summary>
/// Modify any requests made for this provider before they are sent.
/// </summary>
/// <param name="request">HTTP request about to be sent</param>
void ModifyRequest(HttpRequestMessage request) {
}
}
}

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using ExpandedSearchInfo.Configs;
using ExpandedSearchInfo.Sections;
@ -38,5 +39,9 @@ namespace ExpandedSearchInfo.Providers {
var uri = response.RequestMessage!.RequestUri!;
return new TextSection(this, $"Text##{uri}", uri, info);
}
public void ModifyRequest(HttpRequestMessage request) {
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
}
}
}

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Dalamud.Game.Text.SeStringHandling;
using Dalamud.Logging;
@ -142,13 +143,19 @@ namespace ExpandedSearchInfo {
MaxAutomaticRedirections = 5,
};
handler.CookieContainer.Add(new Cookie("warning", "1", "/", "www.f-list.net"));
var client = new HttpClient(handler);
var version = this.GetType().Assembly.GetName().Version?.ToString(3) ?? "unknown";
var client = new HttpClient(handler) {
DefaultRequestHeaders = {
UserAgent = { new ProductInfoHeaderValue("ExpandedSearchInfo", version) },
},
};
var sections = new List<ISearchInfoSection>();
// run through each extracted uri
foreach (var uri in uris) {
if (uri.Scheme != "http" && uri.Scheme != "https") {
if (uri.Scheme is not ("http" or "https")) {
continue;
}
@ -163,7 +170,12 @@ namespace ExpandedSearchInfo {
}
// get the http response from the uri and make sure it's ok
var resp = await client.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead);
var req = new HttpRequestMessage(HttpMethod.Get, uri);
foreach (var provider in matching) {
provider.ModifyRequest(req);
}
var resp = await client.SendAsync(req);
if (resp.StatusCode != HttpStatusCode.OK) {
continue;
}