From 86d57109691ea6a9a5cf646e38ba68a47f64e846 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Sun, 7 Jan 2024 11:17:05 -0500 Subject: [PATCH] Fix `HTTP/0.9 when not allowed` errors (#253) * Fix `HTTP/0.9 when not allowed` errors * tweak retry callback * throw ex in debug mode --- CHANGELOG.md | 4 +++ .../Services/Invidious/InvidiousService.bs | 15 ++++++++++ playlet-lib/src/source/services/HttpClient.bs | 30 ++++++++++++++++++- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index caef21cb..6d7da66c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - A home screen editor allowing to enable/disable feeds, and change their order. - The editor can be accessed from the Settings screen. +### Fixed + +- `HTTP/0.9 when not allowed` errors by adding a retry to web requests + ## [0.18.1] - 2024-01-02 ### Fixed diff --git a/playlet-lib/src/components/Services/Invidious/InvidiousService.bs b/playlet-lib/src/components/Services/Invidious/InvidiousService.bs index 1cd437a5..f85e43a6 100644 --- a/playlet-lib/src/components/Services/Invidious/InvidiousService.bs +++ b/playlet-lib/src/components/Services/Invidious/InvidiousService.bs @@ -123,6 +123,21 @@ namespace Invidious request.CacheSeconds(60 * 60 * 5) request.Cancellation(cancellation) + request.TryCount(2) + request.RetryCallback(function(context as object) as boolean + response = context.response + ' In some cases, video info requests return "HTTP/0.9 when not allowed" errors. + ' Retrying should usually fix the issue. + if response.StatusCode() = HttpClient.CurlErrors.CURLE_UNSUPPORTED_PROTOCOL + errorMessage = response.ErrorMessage() + if errorMessage.Contains("HTTP/0.9") + return true + end if + end if + + return false + end function) + return request.Await() end function diff --git a/playlet-lib/src/source/services/HttpClient.bs b/playlet-lib/src/source/services/HttpClient.bs index 1943848f..3bd8a6da 100644 --- a/playlet-lib/src/source/services/HttpClient.bs +++ b/playlet-lib/src/source/services/HttpClient.bs @@ -1,6 +1,7 @@ #const DISABLE_CACHE = false import "pkg:/source/utils/CryptoUtils.bs" +import "pkg:/source/utils/ErrorUtils.bs" import "pkg:/source/utils/Logging.bs" import "pkg:/source/utils/StringUtils.bs" import "pkg:/source/utils/Types.bs" @@ -237,6 +238,11 @@ namespace HttpClient return m end function + function RetryCallback(callback as function) as HttpRequest + m._retryCallback = callback + return m + end function + function NoCache() as HttpRequest m._noCache = true return m @@ -368,7 +374,7 @@ namespace HttpClient response = invalid response = m.AwaitInternal() - if response.IsSuccess() or response.TimedOut() or response.IsCancelled() or response.StatusCode() < 500 + if response.IsSuccess() or response.TimedOut() or response.IsCancelled() return response end if triesLeft -= 1 @@ -378,6 +384,28 @@ namespace HttpClient tryNumber = m._tryCount - triesLeft + 1 LogInfo("Request failed ID:", m.urlTransfer.GetIdentity(), "ErrorMessage:", response.ErrorMessage(), `\nRetrying: (`, tryNumber, "/", m._tryCount, ") in", backoffMs, "ms") + if IsFunction(m._retryCallback) + try + shouldRetry = m._retryCallback({ + tryNumber: tryNumber + tryCount: m._tryCount + request: m + response: response + }) + + if IsBool(shouldRetry) and not shouldRetry + LogInfo("Retry callback returned false. Not retrying.") + return response + end if + catch error + #if DEBUG + throw error + #else + LogError("Retry callback failed:", ErrorUtils.Format(error)) + #end if + end try + end if + m.urlTransfer = invalid m._cache = invalid m._sent = invalid