Skip to content

Commit

Permalink
Merge pull request #8 from evidolob/fix-redirect
Browse files Browse the repository at this point in the history
Fix redirects in case of relative URL
  • Loading branch information
aeschli authored Nov 17, 2021
2 parents 693f239 + 14d45a8 commit 2db2b1c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
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();
});

0 comments on commit 2db2b1c

Please sign in to comment.