server-docker icon indicating copy to clipboard operation
server-docker copied to clipboard

Proxy server sent wrong host header to redirected url

Open martinpl opened this issue 1 year ago • 0 comments

Hello, I have issue with Stremio proxy server. I'm passing link that contains redirection. Redirection go to wanted url but it hits there loop and ends on "maximum redirect reached". Problem is that streamio pass orginal "host" header to second request and that why there is loop.

My quick fix (end of fetch method)

// HTTP-redirect fetch step 5
if (request.counter >= request.follow) {
    reject(new FetchError(`maximum redirect reached at: ${request.url}`, 'max-redirect'));
    finalize();
    return;
}

// just update host to new location
request.headers.set('host', new URL(locationURL).host)

// HTTP-redirect fetch step 6 (counter increment)
// Create a new Request object.
const requestOpts = {
    headers: new Headers(request.headers),
    follow: request.follow,
    counter: request.counter + 1,
    agent: request.agent,
    compress: request.compress,
    method: request.method,
    body: request.body
};

// HTTP-redirect fetch step 9
if (res.statusCode !== 303 && request.body && getTotalBytes(request) === null) {
    reject(new FetchError('Cannot follow redirect with body being a readable stream', 'unsupported-redirect'));
    finalize();
    return;
}

// HTTP-redirect fetch step 11
if (res.statusCode === 303 || (res.statusCode === 301 || res.statusCode === 302) && request.method === 'POST') {
    requestOpts.method = 'GET';
    requestOpts.body = undefined;
    requestOpts.headers.delete('content-length');
}

// HTTP-redirect fetch step 15
resolve(fetch(new Request(locationURL, requestOpts)));
finalize();
return;

PS. Thanks for great app :)

martinpl avatar Dec 19 '24 17:12 martinpl