Skip to content

Commit

Permalink
Simplify the handlers that manage the files download reducing the ove…
Browse files Browse the repository at this point in the history
…rhead of the handler.
  • Loading branch information
evilaliv3 committed Sep 14, 2016
1 parent 1f07a76 commit d8232e5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 47 deletions.
53 changes: 12 additions & 41 deletions backend/globaleaks/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,36 +499,16 @@ def do_verbose_log(self, content):
except Exception as excep:
log.err("Unable to open %s: %s" % (GLSettings.httplogfile, excep))

def write_chunk(self, f):
try:
chunk = f.read(GLSettings.file_chunk_size)
if len(chunk) != 0:
self.write(chunk)
self.flush()
reactor.callLater(0.01, self.write_chunk, f)
else:
f.close()
self.finish()
except Exception:
f.close()
self.finish()

@inlineCallbacks
def write_file(self, filepath):
f = None

try:
f = open(filepath, "rb")
except IOError as srcerr:
log.err("Unable to open %s: %s " % (filepath, srcerr.strerror))
self.finish()

try:
reactor.callLater(0, self.write_chunk, f)
except Exception:
if f is not None:
f.close()
with open(filepath, "rb") as f:
chunk = f.read(GLSettings.file_chunk_size)
self.write(chunk)

self.finish()
chunk = f.read(GLSettings.file_chunk_size)
while(len(chunk) != 0):
yield deferred_sleep(0.001)
self.write(chunk)

def write_error(self, status_code, **kw):
exception = kw.get('exception')
Expand Down Expand Up @@ -701,24 +681,15 @@ def handler_request_logging_end(self):


class BaseStaticFileHandler(BaseHandler):
def initialize(self, path=None):
if path is None:
path = GLSettings.static_path

self.root = "%s%s" % (os.path.abspath(path), os.path.sep)

def parse_url_path(self, url_path):
if os.path.sep != "/":
url_path = url_path.replace("/", os.path.sep)
return url_path
def initialize(self, path):
self.root = "%s%s" % (os.path.abspath(path), "/")

@BaseHandler.unauthenticated
@web.asynchronous
@inlineCallbacks
def get(self, path):
if path == '':
path = 'index.html'

path = self.parse_url_path(path)
abspath = os.path.abspath(os.path.join(self.root, path))

directory_traversal_check(self.root, abspath)
Expand All @@ -730,7 +701,7 @@ def get(self, path):
if mime_type:
self.set_header("Content-Type", mime_type)

self.write_file(abspath)
yield self.write_file(abspath)


class BaseRedirectHandler(BaseHandler, RedirectHandler):
Expand Down
4 changes: 1 addition & 3 deletions backend/globaleaks/handlers/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from twisted.internet import threads
from twisted.internet.defer import inlineCallbacks

from cyclone.web import asynchronous
from globaleaks.handlers.base import BaseHandler
from globaleaks.handlers.rtip import db_access_rtip
from globaleaks.models import ReceiverFile, InternalTip, InternalFile, WhistleblowerTip
Expand Down Expand Up @@ -248,7 +247,6 @@ class Download(BaseHandler):

@BaseHandler.transport_security_check('receiver')
@BaseHandler.authenticated('receiver')
@asynchronous
@inlineCallbacks
def get(self, rtip_id, rfile_id):
rfile = yield download_file(self.current_user.user_id, rtip_id, rfile_id)
Expand All @@ -260,6 +258,6 @@ def get(self, rtip_id, rfile_id):
self.set_header('Content-Type', 'application/octet-stream')
self.set_header('Content-Length', rfile['size'])
self.set_header('Content-Disposition', 'attachment; filename=\"%s\"' % rfile['name'])
self.write_file(filelocation)
yield self.write_file(filelocation)
else:
self.set_status(404)
1 change: 0 additions & 1 deletion backend/globaleaks/handlers/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ def get(self):


class RobotstxtHandler(BaseHandler):

@BaseHandler.transport_security_check("unauth")
@BaseHandler.unauthenticated
def get(self):
Expand Down
3 changes: 1 addition & 2 deletions backend/globaleaks/rest/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@
(r'/wizard', wizard.Wizard),

## Special Files Handlers##
(r'/(favicon.ico)', base.BaseStaticFileHandler),
(r'/robots.txt', public.RobotstxtHandler),
(r'/s/(.*)', base.BaseStaticFileHandler),
(r'/s/(.*)', base.BaseStaticFileHandler, {'path': GLSettings.static_path}),
(r'/static/(.*)', base.BaseStaticFileHandler), # still here for backward compatibility
(r'/l10n/(' + '|'.join(LANGUAGES_SUPPORTED_CODES) + ')', l10n.L10NHandler),

Expand Down

0 comments on commit d8232e5

Please sign in to comment.