Skip to content

Commit

Permalink
Read the query from the request body properly.
Browse files Browse the repository at this point in the history
  • Loading branch information
artob committed Jan 4, 2025
1 parent 08791bc commit aea18c5
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/sparql/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def application(dataset: RDF::Repository.new, **options)
rescue SPARQL::Grammar::Parser::Error => e
halt 400, "Error parsing query: #{e.message}"
end
res = query.execute(repo,
res = query.execute(repo,
logger: request.logger,
**options.merge(opts))
res.is_a?(RDF::Literal::Boolean) ? [res] : res
Expand All @@ -61,21 +61,24 @@ def application(dataset: RDF::Repository.new, **options)
end

post '/' do
opts = params.inject({}) {|memo, (k,v)| memo.merge(k.to_sym => v)}
# Note, this depends on the Rack::SPARQL::ContentNegotiation middleware to rewrite application/x-www-form-urlencoded to be conformat with either application/sparql-query or application/sparql-update.
request_body = request.body.read
opts = params.inject({}) { |memo, (k,v)| memo.merge(k.to_sym => v) }
# Note, this depends on the Rack::SPARQL::ContentNegotiation
# middleware to rewrite application/x-www-form-urlencoded to be
# conformant with either application/sparql-query or
# application/sparql-update.
query = begin
update = case request.content_type
when %r(application/sparql-query) then false
when %r(application/sparql-update) then true
else
halt 406, "No query found for #{request.content_type}"
end

# XXX Rack always sets input to ASCII-8BIT
#unless request.body.external_encoding == Encoding::UTF_8
# halt 400, "improper body encoding: #{request.body.external_encoding}"
#end
SPARQL.parse(request.body, base_uri: url, update: update)
SPARQL.parse(request_body, base_uri: url, update: update)
rescue SPARQL::Grammar::Parser::Error => e
halt 400, "Error parsing #{update ? 'update' : 'query'}: #{e.message}"
end
Expand All @@ -90,4 +93,4 @@ def application(dataset: RDF::Repository.new, **options)
end
module_function :application
end
end
end

0 comments on commit aea18c5

Please sign in to comment.