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 a bug with url encoded parameters in url.parse #1480

Merged
merged 1 commit into from
Sep 11, 2017
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
2 changes: 1 addition & 1 deletion modules/atomxBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var AtomxAdapter = function AtomxAdapter() {
id: bid.params.id,
size: sizes[j],
prebid: bid.placementCode
}, {method: 'GET'});
}, {method: 'GET', noDecodeWholeURL: true});
}
} else {
var bidObject = bidfactory.createBid(CONSTANTS.STATUS.NO_BID, bid);
Expand Down
2 changes: 1 addition & 1 deletion src/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function ajax(url, callback, data, options = {}) {
}

if (method === 'GET' && data) {
let urlInfo = parseURL(url);
let urlInfo = parseURL(url, options);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if passing the whole options object here is the best or if it should extract the options that apply to url.parse. This would result in a lot more code.

Object.assign(urlInfo.search, data);
url = formatURL(urlInfo);
}
Expand Down
8 changes: 6 additions & 2 deletions src/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ export function formatQS(query) {
.join('&');
}

export function parse(url) {
export function parse(url, options) {
let parsed = document.createElement('a');
parsed.href = decodeURIComponent(url);
if (options && 'noDecodeWholeURL' in options && options.noDecodeWholeURL) {
parsed.href = url;
} else {
parsed.href = decodeURIComponent(url);
}
return {
protocol: (parsed.protocol || '').replace(/:$/, ''),
hostname: parsed.hostname,
Expand Down
28 changes: 23 additions & 5 deletions test/spec/url_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('helpers.url', () => {
let parsed;

beforeEach(() => {
parsed = parse('http://example.com:3000/pathname/?search=test&foo=bar#hash');
parsed = parse('http://example.com:3000/pathname/?search=test&foo=bar&bar=foo%26foo%3Dxxx#hash');
});

it('extracts the protocol', () => {
Expand All @@ -28,8 +28,9 @@ describe('helpers.url', () => {
it('extracts the search query', () => {
expect(parsed).to.have.property('search');
expect(parsed.search).to.eql({
foo: 'bar',
search: 'test'
foo: 'xxx',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left this here to show that the current url.parse is quite broken with foo being set to xxx instead of the expected bar.

search: 'test',
bar: 'foo',
});
});

Expand All @@ -42,16 +43,33 @@ describe('helpers.url', () => {
});
});

describe('parse(url, {noDecodeWholeURL: true})', () => {
let parsed;

beforeEach(() => {
parsed = parse('http://example.com:3000/pathname/?search=test&foo=bar&bar=foo%26foo%3Dxxx#hash', {noDecodeWholeURL: true});
});

it('extracts the search query', () => {
expect(parsed).to.have.property('search');
expect(parsed.search).to.eql({
foo: 'bar',
search: 'test',
bar: 'foo%26foo%3Dxxx',
});
});
});

describe('format()', () => {
it('formats an object in to a URL', () => {
expect(format({
protocol: 'http',
hostname: 'example.com',
port: 3000,
pathname: '/pathname/',
search: {foo: 'bar', search: 'test'},
search: {foo: 'bar', search: 'test', bar: 'foo%26foo%3Dxxx'},
hash: 'hash'
})).to.equal('http://example.com:3000/pathname/?foo=bar&search=test#hash');
})).to.equal('http://example.com:3000/pathname/?foo=bar&search=test&bar=foo%26foo%3Dxxx#hash');
});

it('will use defaults for missing properties', () => {
Expand Down