only rate limit full pdf downloads

This commit is contained in:
Henry Oswald 2015-11-10 16:23:55 +00:00
parent da9535f442
commit 16670cb3e3
2 changed files with 15 additions and 16 deletions

View file

@ -41,6 +41,20 @@ module.exports = CompileController =
project_id = req.params.Project_id project_id = req.params.Project_id
isPdfjsPartialDownload = req.query?.pdfng isPdfjsPartialDownload = req.query?.pdfng
rateLimit = (callback)->
if isPdfjsPartialDownload
callback null, true
else
rateLimitOpts =
endpointName: "full-pdf-download"
throttle: 30
subjectName : req.ip
timeInterval : 60 * 5
RateLimiter.addCount rateLimitOpts, callback
Project.findById project_id, {name: 1}, (err, project)-> Project.findById project_id, {name: 1}, (err, project)->
res.contentType("application/pdf") res.contentType("application/pdf")
if !!req.query.popupDownload if !!req.query.popupDownload
@ -50,19 +64,7 @@ module.exports = CompileController =
logger.log project_id: project_id, "download pdf to embed in browser" logger.log project_id: project_id, "download pdf to embed in browser"
res.header('Content-Disposition', "filename=#{project.getSafeProjectName()}.pdf") res.header('Content-Disposition', "filename=#{project.getSafeProjectName()}.pdf")
rateLimit (err, canContinue)->
if isPdfjsPartialDownload
rateLimitOpts =
endpointName: "partial-pdf-download"
throttle: 500
else
rateLimitOpts =
endpointName: "full-pdf-download"
throttle: 30
rateLimitOpts.subjectName = req.ip
rateLimitOpts.timeInterval = 60 * 5
RateLimiter.addCount rateLimitOpts, (err, canContinue)->
if err? if err?
logger.err err:err, "error checking rate limit for pdf download" logger.err err:err, "error checking rate limit for pdf download"
return res.send 500 return res.send 500

View file

@ -129,9 +129,6 @@ describe "CompileController", ->
it "should proxy the PDF from the CLSI", -> it "should proxy the PDF from the CLSI", ->
@CompileController.proxyToClsi.calledWith(@project_id, "/project/#{@project_id}/output/output.pdf", @req, @res, @next).should.equal true @CompileController.proxyToClsi.calledWith(@project_id, "/project/#{@project_id}/output/output.pdf", @req, @res, @next).should.equal true
it "should check the rate limiter", ->
@RateLimiter.addCount.args[0][0].throttle.should.equal 500
describe "when the pdf is not going to be used in pdfjs viewer", -> describe "when the pdf is not going to be used in pdfjs viewer", ->
it "should check the rate limiter when pdfng is not set", (done)-> it "should check the rate limiter when pdfng is not set", (done)->