Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix redirects in case of relative URL #8

Merged
merged 1 commit into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/node/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Url, parse as parseUrl } from 'url';
import { Url, parse as parseUrl, URL, format } from 'url';
import * as https from 'https';
import * as http from 'http';
import * as zlib from 'zlib';
Expand Down Expand Up @@ -57,6 +57,15 @@ export const xhr: XHRRequest = (options: XHROptions): Promise<XHRResponse> => {
isCompleted = true;
if (options.followRedirects > 0 && (res.statusCode >= 300 && res.statusCode <= 303 || res.statusCode === 307)) {
let location = res.headers['location'];
if(location.startsWith('/')){
let endpoint = parseUrl(options.url);
location = format({
protocol: endpoint.protocol,
hostname: endpoint.hostname,
port: endpoint.port,
pathname: location
});
}
if (location) {
let newOptions = {
type: options.type, url: location, user: options.user, password: options.password, headers: options.headers,
Expand Down Expand Up @@ -141,8 +150,17 @@ function request(options: XHROptions): Promise<RequestResult> {

let handler = (res: http.IncomingMessage) => {
if (res.statusCode >= 300 && res.statusCode < 400 && options.followRedirects && options.followRedirects > 0 && res.headers['location']) {
let location = res.headers['location'];
if(location.startsWith('/')){
location = format({
protocol: endpoint.protocol,
hostname: endpoint.hostname,
port: endpoint.port,
pathname: location
});
}
c(<any>request(assign({}, options, {
url: res.headers['location'],
url: location,
followRedirects: options.followRedirects - 1
})));
} else {
Expand Down Expand Up @@ -234,4 +252,4 @@ function getProxyAgent(rawRequestURL: string, options: ProxyOptions = {}): any {
};

return requestURL.protocol === 'http:' ? createHttpProxyAgent(opts) : createHttpsProxyAgent(opts);
}
}
27 changes: 26 additions & 1 deletion src/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { createServer, createSecureServer, createProxy, createSecureProxy } from
import { AddressInfo } from 'net';
import { promises as fs } from 'fs';
import { join } from 'path';
import { IncomingMessage, ServerResponse } from 'http';

test('text content', async t => {
const testContent = JSON.stringify({ hello: 1, world: true})
Expand Down Expand Up @@ -82,4 +83,28 @@ test('proxy https to https', async t => {

server.close();
proxy.close();
})
});

test('relative redirect', async t => {
const server = await createServer();

server.on('request', (req:IncomingMessage, res: ServerResponse) => {
if(req.url.includes('/foo')) {
res.setHeader('Location', '/bar');
res.statusCode = 301;
res.end();
}
if(req.url.includes('/bar')){
res.end('Bar');
}
});

const serverAddress = server.address() as AddressInfo;

const response = await xhr({ url: `http://${serverAddress.address}:${serverAddress.port}/foo` });

t.deepEqual(response.body.toString(), 'Bar');
t.is(response.status, 200);

server.close();
});