Skip to content

Commit

Permalink
fix(ajax): Fix case-insensitive headers in HTTP request
Browse files Browse the repository at this point in the history
In RFC 7230 and RFC 7540 is written, that HTTP headers is case-insensitive, so this is fix for correct serializing request body base on HTTP headers.
  • Loading branch information
Tomas Dostal committed Jan 7, 2019
1 parent b0f4fc0 commit f150e95
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/internal/observable/dom/AjaxObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,18 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
const headers = request.headers = request.headers || {};

// force CORS if requested
if (!request.crossDomain && !headers['X-Requested-With']) {
if (!request.crossDomain && !this.getHeader(headers, 'X-Requested-With')) {
headers['X-Requested-With'] = 'XMLHttpRequest';
}

// ensure content type is set
if (!('Content-Type' in headers) && !(root.FormData && request.body instanceof root.FormData) && typeof request.body !== 'undefined') {
let contentTypeHeader = this.getHeader(headers, 'Content-Type');
if (!contentTypeHeader && !(root.FormData && request.body instanceof root.FormData) && typeof request.body !== 'undefined') {
headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
}

// properly serialize body
request.body = this.serializeBody(request.body, request.headers['Content-Type']);
request.body = this.serializeBody(request.body, this.getHeader(request.headers, 'Content-Type'));

this.send();
}
Expand Down Expand Up @@ -315,6 +316,16 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
}
}

private getHeader(headers: {}, headerName: string): any {
for (let key in headers) {
if (key.toLowerCase() === headerName.toLowerCase()) {
return headers[key];
}
}

return undefined;
}

private setupEvents(xhr: XMLHttpRequest, request: AjaxRequest) {
const progressSubscriber = request.progressSubscriber;

Expand Down

0 comments on commit f150e95

Please sign in to comment.