Process local http request (#599)
* Local http? * option utils * Fix for hanging web app * Lint fix --------- Co-authored-by: github-action linter <githubaction@githubaction.com>
This commit is contained in:
1 parent
fae875845a
commit
f3f9327c1c
8 files changed
+123
-11
No files matched your search
@@ -6,6 +6,7 @@ import "pkg:/source/AsyncTask/Tasks.bs"
|
|||||||
import "pkg:/source/services/HttpClient.bs"
|
import "pkg:/source/services/HttpClient.bs"
|
||||||
import "pkg:/source/utils/CryptoUtils.bs"
|
import "pkg:/source/utils/CryptoUtils.bs"
|
||||||
import "pkg:/source/utils/Logging.bs"
|
import "pkg:/source/utils/Logging.bs"
|
||||||
|
import "pkg:/source/utils/OptionUtils.bs"
|
||||||
import "pkg:/source/utils/RegistryUtils.bs"
|
import "pkg:/source/utils/RegistryUtils.bs"
|
||||||
import "pkg:/source/utils/StringUtils.bs"
|
import "pkg:/source/utils/StringUtils.bs"
|
||||||
import "pkg:/source/utils/TimeUtils.bs"
|
import "pkg:/source/utils/TimeUtils.bs"
|
||||||
@@ -65,10 +66,7 @@ namespace Invidious
|
|||||||
end function
|
end function
|
||||||
|
|
||||||
function GetInstance() as string
|
function GetInstance() as string
|
||||||
if m.preferences = invalid
|
instance = m.GetInstanceFromPreferences()
|
||||||
m.preferences = m.invidiousNode.preferences
|
|
||||||
end if
|
|
||||||
instance = m.preferences["invidious.instance"]
|
|
||||||
if not StringUtils.IsNullOrEmpty(instance)
|
if not StringUtils.IsNullOrEmpty(instance)
|
||||||
return instance
|
return instance
|
||||||
end if
|
end if
|
||||||
@@ -82,6 +80,13 @@ namespace Invidious
|
|||||||
return instance
|
return instance
|
||||||
end function
|
end function
|
||||||
|
|
||||||
|
function GetInstanceFromPreferences() as string
|
||||||
|
if m.preferences = invalid
|
||||||
|
m.preferences = m.invidiousNode.preferences
|
||||||
|
end if
|
||||||
|
return m.preferences["invidious.instance"]
|
||||||
|
end function
|
||||||
|
|
||||||
function MarkVideoWatched(videoId as dynamic) as void
|
function MarkVideoWatched(videoId as dynamic) as void
|
||||||
if StringUtils.IsNullOrEmpty(videoId)
|
if StringUtils.IsNullOrEmpty(videoId)
|
||||||
return
|
return
|
||||||
@@ -258,7 +263,11 @@ namespace Invidious
|
|||||||
return feedSource
|
return feedSource
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function MakeRequest(feedSource as object, additionalQueryParams = invalid as object, cancellation = invalid as object) as object
|
function MakeRequest(feedSource as object, options = invalid as dynamic) as object
|
||||||
|
additionalQueryParams = OptionUtils.Get(options, "additionalQueryParams")
|
||||||
|
cancellation = OptionUtils.Get(options, "cancellation")
|
||||||
|
httpServer = OptionUtils.Get(options, "httpServer")
|
||||||
|
|
||||||
if m.endpoints = invalid
|
if m.endpoints = invalid
|
||||||
m.endpoints = m.invidiousNode.apiDefinitions
|
m.endpoints = m.invidiousNode.apiDefinitions
|
||||||
end if
|
end if
|
||||||
@@ -334,7 +343,7 @@ namespace Invidious
|
|||||||
|
|
||||||
request.Cancellation(cancellation)
|
request.Cancellation(cancellation)
|
||||||
|
|
||||||
response = request.Await()
|
response = m.SendRequest(request, httpServer)
|
||||||
|
|
||||||
responseHandler = invalid
|
responseHandler = invalid
|
||||||
if IsString(endpoint.responseHandler) and m.DoesExist(endpoint.responseHandler)
|
if IsString(endpoint.responseHandler) and m.DoesExist(endpoint.responseHandler)
|
||||||
@@ -362,6 +371,33 @@ namespace Invidious
|
|||||||
end if
|
end if
|
||||||
end function
|
end function
|
||||||
|
|
||||||
|
function SendRequest(request as HttpClient.HttpRequest, httpServer as dynamic) as dynamic
|
||||||
|
if httpServer = invalid
|
||||||
|
return request.Await()
|
||||||
|
end if
|
||||||
|
|
||||||
|
isDefaultInstance = StringUtils.IsNullOrEmpty(m.GetInstanceFromPreferences())
|
||||||
|
if not isDefaultInstance
|
||||||
|
return request.Await()
|
||||||
|
end if
|
||||||
|
|
||||||
|
parsedUrl = UrlUtils.ParseUrl(request.BuildUrl())
|
||||||
|
method = request.GetMethod()
|
||||||
|
uri = parsedUrl.path
|
||||||
|
headers = request.GetHeaders()
|
||||||
|
body = ValidString(request.GetBody())
|
||||||
|
responseObj = httpServer.ProcessRequestLocal(method, uri, headers, body)
|
||||||
|
response = new HttpClient.HttpResponse(request, invalid)
|
||||||
|
response.OverrideStatusCode(responseObj.http_code)
|
||||||
|
response.OverrideHeaders(responseObj.headers)
|
||||||
|
body = ""
|
||||||
|
if responseObj.body <> invalid
|
||||||
|
body = responseObj.body.ToAsciiString()
|
||||||
|
end if
|
||||||
|
response.OverrideText(body)
|
||||||
|
return response
|
||||||
|
end function
|
||||||
|
|
||||||
function DefaultHandler(context as object) as object
|
function DefaultHandler(context as object) as object
|
||||||
response = context.response
|
response = context.response
|
||||||
|
|
||||||
|
|||||||
@@ -57,7 +57,10 @@ function VideoRowListRowContentTask(input as object) as object
|
|||||||
feedSource = service.MarkFeedPagination(feedContentNode)
|
feedSource = service.MarkFeedPagination(feedContentNode)
|
||||||
hadContinuation = not StringUtils.IsNullOrEmpty(feedSource.state.continuation)
|
hadContinuation = not StringUtils.IsNullOrEmpty(feedSource.state.continuation)
|
||||||
|
|
||||||
response = service.MakeRequest(feedSource, feedSource.state.queryParams, m.top.cancellation)
|
response = service.MakeRequest(feedSource, {
|
||||||
|
additionalQueryParams: feedSource.state.queryParams
|
||||||
|
cancellation: m.top.cancellation
|
||||||
|
})
|
||||||
|
|
||||||
if not response.success
|
if not response.success
|
||||||
if response.error = Invidious.ERROR_NOT_AUTHENTICATED
|
if response.error = Invidious.ERROR_NOT_AUTHENTICATED
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ namespace Http
|
|||||||
function new(server as object)
|
function new(server as object)
|
||||||
super()
|
super()
|
||||||
|
|
||||||
|
m.server = server
|
||||||
task = server.task
|
task = server.task
|
||||||
m.profilesNode = task.profilesService
|
m.profilesNode = task.profilesService
|
||||||
m.invidiousNode = task.invidious
|
m.invidiousNode = task.invidious
|
||||||
@@ -72,12 +73,15 @@ namespace Http
|
|||||||
return true
|
return true
|
||||||
end if
|
end if
|
||||||
|
|
||||||
additionalQueryParams = invalid
|
options = {
|
||||||
|
httpServer: m.server
|
||||||
|
}
|
||||||
if feedSource.state <> invalid and feedSource.state.queryParams <> invalid
|
if feedSource.state <> invalid and feedSource.state.queryParams <> invalid
|
||||||
additionalQueryParams = feedSource.state.queryParams
|
options.additionalQueryParams = feedSource.state.queryParams
|
||||||
end if
|
end if
|
||||||
|
|
||||||
invResponse = m.invidiousService.MakeRequest(feedSource, additionalQueryParams)
|
invResponse = m.invidiousService.MakeRequest(feedSource, options)
|
||||||
|
|
||||||
if not invResponse.success
|
if not invResponse.success
|
||||||
response.Default(500, `Failed to make request: ${invResponse.error}`)
|
response.Default(500, `Failed to make request: ${invResponse.error}`)
|
||||||
return true
|
return true
|
||||||
|
|||||||
@@ -34,6 +34,15 @@ namespace Http
|
|||||||
m.headers = CreateObject("roAssociativeArray")
|
m.headers = CreateObject("roAssociativeArray")
|
||||||
end function
|
end function
|
||||||
|
|
||||||
|
function FromParsedData(method as string, uri as string, headers as object, body as string)
|
||||||
|
m.method = method
|
||||||
|
m.uri = uri
|
||||||
|
m.ProcessOperation()
|
||||||
|
m.headers = headers
|
||||||
|
m.body = body
|
||||||
|
m.state = Http.HttpRequestState.DONE
|
||||||
|
end function
|
||||||
|
|
||||||
function Add(incoming as string, maybeUnicode as boolean)
|
function Add(incoming as string, maybeUnicode as boolean)
|
||||||
if IsString(m.buffer)
|
if IsString(m.buffer)
|
||||||
m.buffer = m.buffer + incoming
|
m.buffer = m.buffer + incoming
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ namespace Http
|
|||||||
m.routes = []
|
m.routes = []
|
||||||
end function
|
end function
|
||||||
|
|
||||||
private function OnRequest(request as object, response as HttpResponse, connection as HttpConnection) as boolean
|
function OnRequest(request as object, response as HttpResponse, connection as HttpConnection) as boolean
|
||||||
for each route in m.routes
|
for each route in m.routes
|
||||||
if m.RouteMatchesRequest(route, request)
|
if m.RouteMatchesRequest(route, request)
|
||||||
try
|
try
|
||||||
|
|||||||
@@ -195,6 +195,22 @@ namespace Http
|
|||||||
stats = stats + " Bytes" + Stri(m.total_in) + " in," + Stri(m.total_out) + " out"
|
stats = stats + " Bytes" + Stri(m.total_in) + " in," + Stri(m.total_out) + " out"
|
||||||
return stats
|
return stats
|
||||||
end function
|
end function
|
||||||
|
|
||||||
|
function ProcessRequestLocal(method as string, uri as string, headers as object, body as string) as object
|
||||||
|
LogInfo(`Processing request locally ${method} ${uri}`)
|
||||||
|
request = new HttpRequest()
|
||||||
|
request.FromParsedData(method, uri, headers, body)
|
||||||
|
|
||||||
|
response = new HttpResponse(request, m, m.settings)
|
||||||
|
response.Process({})
|
||||||
|
|
||||||
|
return {
|
||||||
|
http_code: response.http_code
|
||||||
|
headers: response.headers
|
||||||
|
body: response.body_buffer
|
||||||
|
}
|
||||||
|
end function
|
||||||
|
|
||||||
end class
|
end class
|
||||||
|
|
||||||
end namespace
|
end namespace
|
||||||
@@ -149,6 +149,10 @@ namespace HttpClient
|
|||||||
return m
|
return m
|
||||||
end function
|
end function
|
||||||
|
|
||||||
|
function GetMethod() as string
|
||||||
|
return m._method
|
||||||
|
end function
|
||||||
|
|
||||||
function Url(url as string) as HttpRequest
|
function Url(url as string) as HttpRequest
|
||||||
m._url = url
|
m._url = url
|
||||||
return m
|
return m
|
||||||
@@ -221,11 +225,22 @@ namespace HttpClient
|
|||||||
return m
|
return m
|
||||||
end function
|
end function
|
||||||
|
|
||||||
|
function GetHeaders() as object
|
||||||
|
if m._headers = invalid
|
||||||
|
m._headers = {}
|
||||||
|
end if
|
||||||
|
return m._headers
|
||||||
|
end function
|
||||||
|
|
||||||
function Body(body as dynamic) as HttpRequest
|
function Body(body as dynamic) as HttpRequest
|
||||||
m._body = ValidString(body)
|
m._body = ValidString(body)
|
||||||
return m
|
return m
|
||||||
end function
|
end function
|
||||||
|
|
||||||
|
function GetBody() as dynamic
|
||||||
|
return m._body
|
||||||
|
end function
|
||||||
|
|
||||||
function Json(body as object) as HttpRequest
|
function Json(body as object) as HttpRequest
|
||||||
m.Body(FormatJson(body))
|
m.Body(FormatJson(body))
|
||||||
m.Header("Content-Type", "application/json")
|
m.Header("Content-Type", "application/json")
|
||||||
@@ -612,6 +627,10 @@ namespace HttpClient
|
|||||||
m._json = json
|
m._json = json
|
||||||
end function
|
end function
|
||||||
|
|
||||||
|
function OverrideHeaders(headers as object)
|
||||||
|
m._headers = headers
|
||||||
|
end function
|
||||||
|
|
||||||
function OverrideErrorMessage(errorMessage as string)
|
function OverrideErrorMessage(errorMessage as string)
|
||||||
m._errorMessage = errorMessage
|
m._errorMessage = errorMessage
|
||||||
end function
|
end function
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
namespace OptionUtils
|
||||||
|
|
||||||
|
function Get(opts as object, key as string, passthroughFunction = invalid as dynamic) as dynamic
|
||||||
|
if opts = invalid
|
||||||
|
if passthroughFunction = invalid
|
||||||
|
return invalid
|
||||||
|
end if
|
||||||
|
return passthroughFunction(invalid)
|
||||||
|
end if
|
||||||
|
|
||||||
|
value = opts[key]
|
||||||
|
if value = invalid
|
||||||
|
if passthroughFunction = invalid
|
||||||
|
return invalid
|
||||||
|
end if
|
||||||
|
return passthroughFunction(invalid)
|
||||||
|
end if
|
||||||
|
|
||||||
|
if passthroughFunction <> invalid
|
||||||
|
return passthroughFunction(value)
|
||||||
|
end if
|
||||||
|
return value
|
||||||
|
end function
|
||||||
|
|
||||||
|
end namespace
|
||||||
Reference in new issue
Block a user