handle redirect in lounge code

This commit is contained in:
Brahim Hadriche committed 2026-07-01 18:17:31 -04:00
1 parent 38bfa1a102
commit 0f2a1a2b16
6 files changed
+162 -5

No files matched your search

@@ -6,6 +6,7 @@ enum HttpDecoderState
ChunkSize
ChunkData
Done
Redirect
Error
end enum
@@ -54,6 +55,14 @@ class HttpChunkedDecoder
return m.state = HttpDecoderState.Error
end function
function IsRedirect() as boolean
return m.state = HttpDecoderState.Redirect
end function
function Location() as dynamic
return m.headers["location"]
end function
private function ReceiveFrom(socket as object) as integer
available = socket.GetCountRcvBuf()
if available <= 0
@@ -157,6 +166,11 @@ class HttpChunkedDecoder
end function
private function FinishHead() as boolean
if m.IsRedirectStatus(m.statusCode)
m.state = HttpDecoderState.Redirect
return false
end if
if m.statusCode <> 200
m.state = HttpDecoderState.Error
return false
@@ -174,6 +188,10 @@ class HttpChunkedDecoder
return true
end function
private function IsRedirectStatus(code as integer) as boolean
return code = 301 or code = 302 or code = 303 or code = 307 or code = 308
end function
private function ParseChunkSize() as boolean
' Skip the CRLF trailing the previous chunk's data (and any stray newlines).
while m.cursor < m.buffer.Count() and (m.buffer[m.cursor] = 13 or m.buffer[m.cursor] = 10)
@@ -277,9 +277,9 @@ class LoungeApi
function GetCloseInfo() as object
if m.backchannel = invalid
return { statusCode: 0, reason: "" }
return { statusCode: 0, reason: "", target: "" }
end if
return { statusCode: m.backchannel.statusCode, reason: m.backchannel.closeReason }
return { statusCode: m.backchannel.statusCode, reason: m.backchannel.closeReason, target: m.backchannel.redirectTarget }
end function
function RefreshToken() as dynamic
@@ -7,6 +7,7 @@ import "pkg:/source/utils/UrlUtils.bs"
' Server sends periodic noop keepalives, so no data for this long means a dead connection.
const LOUNGE_BACKCHANNEL_TIMEOUT_MS = 60000
const LOUNGE_BACKCHANNEL_BODY_COMPACT = 32768
const LOUNGE_BACKCHANNEL_MAX_REDIRECTS = 10
' Raw socket on plain HTTP: roUrlTransfer can't read a streaming body incrementally, and
' roStreamSocket has no TLS.
@@ -14,6 +15,7 @@ class LoungeBackchannel
public statusCode = 0
public closeReason = ""
public redirectTarget = ""
private socket
private decoder
@@ -21,12 +23,28 @@ class LoungeBackchannel
private bodyCursor = 0
private timer
private url = ""
private messagePort
private redirectCount = 0
function new()
m.timer = CreateObject("roTimespan")
end function
function Open(url as string, messagePort as object) as boolean
m.messagePort = messagePort
m.redirectCount = 0
m.redirectTarget = ""
return m.Connect(url)
end function
private function Connect(url as string) as boolean
' A redirect or reconnect reuses Connect; close the previous socket so it doesn't leak.
if m.socket <> invalid
m.socket.NotifyReadable(false)
m.socket.Close()
m.socket = invalid
end if
m.url = url
m.statusCode = 0
m.closeReason = ""
@@ -62,7 +80,7 @@ class LoungeBackchannel
socket.SendStr(request)
socket.NotifyReadable(true)
socket.SetMessagePort(messagePort)
socket.SetMessagePort(m.messagePort)
m.socket = socket
m.decoder = new HttpChunkedDecoder()
@@ -111,6 +129,10 @@ class LoungeBackchannel
m.timer.Mark()
if m.decoder.IsRedirect()
return m.FollowRedirect()
end if
result = LoungeFraming.Extract(m.body, m.bodyCursor)
m.bodyCursor = result.nextPos
for each frame in result.frames
@@ -171,4 +193,50 @@ class LoungeBackchannel
m.bodyCursor = 0
end function
private function FollowRedirect() as object
frames = []
location = m.decoder.Location()
if not IsString(location) or location = ""
m.CloseWith("redirect_no_location")
return frames
end if
nextUrl = LoungeRedirect.Resolve(m.url, location)
m.redirectTarget = nextUrl
m.redirectCount += 1
if m.redirectCount > LOUNGE_BACKCHANNEL_MAX_REDIRECTS
m.CloseWith("redirect_limit")
return frames
end if
LogInfo("Lounge backchannel following redirect to", m.redirectTarget)
m.Connect(nextUrl)
return frames
end function
end class
namespace LoungeRedirect
' Force the target to plain HTTP - a raw socket has no TLS, so an https redirect is followed over http.
function Resolve(currentUrl as string, location as string) as string
if location.StartsWith("http://") or location.StartsWith("https://")
parts = UrlUtils.ParseUrl(location)
else
parts = UrlUtils.ParseUrl(currentUrl)
if not location.StartsWith("/")
location = "/" + location
end if
parts.path = location
end if
parts.protocol = "http"
if parts.port = "80" or parts.port = "443"
parts.port = invalid
end if
return UrlUtils.BuildUrlFromParts(parts)
end function
end namespace
@@ -134,9 +134,13 @@ function LoungeServiceLoop() as void
LogDebug("Backchannel closed:", closeInfo.reason)
if IsActionableCloseFailure(closeInfo.reason, needsRebind, needsTokenRefresh)
message = "Lounge backchannel failure: " + closeInfo.reason
if not StringUtils.IsNullOrEmpty(closeInfo.target)
message += " -> " + closeInfo.target
end if
LogTelemetryEvent({
level: "warning"
message: "Lounge backchannel failure: " + closeInfo.reason
message: message
fingerprint: "lounge-backchannel-" + closeInfo.reason
})
end if
@@ -65,6 +65,29 @@ namespace Tests
m.assertEqual(r.decoder.errorReason, "", "an HTTP status error is fingerprinted by code, not a slug")
end function
@it("surfaces a 3xx as a redirect carrying the Location, not an error")
@params("301 Moved Permanently", 301)
@params("302 Found", 302)
@params("303 See Other", 303)
@params("307 Temporary Redirect", 307)
@params("308 Permanent Redirect", 308)
function redirect(statusLine as string, expectedCode as integer)
r = m.DecodeWhole(`HTTP/1.1 ` + statusLine + `\r\nLocation: https://rr4.youtube.com/api/lounge/bc/bind?SID=x\r\n\r\n`)
m.assertTrue(r.decoder.IsRedirect())
m.assertFalse(r.decoder.IsError())
m.assertFalse(r.decoder.IsDone())
m.assertEqual(r.decoder.statusCode, expectedCode)
m.assertEqual(r.decoder.Location(), "https://rr4.youtube.com/api/lounge/bc/bind?SID=x")
end function
@it("treats an HTTP/1.0 307 as a redirect")
function redirectHttp10()
r = m.DecodeWhole(`HTTP/1.0 307 Temporary Redirect\r\nLocation: http://portal.example/login\r\n\r\n`)
m.assertTrue(r.decoder.IsRedirect())
m.assertFalse(r.decoder.IsError())
m.assertEqual(r.decoder.Location(), "http://portal.example/login")
end function
@it("flags a 200 response that is not chunked as an error rather than crashing")
function nonChunked()
r = m.DecodeWhole(`HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhello`)
@@ -102,7 +125,7 @@ namespace Tests
decoder = new HttpChunkedDecoder()
socket = new LoungeMockSocket(m.Bytes(response), readSize)
out = CreateObject("roByteArray")
while socket.GetCountRcvBuf() > 0 and not decoder.IsDone() and not decoder.IsError()
while socket.GetCountRcvBuf() > 0 and not decoder.IsDone() and not decoder.IsError() and not decoder.IsRedirect()
decoder.Pump(socket, out)
end while
return { decoder: decoder, body: out.ToAsciiString() }
@@ -0,0 +1,44 @@
import "pkg:/components/Services/LoungeService/LoungeBackchannel.bs"
namespace Tests
@suite
class LoungeRedirectTests extends Rooibos.BaseTestSuite
@describe("LoungeRedirect.Resolve")
@it("downgrades an https target to http, keeping host, path and query")
function downgradesHttps()
resolved = LoungeRedirect.Resolve("http://www.youtube.com/api/lounge/bc/bind?SID=a", "https://rr4.youtube.com/api/lounge/bc/bind?SID=a&gsessionid=b")
m.assertEqual(resolved, "http://rr4.youtube.com/api/lounge/bc/bind?SID=a&gsessionid=b")
end function
@it("keeps an http target as-is")
function keepsHttp()
resolved = LoungeRedirect.Resolve("http://www.youtube.com/x", "http://other.host/y?z=1")
m.assertEqual(resolved, "http://other.host/y?z=1")
end function
@it("drops a default 80 or 443 port")
@params("https://host:443/p", "http://host/p")
@params("http://host:80/p", "http://host/p")
@params("http://host:8080/p", "http://host:8080/p")
function dropsDefaultPort(location as string, expected as string)
m.assertEqual(LoungeRedirect.Resolve("http://www.youtube.com/x", location), expected)
end function
@it("resolves a root-relative Location against the current host")
function relativeLocation()
resolved = LoungeRedirect.Resolve("http://www.youtube.com/api/lounge/bc/bind?SID=a", "/api/lounge/bc/bind?SID=a&AID=5")
m.assertEqual(resolved, "http://www.youtube.com/api/lounge/bc/bind?SID=a&AID=5")
end function
@it("prefixes a slash onto a bare relative Location")
function bareRelativeLocation()
resolved = LoungeRedirect.Resolve("http://www.youtube.com/api/lounge/bc/bind", "elsewhere?x=1")
m.assertEqual(resolved, "http://www.youtube.com/elsewhere?x=1")
end function
end class
end namespace