Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: service description only without query #487

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tame-berries-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zazuko/trifid-plugin-sparql-proxy": patch
---

Fixes Service Description to only respond to requests without **any** query strings, as it is required by the [spec](https://www.w3.org/TR/2013/REC-sparql11-service-description-20130321/#accessing).
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 20 additions & 20 deletions packages/sparql-proxy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,26 @@ const factory = async (trifid) => {
fullUrlObject.searchParams.forEach((_value, key) => fullUrlObject.searchParams.delete(key))
const iriUrlString = fullUrlObject.toString()

if (Object.keys(request.query).length === 0 && request.method === 'GET') {
const dataset = rdf.dataset(await serviceDescription)
rdf.clownface({ dataset })
.has(rdf.ns.rdf.type, rdf.ns.sd.Service)
.addOut(rdf.ns.sd.endpoint, rdf.namedNode(fullUrl))

const accept = request.accepts()
const negotiatedTypes = accept.type([...rdf.formats.serializers.keys()])
const negotiatedType = Array.isArray(negotiatedTypes) ? negotiatedTypes[0] : negotiatedTypes
if (!negotiatedType) {
reply.code(406).send()
return reply
}

reply
.header('content-type', negotiatedType)
.send(await dataset.serialize({ format: negotiatedType }))
return reply
}

// Enforce non-trailing slash
if (fullUrlPathname.slice(-1) === '/') {
reply.redirect(`${fullUrlPathname.slice(0, -1)}`)
Expand Down Expand Up @@ -200,26 +220,6 @@ const factory = async (trifid) => {
return reply
}

if (!query && method === 'GET') {
const dataset = await serviceDescription
rdf.clownface({ dataset })
.has(rdf.ns.rdf.type, rdf.ns.sd.Service)
.addOut(rdf.ns.sd.endpoint, rdf.namedNode(fullUrl))

const accept = request.accepts()
const negotiatedTypes = accept.type([...rdf.formats.serializers.keys()])
const negotiatedType = Array.isArray(negotiatedTypes) ? negotiatedTypes[0] : negotiatedTypes
if (!negotiatedType) {
reply.code(406).send()
return reply
}

reply
.header('content-type', negotiatedType)
.send(await dataset.serialize({ format: negotiatedType }))
return reply
}

if (rewriteResponse && options.rewriteQuery) {
query = query.replaceAll(rewriteResponse.replacement, rewriteResponse.origin)
}
Expand Down
25 changes: 20 additions & 5 deletions packages/sparql-proxy/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ describe('sparql-proxy', () => {
restore()
})

it('does not serve Service Description when there are any query string', async () => {
// given
const url = await startTrifid({
serviceDescriptionTimeout: 0,
})

// when
const response = await rdf.fetch(`${url}/query?foo=bar`)

// then
expect(response.headers.get('content-type')).to.match(/text\/plain/)
})

for (const [property] of forwardedProperties) {
if (rdf.ns.sd.endpoint.equals(property)) continue

Expand All @@ -80,12 +93,14 @@ describe('sparql-proxy', () => {
const url = await startTrifid()

// when
const response = await rdf.fetch(`${url}/query`)
const dataset = await response.dataset()
for (const path of ['query', 'query/']) {
const response = await rdf.fetch(`${url}/${path}`)
const dataset = await response.dataset()

// then
const service = rdf.clownface({ dataset }).has(rdf.ns.sd.endpoint)
expect(service.out(rdf.ns.sd.endpoint).term).to.deep.eq(rdf.namedNode(`${url}/query`))
// then
const service = rdf.clownface({ dataset }).has(rdf.ns.sd.endpoint)
expect(service.out(rdf.ns.sd.endpoint).term).to.deep.eq(rdf.namedNode(`${url}/${path}`))
}
})

it('serves minimal description if original service is too slow', async () => {
Expand Down