Skip to content

Commit

Permalink
Breaking: Use text/xml as the media type for XML
Browse files Browse the repository at this point in the history
From https://tools.ietf.org/html/rfc3023#page-5:

 " If an XML document -- that is, the unprocessed,
   source XML document -- is readable by casual users,
   text/xml is preferable to application/xml. "
  • Loading branch information
alrra authored and molant committed Jan 12, 2018
1 parent 7226ac7 commit 2e75ac5
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/lib/utils/content-type.ts
Expand Up @@ -239,6 +239,9 @@ const determineMediaTypeBasedOnFileExtension = (resource: string): string => {
return 'font/ttf';
case 'otf':
return 'font/otf';
case 'xml':
// See: https://tools.ietf.org/html/rfc3023#page-5.
return 'text/xml';
}
/* eslint-enable no-default */

Expand All @@ -263,6 +266,25 @@ const determineMediaTypeBasedOnFileType = (rawContent: Buffer): string => {
return null;
};

const getPreferedMediaType = (mediaType: string): string => {

// Prefer certain media types over others.

switch (mediaType) {
case 'application/xml':
/*
* From https://tools.ietf.org/html/rfc3023#page-5:
*
* " If an XML document -- that is, the unprocessed,
* source XML document -- is readable by casual users,
* text/xml is preferable to application/xml. "
*/
return 'text/xml';
default:
return mediaType;
}
};

const parseContentTypeHeader = (headers): MediaType => {
const contentTypeHeaderValue: string = normalizeString(headers ? headers['content-type'] : null);

Expand Down Expand Up @@ -323,12 +345,14 @@ const getContentTypeData = (element: IAsyncHTMLElement, resource: string, header
* information, as sometimes servers are misconfigured.
*/

const mediaType =
let mediaType =
determineMediaTypeBasedOnElement(element) ||
determineMediaTypeBasedOnFileType(rawContent) ||
determineMediaTypeBasedOnFileExtension(resource) ||
originalMediaType;

mediaType = getPreferedMediaType(mediaType);

const charset = determineCharset(originalCharset, mediaType);

return {
Expand Down

0 comments on commit 2e75ac5

Please sign in to comment.