Skip to content

Commit 9fa33c3

Browse files
committed
Dead Link Checker: add a user-agent for Reddit
1 parent a62ced2 commit 9fa33c3

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

scripts/check-dead-links.fsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
open System
55
open System.Net
66
open System.Net.Http
7+
open System.Net.Http.Headers
78
open System.Threading.Tasks
89
open Markdig
910
open System.IO
@@ -39,6 +40,11 @@ let collectLinks node =
3940
let exclusionCodes = Map.ofArray [|
4041
"https://www.lkokemohr.de/fsharp_godot.html", HttpStatusCode.TooManyRequests // GitHub runner receives this often
4142
|]
43+
let userAgents = Map.ofArray [|
44+
// Reddit doesn't like when we access it without any user-agent header.
45+
// Note that this won't work for every other resource; e.g., X doesn't like whe we *pass* the user-agent.
46+
"https://www.reddit.com/r/fsharp/", "Mozilla/5.0 (compatible; +https://github.com/fsprojects/awesome-fsharp)"
47+
|]
4248

4349
let printLock = Object()
4450
let retryPipeline =
@@ -49,7 +55,13 @@ let checkLinkStatus (client: HttpClient) (url: string) = task {
4955
try
5056
lock printLock (fun () -> printfn $"Verifying link {url}…")
5157
let! response = retryPipeline.ExecuteAsync(
52-
fun _ -> ValueTask<HttpResponseMessage>(client.GetAsync url)
58+
fun _ -> ValueTask<HttpResponseMessage>(task {
59+
use request = new HttpRequestMessage(HttpMethod.Get, url)
60+
Map.tryFind url userAgents |> Option.iter(
61+
fun ua -> request.Headers.TryAddWithoutValidation("User-Agent", ua) |> ignore
62+
)
63+
return! client.SendAsync request
64+
})
5365
)
5466
if response.StatusCode <> HttpStatusCode.OK then
5567
return

0 commit comments

Comments
 (0)