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/utils/CryptoUtils.bs"
|
||||
import "pkg:/source/utils/Logging.bs"
|
||||
import "pkg:/source/utils/OptionUtils.bs"
|
||||
import "pkg:/source/utils/RegistryUtils.bs"
|
||||
import "pkg:/source/utils/StringUtils.bs"
|
||||
import "pkg:/source/utils/TimeUtils.bs"
|
||||
@@ -65,10 +66,7 @@ namespace Invidious
|
||||
end function
|
||||
|
||||
function GetInstance() as string
|
||||
if m.preferences = invalid
|
||||
m.preferences = m.invidiousNode.preferences
|
||||
end if
|
||||
instance = m.preferences["invidious.instance"]
|
||||
instance = m.GetInstanceFromPreferences()
|
||||
if not StringUtils.IsNullOrEmpty(instance)
|
||||
return instance
|
||||
end if
|
||||
@@ -82,6 +80,13 @@ namespace Invidious
|
||||
return instance
|
||||
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
|
||||
if StringUtils.IsNullOrEmpty(videoId)
|
||||
return
|
||||
@@ -258,7 +263,11 @@ namespace Invidious
|
||||
return feedSource
|
||||
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
|
||||
m.endpoints = m.invidiousNode.apiDefinitions
|
||||
end if
|
||||
@@ -334,7 +343,7 @@ namespace Invidious
|
||||
|
||||
request.Cancellation(cancellation)
|
||||
|
||||
response = request.Await()
|
||||
response = m.SendRequest(request, httpServer)
|
||||
|
||||
responseHandler = invalid
|
||||
if IsString(endpoint.responseHandler) and m.DoesExist(endpoint.responseHandler)
|
||||
@@ -362,6 +371,33 @@ namespace Invidious
|
||||
end if
|
||||
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
|
||||
response = context.response
|
||||
|
||||
|
||||
@@ -57,7 +57,10 @@ function VideoRowListRowContentTask(input as object) as object
|
||||
feedSource = service.MarkFeedPagination(feedContentNode)
|
||||
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 response.error = Invidious.ERROR_NOT_AUTHENTICATED
|
||||
|
||||
@@ -9,6 +9,7 @@ namespace Http
|
||||
function new(server as object)
|
||||
super()
|
||||
|
||||
m.server = server
|
||||
task = server.task
|
||||
m.profilesNode = task.profilesService
|
||||
m.invidiousNode = task.invidious
|
||||
@@ -72,12 +73,15 @@ namespace Http
|
||||
return true
|
||||
end if
|
||||
|
||||
additionalQueryParams = invalid
|
||||
options = {
|
||||
httpServer: m.server
|
||||
}
|
||||
if feedSource.state <> invalid and feedSource.state.queryParams <> invalid
|
||||
additionalQueryParams = feedSource.state.queryParams
|
||||
options.additionalQueryParams = feedSource.state.queryParams
|
||||
end if
|
||||
|
||||
invResponse = m.invidiousService.MakeRequest(feedSource, additionalQueryParams)
|
||||
invResponse = m.invidiousService.MakeRequest(feedSource, options)
|
||||
|
||||
if not invResponse.success
|
||||
response.Default(500, `Failed to make request: ${invResponse.error}`)
|
||||
return true
|
||||
|
||||
@@ -34,6 +34,15 @@ namespace Http
|
||||
m.headers = CreateObject("roAssociativeArray")
|
||||
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)
|
||||
if IsString(m.buffer)
|
||||
m.buffer = m.buffer + incoming
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace Http
|
||||
m.routes = []
|
||||
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
|
||||
if m.RouteMatchesRequest(route, request)
|
||||
try
|
||||
|
||||
@@ -195,6 +195,22 @@ namespace Http
|
||||
stats = stats + " Bytes" + Stri(m.total_in) + " in," + Stri(m.total_out) + " out"
|
||||
return stats
|
||||
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 namespace
|
||||
Reference in new issue
Block a user