From 429a5b2bc7f32e0a40cffe4923b7862e90e77d9c Mon Sep 17 00:00:00 2001 From: Simon Legander Date: Wed, 1 Apr 2020 16:36:02 +0200 Subject: [PATCH] [fix] move common logic to helper fn. --- src/aid.js | 7 ++++++- src/parse/request.js | 4 ++-- src/render/post/structured.js | 3 ++- src/validate/postData.js | 4 ++-- src/validate/request.js | 6 +++--- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/aid.js b/src/aid.js index 3ee51dc0..85c78f7b 100644 --- a/src/aid.js +++ b/src/aid.js @@ -38,6 +38,10 @@ function parseContentType (str = '') { } } +function getContentTypeValue (str = '') { + return str.split(';')[0] +} + function isBlacklistedHeader (headerName = '') { const HEADERS_BLACKLIST = ['Content-Length'] const [name] = headerName.split(';') @@ -59,5 +63,6 @@ module.exports = { isString, extrinsic, nought, - parseContentType + parseContentType, + getContentTypeValue } diff --git a/src/parse/request.js b/src/parse/request.js index 64b16e28..9b926757 100644 --- a/src/parse/request.js +++ b/src/parse/request.js @@ -2,7 +2,7 @@ const headers = require('./headers') const postData = require('./postData') const queryString = require('./queryString') const state = require('./state/request') -const { emptyObject } = require('../aid') +const { emptyObject, getContentTypeValue } = require('../aid') function request (node, spec) { spec.method = node.method.toUpperCase() @@ -41,7 +41,7 @@ function addBoundary (boundary, headers) { if (headers.has('Content-Type')) { const items = [...headers.get('Content-Type').values()] const newItems = items.map(item => { - const value = item.value.split(';')[0] + const value = getContentTypeValue(item.value) if (value === 'multipart/form-data') { return { value: `${value}; boundary=${boundary}` } } diff --git a/src/render/post/structured.js b/src/render/post/structured.js index 643adce4..47eddf5e 100644 --- a/src/render/post/structured.js +++ b/src/render/post/structured.js @@ -1,9 +1,10 @@ const multipart = require('./multipart') const url = require('./url') const { UnrecognizedError } = require('../../error') +const { getContentTypeValue } = require('../../aid') function structured (spec) { - switch (spec.post.type.split(';')[0]) { + switch (getContentTypeValue(spec.post.type)) { case 'application/x-www-form-urlencoded': return url(spec) case 'multipart/form-data': diff --git a/src/validate/postData.js b/src/validate/postData.js index 46a066c4..36f33872 100644 --- a/src/validate/postData.js +++ b/src/validate/postData.js @@ -1,5 +1,5 @@ const params = require('./params') -const { empty, emptyObject, seralizeURLSearchParams } = require('../aid') +const { empty, emptyObject, seralizeURLSearchParams, getContentTypeValue } = require('../aid') const { InvalidArchiveError } = require('../error') /* @@ -76,7 +76,7 @@ function validate (node, i) { ![ 'application/x-www-form-urlencoded', 'multipart/form-data' - ].includes(node.mimeType.split(';')[0]) + ].includes(getContentTypeValue(node.mimeType)) ) { throw new InvalidArchiveError( { name: 'InvalidPostDataType' }, diff --git a/src/validate/request.js b/src/validate/request.js index ff7252d8..57d530f2 100644 --- a/src/validate/request.js +++ b/src/validate/request.js @@ -3,7 +3,7 @@ const headers = require('./headers') const isPlainObject = require('is-plain-object') const postData = require('./postData') const queryString = require('./queryString') -const { empty, emptyObject } = require('../aid') +const { empty, emptyObject, getContentTypeValue } = require('../aid') const { absoluteUrl, variableStart } = require('../expression') const { InvalidArchiveError } = require('../error') @@ -116,8 +116,8 @@ function relation (node, i) { node.headers.findIndex(findContentType) !== -1 ) { const header = node.headers.find(findContentType) - const headerType = header.value ? header.value.split(';')[0] : '' - const postType = node.postData.mimeType ? node.postData.mimeType.split(';')[0] : '' + const headerType = getContentTypeValue(header.value) + const postType = getContentTypeValue(node.postData.mimeType) if (headerType !== postType) { throw new InvalidArchiveError( { name: 'InconsistentContentType' },