Skip to content

Commit

Permalink
Enhanced protocol parsing implementation to fix #4633; (#4639)
Browse files Browse the repository at this point in the history
Added unit tests;

Co-authored-by: Jay <jasonsaayman@gmail.com>
  • Loading branch information
DigitalBrainJS and jasonsaayman committed Apr 27, 2022
1 parent 76432c1 commit b9e9fb4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
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);
});
});
});

0 comments on commit b9e9fb4

Please sign in to comment.