From 0ce854a18b2b8bb5bdcdba036e9195fd34e97dd7 Mon Sep 17 00:00:00 2001 From: Jan Forberg Date: Thu, 1 Feb 2024 12:47:43 +0100 Subject: [PATCH] better accept header parsing --- server/app/common/get-linked-data.js | 50 ++++++++++++++-------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/server/app/common/get-linked-data.js b/server/app/common/get-linked-data.js index b5c06a6..7cc1d79 100644 --- a/server/app/common/get-linked-data.js +++ b/server/app/common/get-linked-data.js @@ -25,38 +25,38 @@ module.exports = async function getLinkedData(req, res, next, resourceUri, templ accept = HttpStrings.CONTENT_TYPE_JSONLD; } - if(accept == HttpStrings.CONTENT_TYPE_JSONLD) { + let indexOfComma = accept.indexOf(","); + let firstAccept = accept.substring(0, indexOfComma); + + if (firstAccept == HttpStrings.CONTENT_TYPE_JSONLD) { // Handle JSONLD separately var jsonld = await getJsonld(resourceUri, template, formatting); - if(jsonld != null) { + if (jsonld != null) { res.set(HttpStrings.HEADER_CONTENT_TYPE, HttpStrings.CONTENT_TYPE_JSONLD); res.status(200).send(`${JSON.stringify(jsonld, null, 3)}\n`); return; } - } else { - - // Format the query - var query = ServerUtils.formatQuery(template, { - RESOURCE_URI: resourceUri - }); - - // Prepare OPTIONS for database request - var headers = {}; - headers[HttpStrings.HEADER_CONTENT_TYPE] = HttpStrings.CONTENT_TYPE_FORM_URL_ENCODED; - headers[HttpStrings.HEADER_ACCEPT] = accept; - - var options = { - method: HttpStrings.METHOD_POST, - uri: `${process.env.DATABUS_DATABASE_URL}/sparql?timeout=10000`, - body: `query=${encodeURIComponent(query)}`, - headers: headers, - }; - - // Pipe the request - request(options).pipe(res); - return; } - res.status(500).send('Encountered a database error when trying to fetch the resource.'); + // Format the query + var query = ServerUtils.formatQuery(template, { + RESOURCE_URI: resourceUri + }); + + // Prepare OPTIONS for database request + var headers = {}; + headers[HttpStrings.HEADER_CONTENT_TYPE] = HttpStrings.CONTENT_TYPE_FORM_URL_ENCODED; + headers[HttpStrings.HEADER_ACCEPT] = accept; + + var options = { + method: HttpStrings.METHOD_POST, + uri: `${process.env.DATABUS_DATABASE_URL}/sparql?timeout=10000`, + body: `query=${encodeURIComponent(query)}`, + headers: headers, + }; + + // Pipe the request + request(options).pipe(res); + return; } \ No newline at end of file