Skip to content

Commit

Permalink
Merge pull request #171 from telefonicaid/bug/contentType
Browse files Browse the repository at this point in the history
FIX Use express methods to check mime type
  • Loading branch information
XavierVal committed Oct 20, 2015
2 parents 70b23ef + adc70ac commit 2c9b3ad
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 12 deletions.
3 changes: 2 additions & 1 deletion CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
- FIX 500 error while removing devices (#151)
- FIX Change Device Not Found to 404 instead of 500 (#161).
- ADD First version of the stats registry (#160).
- FIX Reestructure files and folders (#164).
- FIX Reestructure files and folders (#164).
- FIX Use Express Methods to check content type (#170).
2 changes: 1 addition & 1 deletion lib/services/ngsi/ngsiParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ function readQueryPayload(req, callback) {

function readBody(parseFunction) {
return function readMiddleware(req, res, next) {
if (req.headers['content-type'] === 'application/xml') {
if (req.is('xml')) {
parseFunction(req, next);
} else {
next();
Expand Down
8 changes: 4 additions & 4 deletions lib/services/northBound/contextServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ function handleUpdate(req, res, next) {
} else {
logger.debug(context, 'Update action from [%s] handled successfully.', req.get('host'));

if (req.is('application/xml')) {
if (req.is('xml')) {
res.set('Content-Type', 'application/xml');
res
.status(200)
Expand Down Expand Up @@ -390,7 +390,7 @@ function handleQuery(req, res, next) {
} else {
logger.debug(context, 'Query from [%s] handled successfully.', req.get('host'));

if (req.is('application/xml')) {
if (req.is('xml')) {
res.set('Content-Type', 'application/xml');
res.status(200)
.send(generateXmlResponse(queryTemplates)(createQueryResponse(req, res, result)));
Expand Down Expand Up @@ -462,7 +462,7 @@ function setQueryHandler(newHandler) {
}

function ensureType(req, res, next) {
if (req.headers['content-type'] === 'application/json' || req.headers['content-type'] === 'application/xml') {
if (req.is('json') || req.is('xml')) {
next();
} else {
next(new errors.UnsupportedContentType(req.headers['content-type']));
Expand All @@ -471,7 +471,7 @@ function ensureType(req, res, next) {

function validateJson(template) {
return function validate(req, res, next) {
if (req.headers['content-type'] === 'application/json') {
if (req.is('json')) {
var errorList = revalidator.validate(req.body, template);

if (errorList.valid) {
Expand Down
4 changes: 2 additions & 2 deletions lib/services/northBound/northboundServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ function handleError(error, req, res, next) {
function traceRequest(req, res, next) {
logger.debug(context, 'Request for path [%s] from [%s]', req.path, req.get('host'));

if (req.headers['content-type'] === 'application/json') {
if (req.is('json')) {
logger.debug(context, 'Body:\n\n%s\n\n', JSON.stringify(req.body, null, 4));
} else if (req.headers['content-type'] === 'application/xml') {
} else if (req.is('xml')) {
logger.debug(context, 'Body:\n\n%s\n\n', req.rawBody);
} else {
logger.debug(context, 'Unrecognized body type', req.headers['content-type']);
Expand Down
5 changes: 1 addition & 4 deletions lib/services/northBound/restUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ function checkMandatoryQueryParams(mandatoryAttributes, body, callback) {
* @param {Function} next Invokes the next middleware in the chain.
*/
function xmlRawBody(req, res, next) {
var contentType = req.headers['content-type'] || '',
mime = contentType.split(';')[0];

if (mime !== 'text/xml' && mime !== 'application/xml') {
if (!req.is('xml')) {
next();
} else {
var data = '';
Expand Down

0 comments on commit 2c9b3ad

Please sign in to comment.