Fix encoding for request bodies

This commit is contained in:
WardPearce
2025-05-01 12:20:52 +12:00
parent 7b61513d35
commit c8f54e9679
2 changed files with 36 additions and 17 deletions
+17 -1
View File
@@ -83,6 +83,7 @@
let lastSeekMs = 0;
let lastManualFormatSelectionMs = 0;
let lastActionMs = 0;
let playerElementResizeObserver: ResizeObserver | undefined;
let clientViewportHeight = playerElement?.clientHeight || 0;
let clientViewportWidth = playerElement?.clientWidth || 0;
let lastPlaybackCookie: Protos.PlaybackCookie | undefined;
@@ -179,6 +180,15 @@
player = new shaka.Player();
playerElement = document.getElementById('player') as HTMLMediaElement;
playerElementResizeObserver = new ResizeObserver(() => {
if (playerElement) {
clientViewportHeight = playerElement.clientHeight;
clientViewportWidth = playerElement.clientWidth;
}
});
playerElementResizeObserver.observe(playerElement);
// Change instantly to stop video from being loud for a second
const savedVolume = localStorage.getItem(STORAGE_KEY_VOLUME);
if (savedVolume) {
@@ -376,7 +386,7 @@
lastManualFormatSelectionMs === 0 ? 0 : Date.now() - lastManualFormatSelectionMs,
clientViewportIsFlexible: false,
bandwidthEstimate: Math.round(player.getStats().estimatedBandwidth),
drcEnabled: currentFormat.is_drc,
drcEnabled: currentFormat.is_drc === true,
enabledTrackTypesBitfield: currentFormat.has_audio ? 1 : 2,
clientViewportHeight,
clientViewportWidth
@@ -441,6 +451,8 @@
if (videoFormatId) videoPlaybackAbrRequest.selectedFormatIds.push(videoFormatId);
}
console.log(videoPlaybackAbrRequest);
request.body = Protos.VideoPlaybackAbrRequest.encode(videoPlaybackAbrRequest).finish();
const byteRange = headers.Range
@@ -898,6 +910,10 @@
onDestroy(async () => {
HttpFetchPlugin.cacheManager.clearCache();
if (playerElementResizeObserver) {
playerElementResizeObserver.disconnect();
}
if (Capacitor.getPlatform() === 'android') {
if (originalOrigination) {
await StatusBar.setOverlaysWebView({ overlay: false });
+19 -16
View File
@@ -37,7 +37,7 @@ function setCorsHeaders(res) {
res.setHeader('Access-Control-Allow-Credentials', 'true');
}
function fetchWithRedirects(targetUrl, options, redirectCount = 0) {
function fetchWithRedirects(targetUrl, options, bodyChunks, redirectCount = 0) {
return new Promise((resolve, reject) => {
const httpClient = targetUrl.protocol.startsWith('https') ? https : http;
@@ -51,7 +51,7 @@ function fetchWithRedirects(targetUrl, options, redirectCount = 0) {
try {
// Attempt to create the redirect URL
const redirectUrl = new URL(res.headers.location, targetUrl);
return resolve(fetchWithRedirects(redirectUrl, options, redirectCount + 1));
return resolve(fetchWithRedirects(redirectUrl, options, bodyChunks, redirectCount + 1));
} catch (error) {
req.end();
return reject(new Error(`Invalid URL in redirect: ${error.message}`));
@@ -60,8 +60,12 @@ function fetchWithRedirects(targetUrl, options, redirectCount = 0) {
resolve(res); // Resolve with the final response if not a redirect
});
if (options.body && (req.method === 'POST' || req.method === 'PUT')) {
req.write(options.body);
// For POST and PUT methods, pass the body to the outgoing request
if (bodyChunks && (req.method === 'POST' || req.method === 'PUT')) {
const buffer = Buffer.concat(bodyChunks);
options.headers['Content-Length'] = buffer.length;
req.write(buffer);
}
req.setTimeout(10000, () => reject(new Error('Request timeout')), req.end());
@@ -96,9 +100,9 @@ const server = http.createServer(async (req, res) => {
return res.end(`Invalid URL: ${error.message}`);
}
let body = '';
let chunks = [];
req.on('data', (chunk) => {
body += chunk;
chunks.push(chunk);
});
req.on('end', async () => {
@@ -107,9 +111,14 @@ const server = http.createServer(async (req, res) => {
headers: Object.fromEntries(
Object.entries(req.headers).filter(
([key]) =>
!['host', 'origin', 'referer', 'x-forwarded-for', 'x-requested-with'].includes(
key.toLowerCase()
)
![
'referer',
'x-forwarded-for',
'x-requested-with',
'sec-ch-ua-mobile',
'sec-ch-ua',
'sec-ch-ua-platform'
].includes(key.toLowerCase())
)
)
};
@@ -118,14 +127,8 @@ const server = http.createServer(async (req, res) => {
options.headers.origin = parsedTarget.origin;
options.headers['user-agent'] = USER_AGENT;
// For POST and PUT methods, pass the body to the outgoing request
if (req.method === 'POST' || req.method === 'PUT') {
options.headers['Content-Length'] = Buffer.byteLength(body);
options.body = body;
}
try {
const proxyRes = await fetchWithRedirects(parsedTarget, options);
const proxyRes = await fetchWithRedirects(parsedTarget, options, chunks);
req.on('close', () => {
console.log('Request canceled by the client.');