From 87d366bf4aa8d62316ed6b6a02843623457ea707 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Mon, 16 May 2022 11:31:40 +0200 Subject: [PATCH] Consistent evaluation of the response content type --- ts/WoltLabSuite/Core/Ajax/Request.ts | 13 +++++++++++-- .../files/js/WoltLabSuite/Core/Ajax/Request.js | 11 +++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/ts/WoltLabSuite/Core/Ajax/Request.ts b/ts/WoltLabSuite/Core/Ajax/Request.ts index 9b6d8d0420..93ec385ced 100644 --- a/ts/WoltLabSuite/Core/Ajax/Request.ts +++ b/ts/WoltLabSuite/Core/Ajax/Request.ts @@ -144,7 +144,7 @@ class AjaxRequest { // HTTP 204 does not contain a body, the `content-type` is undefined. this._success(xhr, options); } else { - if (options.responseType && xhr.getResponseHeader("Content-Type")!.indexOf(options.responseType) !== 0) { + if (options.responseType && this.getContentType(xhr) !== options.responseType) { // request succeeded but invalid response type this._failure(xhr, options); } else { @@ -234,7 +234,7 @@ class AjaxRequest { if (typeof options.success === "function") { let data: ResponseData | null = null; - if (xhr.getResponseHeader("Content-Type")!.split(";", 1)[0].trim() === "application/json") { + if (this.getContentType(xhr) === "application/json") { try { data = JSON.parse(xhr.responseText) as ResponseData; } catch (e) { @@ -369,6 +369,15 @@ class AjaxRequest { } }); } + + private getContentType(xhr: XMLHttpRequest): string | null { + const contentType = xhr.getResponseHeader("content-type"); + if (contentType === null) { + return null; + } + + return contentType.split(";", 1)[0].trim(); + } } Core.enableLegacyInheritance(AjaxRequest); diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Ajax/Request.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Ajax/Request.js index ffdd084dfc..cbeae1139f 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Ajax/Request.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Ajax/Request.js @@ -117,7 +117,7 @@ define(["require", "exports", "tslib", "./Status", "../Core", "../Dom/Change/Lis this._success(xhr, options); } else { - if (options.responseType && xhr.getResponseHeader("Content-Type").indexOf(options.responseType) !== 0) { + if (options.responseType && this.getContentType(xhr) !== options.responseType) { // request succeeded but invalid response type this._failure(xhr, options); } @@ -197,7 +197,7 @@ define(["require", "exports", "tslib", "./Status", "../Core", "../Dom/Change/Lis } if (typeof options.success === "function") { let data = null; - if (xhr.getResponseHeader("Content-Type").split(";", 1)[0].trim() === "application/json") { + if (this.getContentType(xhr) === "application/json") { try { data = JSON.parse(xhr.responseText); } @@ -311,6 +311,13 @@ define(["require", "exports", "tslib", "./Status", "../Core", "../Dom/Change/Lis } }); } + getContentType(xhr) { + const contentType = xhr.getResponseHeader("content-type"); + if (contentType === null) { + return null; + } + return contentType.split(";", 1)[0].trim(); + } } Core.enableLegacyInheritance(AjaxRequest); return AjaxRequest; -- 2.20.1