Skip to content

Commit fc42bbc

Browse files
RubenVerborghdmitrizagidulin
authored andcommitted
Delegate body parsing to middleware.
1 parent 90c218a commit fc42bbc

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

lib/handlers/patch.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = handler
22

3+
var bodyParser = require('body-parser')
34
var mime = require('mime-types')
45
var fs = require('fs')
56
var debug = require('../debug').handlers
@@ -13,24 +14,19 @@ const PATCHERS = {
1314
'application/sparql-update': require('./patch/sparql-update-patcher.js')
1415
}
1516

16-
function handler (req, res, next) {
17-
req.setEncoding('utf8')
18-
req.text = ''
19-
req.on('data', function (chunk) {
20-
req.text += chunk
21-
})
17+
const readEntity = bodyParser.text({ type: '*/*' })
2218

23-
req.on('end', function () {
24-
patchHandler(req, res, next)
25-
})
19+
function handler (req, res, next) {
20+
readEntity(req, res, () => patchHandler(req, res, next))
2621
}
2722

2823
function patchHandler (req, res, next) {
29-
var ldp = req.app.locals.ldp
24+
const patchText = req.body ? req.body.toString() : ''
3025
debug('PATCH -- ' + req.originalUrl)
31-
debug('PATCH -- text length: ' + (req.text ? req.text.length : 'undefined2'))
26+
debug('PATCH -- Received patch (%d bytes)', patchText.length)
3227
res.header('MS-Author-Via', 'SPARQL')
3328

29+
var ldp = req.app.locals.ldp
3430
var root = !ldp.idp ? ldp.root : ldp.root + req.hostname + '/'
3531
var targetFile = utils.uriToFilename(req.path, root)
3632
var targetContentType = mime.lookup(targetFile) || DEFAULT_CONTENT_TYPE
@@ -50,7 +46,7 @@ function patchHandler (req, res, next) {
5046
// Read the RDF graph to be patched from the file
5147
readGraph(targetFile, targetURI, targetContentType)
5248
// Patch the graph and write it back to the file
53-
.then(targetKB => patchGraph(targetKB, targetFile, targetURI, req.text))
49+
.then(targetKB => patchGraph(targetKB, targetFile, targetURI, patchText))
5450
.then(targetKB => writeGraph(targetKB, targetFile, targetURI, targetContentType))
5551
// Send the result to the client
5652
.then(result => { res.send(result) })

0 commit comments

Comments
 (0)