From 4f49c57c0c4a48cb3383b60f46e57f0fc3be92e6 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Sat, 9 Sep 2017 13:24:40 +0000 Subject: [PATCH] Add basic HTML content-type detection (Closes #466) --- app/ui/components/viewers/response-viewer.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/app/ui/components/viewers/response-viewer.js b/app/ui/components/viewers/response-viewer.js index 8fea8dd1c..03889d421 100644 --- a/app/ui/components/viewers/response-viewer.js +++ b/app/ui/components/viewers/response-viewer.js @@ -150,6 +150,17 @@ class ResponseViewer extends PureComponent { // Nothing } + // Try to detect HTML in all cases (even if header is set). It is fairly + // common for webservers to send errors in HTML by default. + // NOTE: This will probably never throw but I'm not 100% so wrap anyway + try { + if (bodyBuffer.slice(0, 100).toString().trim().match(/^/i)) { + contentType = 'text/html'; + } + } catch (e) { + // Nothing + } + const ct = contentType.toLowerCase(); if (previewMode === PREVIEW_MODE_FRIENDLY && ct.indexOf('image/') === 0) { const justContentType = contentType.split(';')[0]; @@ -188,7 +199,7 @@ class ResponseViewer extends PureComponent { return (
);