Consistent evaluation of the response content type
authorAlexander Ebert <ebert@woltlab.com>
Mon, 16 May 2022 09:31:40 +0000 (11:31 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 16 May 2022 09:31:40 +0000 (11:31 +0200)
ts/WoltLabSuite/Core/Ajax/Request.ts
wcfsetup/install/files/js/WoltLabSuite/Core/Ajax/Request.js

index 9b6d8d042070b40a516034f5ba6568eb2a181566..93ec385ced86c381b938d45ad8699fe20cfd0778 100644 (file)
@@ -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);
index ffdd084dfc32820d97e53e6ac2689852bf308eaa..cbeae1139ffd934aab0c3fd7bb2a9bb698e7c498 100644 (file)
@@ -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;