Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
exports.setCharset = function(type, charset){
if (!type || !charset) return type;
// parse type
var parsed = typer.parse(type);
// set charset
parsed.parameters.charset = charset;
// format type
return typer.format(parsed);
};
isJsonContentType(contentTypeValue) {
if (!contentTypeValue) {
return false;
}
try {
const { type } = contentType.parse(`${contentTypeValue}`);
const parsed = mediaTyper.parse(type);
return (
(parsed.type === 'application' && parsed.subtype === 'json') ||
parsed.suffix === 'json'
);
} catch (e) {
// The Content-Type value is basically a user input, it can be any
// kind of rubbish, and it is neither this function's nor Gavel's problem
// if it's invalid
return false;
}
}
}
exports.setCharset = function(type, charset){
if (!type || !charset) return type;
// parse type
var parsed = typer.parse(type);
// set charset
parsed.parameters.charset = charset;
// format type
return typer.format(parsed);
};
exports.setCharset = function(type, charset){
if (!type || !charset) return type;
// parse type
var parsed = typer.parse(type);
// set charset
parsed.parameters.charset = charset;
// format type
return typer.format(parsed);
};
return function urlencodedParser(req, res, next) {
if (req._body) return next();
req.body = req.body || {}
if (!typeis(req, type)) return next();
var charset = typer.parse(req).parameters.charset || 'utf-8'
if (charset.toLowerCase() !== 'utf-8') {
var err = new Error('unsupported charset')
err.status = 415
next(err)
return
}
// read
read(req, res, next, parse, {
encoding: charset,
inflate: inflate,
limit: limit,
verify: verify
})
}
}
private _parseContentType () {
try {
const parsed = mediaTyper.parse(this._data.headers['content-type'])
this.type = parsed.type
this.subtype = parsed.subtype
} catch (error) {
}
}
Promise.resolve().then(() => {
const type = req.headers['content-type'] || 'text/plain'
const length = req.headers['content-length']
encoding = encoding === undefined
? typer.parse(type).parameters.charset
: encoding
const body = rawBodyMap.get(req)
if (body) {
return body
}
return getRawBody(req, { limit, length, encoding })
.then(buf => {
rawBodyMap.set(req, buf)
return buf
})
.catch(err => {
if (err.type === 'entity.too.large') {
throw createError(413, `Body exceeded ${limit} limit`, err)
export function parseHttpContentType(contentType: string): { type: string, subtype: string, suffix?: string, parameters: { [id: string]: string; } } {
const type = ContentType.parse(contentType);
const mime = MimeType.parse(type.type);
return {
type: mime.type,
subtype: mime.subtype,
suffix: mime.suffix,
parameters: type.parameters
};
}
function typenormalize(value) {
try {
var type = typer.parse(value)
delete type.parameters
return typer.format(type)
} catch (err) {
return null
}
}