Skip to content
Permalink
Browse files

Ajax: Fix getResponseHeader(key) for IE11

- getResponseHeader(key) combines all header values for the provided key into a
single result where values are concatenated by ', '. This does not happen for
IE11 since multiple values for the same header are returned on separate lines.
This makes the function only return the last value of the header for IE11.
- Updated ajax headers test to better cover Object.prototype collisions

Close gh-4173
Fixes gh-3403
  • Loading branch information...
Andrei15193 authored and timmywil committed Nov 26, 2018
1 parent 3ac9078 commit e0d941156900a6bff7c098c8ea7290528e468cf8
Showing with 16 additions and 5 deletions.
  1. +5 −3 src/ajax.js
  2. +3 −0 test/data/mock.php
  3. +4 −1 test/middleware-mockserver.js
  4. +4 −1 test/unit/ajax.js
@@ -459,12 +459,14 @@ jQuery.extend( {
if ( !responseHeaders ) {
responseHeaders = {};
while ( ( match = rheaders.exec( responseHeadersString ) ) ) {
responseHeaders[ match[ 1 ].toLowerCase() ] = match[ 2 ];
responseHeaders[ match[ 1 ].toLowerCase() + " " ] =
( responseHeaders[ match[ 1 ].toLowerCase() + " " ] || [] )
.concat( match[ 2 ] );
}
}
match = responseHeaders[ key.toLowerCase() ];
match = responseHeaders[ key.toLowerCase() + " " ];
}
return match == null ? null : match;
return match == null ? null : match.join( ", " );
},

// Raw string
@@ -114,6 +114,9 @@ protected function headers( $req ) {
header( 'Sample-Header: Hello World' );
header( 'Empty-Header: ' );
header( 'Sample-Header2: Hello World 2' );
header( 'List-Header: Item 1' );
header( 'list-header: Item 2' );
header( 'constructor: prototype collision (constructor)' );
foreach ( explode( '|' , $req->query[ 'keys' ] ) as $key ) {
// Only echo if key exists in the header
@@ -132,7 +132,10 @@ var mocks = {
resp.writeHead( 200, {
"Sample-Header": "Hello World",
"Empty-Header": "",
"Sample-Header2": "Hello World 2"
"Sample-Header2": "Hello World 2",
"List-Header": "Item 1",
"list-header": "Item 2",
"constructor": "prototype collision (constructor)"
} );
req.query.keys.split( "|" ).forEach( function( key ) {
if ( req.headers[ key.toLowerCase() ] ) {
@@ -252,7 +252,7 @@ QUnit.module( "ajax", {
} );
} );

ajaxTest( "jQuery.ajax() - headers", 5, function( assert ) {
ajaxTest( "jQuery.ajax() - headers", 8, function( assert ) {
return {
setup: function() {
jQuery( document ).ajaxSend( function( evt, xhr ) {
@@ -293,6 +293,9 @@ QUnit.module( "ajax", {
assert.strictEqual( emptyHeader, "", "Empty header received" );
}
assert.strictEqual( xhr.getResponseHeader( "Sample-Header2" ), "Hello World 2", "Second sample header received" );
assert.strictEqual( xhr.getResponseHeader( "List-Header" ), "Item 1, Item 2", "List header received" );
assert.strictEqual( xhr.getResponseHeader( "constructor" ), "prototype collision (constructor)", "constructor header received" );
assert.strictEqual( xhr.getResponseHeader( "__proto__" ), null, "Undefined __proto__ header not received" );
}
};
} );

0 comments on commit e0d9411

Please sign in to comment.
You can’t perform that action at this time.