From 82d5a7fafdfde698dac57c6e136f31eae266e273 Mon Sep 17 00:00:00 2001 From: Shane Kilkelly Date: Wed, 11 May 2016 15:55:21 +0100 Subject: [PATCH] check total size of lines, rather than content-length --- .../document-updater/app/coffee/HttpController.coffee | 11 +++++++++-- .../unit/coffee/HttpController/setDocTests.coffee | 5 ++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/services/document-updater/app/coffee/HttpController.coffee b/services/document-updater/app/coffee/HttpController.coffee index 65c7ca20d3..9c5eac94e9 100644 --- a/services/document-updater/app/coffee/HttpController.coffee +++ b/services/document-updater/app/coffee/HttpController.coffee @@ -30,14 +30,21 @@ module.exports = HttpController = version: version ops: ops + _getTotalSizeOfLines: (lines) -> + size = 0 + for line in lines + size += line.length + return size + setDoc: (req, res, next = (error) ->) -> doc_id = req.params.doc_id project_id = req.params.project_id lines = req.body.lines source = req.body.source user_id = req.body.user_id - if req.headers['content-length'] > TWO_MEGABYTES - logger.log {project_id, doc_id, source, user_id}, "document too large, returning 406 response" + lineSize = HttpController._getTotalSizeOfLines(lines) + if lineSize > TWO_MEGABYTES + logger.log {project_id, doc_id, source, lineSize, user_id}, "document too large, returning 406 response" return res.send 406 logger.log project_id: project_id, doc_id: doc_id, lines: lines, source: source, user_id: user_id, "setting doc via http" timer = new Metrics.Timer("http.setDoc") diff --git a/services/document-updater/test/unit/coffee/HttpController/setDocTests.coffee b/services/document-updater/test/unit/coffee/HttpController/setDocTests.coffee index e8c05ccd03..9a0176c25d 100644 --- a/services/document-updater/test/unit/coffee/HttpController/setDocTests.coffee +++ b/services/document-updater/test/unit/coffee/HttpController/setDocTests.coffee @@ -69,7 +69,10 @@ describe "HttpController.setDoc", -> describe "when the payload is too large", -> beforeEach -> - @req.headers['content-length'] = 40 * 1024 * 1024 + lines = [] + for _ in [0..300000] + lines.push "test test test" + @req.body.lines = lines @DocumentManager.setDocWithLock = sinon.stub().callsArgWith(5) @HttpController.setDoc(@req, @res, @next)