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

Enhanced protocol parsing implementation to fix #4633; #4639

Merged
merged 2 commits into from Apr 27, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/adapters/xhr.js
Expand Up @@ -10,6 +10,7 @@ var isURLSameOrigin = require('./../helpers/isURLSameOrigin');
var transitionalDefaults = require('../defaults/transitional');
var AxiosError = require('../core/AxiosError');
var CanceledError = require('../cancel/CanceledError');
var parseProtocol = require('../helpers/parseProtocol');

module.exports = function xhrAdapter(config) {
return new Promise(function dispatchXhrRequest(resolve, reject) {
Expand Down Expand Up @@ -207,8 +208,7 @@ module.exports = function xhrAdapter(config) {
requestData = null;
}

var tokens = fullPath.split(':', 2);
var protocol = tokens.length > 1 && tokens[0];
var protocol = parseProtocol(fullPath);

if (protocol && [ 'http', 'https', 'file' ].indexOf(protocol) === -1) {
reject(new AxiosError('Unsupported protocol ' + protocol + ':', AxiosError.ERR_BAD_REQUEST, config));
Expand Down
6 changes: 6 additions & 0 deletions lib/helpers/parseProtocol.js
@@ -0,0 +1,6 @@
'use strict';

module.exports = function parseProtocol(url) {
var match = /^([-+\w]{1,25})(:?\/\/|:)/.exec(url);
return match && match[1] || '';
};
24 changes: 24 additions & 0 deletions test/unit/helpers/parseProtocol.js
@@ -0,0 +1,24 @@
var assert = require('assert');
var utils = require('../../../lib/utils');
var parseProtocol = require('../../../lib/helpers/parseProtocol');

describe('helpers::parseProtocol', function () {
it('should parse protocol part if it exists', function () {
utils.forEach({
'http://username:password@example.com/': 'http',
'ftp:google.com': 'ftp',
'sms:+15105550101?body=hello%20there': 'sms',
'tel:0123456789' : 'tel',
'//google.com': '',
'google.com': '',
'admin://etc/default/grub' : 'admin',
'stratum+tcp://server:port': 'stratum+tcp',
'/api/resource:customVerb': '',
'https://stackoverflow.com/questions/': 'https',
'mailto:jsmith@example.com' : 'mailto',
'chrome-extension://1234/<pageName>.html' : 'chrome-extension'
}, (expectedProtocol, url) => {
assert.strictEqual(parseProtocol(url), expectedProtocol);
});
});
});