fix(http): return Response headers

Properly parse and add response Headers to Response.

Closes #5237
This commit is contained in:
Rob Wormald
2015-11-19 17:51:00 -08:00
parent 201f189d0e
commit 4332ccf72c
4 changed files with 64 additions and 3 deletions

View File

@ -2,6 +2,7 @@ import {ConnectionBackend, Connection} from '../interfaces';
import {ReadyStates, RequestMethods, ResponseTypes} from '../enums';
import {Request} from '../static_request';
import {Response} from '../static_response';
import {Headers} from '../headers';
import {ResponseOptions, BaseResponseOptions} from '../base_response_options';
import {Injectable} from 'angular2/angular2';
import {BrowserXhr} from './browser_xhr';
@ -34,8 +35,9 @@ export class XHRConnection implements Connection {
// responseText is the old-school way of retrieving response (supported by IE8 & 9)
// response/responseType properties were introduced in XHR Level2 spec (supported by
// IE10)
let xhrResponse = isPresent(_xhr.response) ? _xhr.response : _xhr.responseText;
let body = isPresent(_xhr.response) ? _xhr.response : _xhr.responseText;
let headers = Headers.fromResponseHeaderString(_xhr.getAllResponseHeaders());
// normalize IE9 bug (http://bugs.jquery.com/ticket/1450)
let status: number = _xhr.status === 1223 ? 204 : _xhr.status;
@ -44,9 +46,9 @@ export class XHRConnection implements Connection {
// Occurs when accessing file resources or on Android 4.1 stock browser
// while retrieving files from application cache.
if (status === 0) {
status = xhrResponse ? 200 : 0;
status = body ? 200 : 0;
}
var responseOptions = new ResponseOptions({body: xhrResponse, status: status});
var responseOptions = new ResponseOptions({body, status, headers});
if (isPresent(baseResponseOptions)) {
responseOptions = baseResponseOptions.merge(responseOptions);
}

View File

@ -54,6 +54,17 @@ export class Headers {
headers, (v, k) => { this._headersMap.set(k, isListLikeIterable(v) ? v : [v]); });
}
/**
* Returns a new Headers instance from the given DOMString of Response Headers
*/
static fromResponseHeaderString(headersString: string): Headers {
return headersString.trim()
.split('\n')
.map(val => val.split(':'))
.map(([key, ...parts]) => ([key.trim(), parts.join(':').trim()]))
.reduce((headers, [key, value]) => !headers.set(key, value) && headers, new Headers());
}
/**
* Appends a header to existing list of header values for a given header name.
*/