From 734a576cb97707778dc26886f54bbb6f6b5b480c Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 22 Aug 2021 10:54:34 -0400 Subject: [PATCH] Still broken --- app/classes/web/tornado.py | 2 + app/classes/web/upload_handler.py | 54 +++++++++++++++++++ .../templates/panel/server_files.html | 5 +- requirements.txt | 3 +- 4 files changed, 61 insertions(+), 3 deletions(-) diff --git a/app/classes/web/tornado.py b/app/classes/web/tornado.py index 4b770b55..1ef91ff6 100644 --- a/app/classes/web/tornado.py +++ b/app/classes/web/tornado.py @@ -28,6 +28,7 @@ try: from app.classes.web.static_handler import CustomStaticHandler from app.classes.shared.translation import translation from app.classes.web.upload_handler import UploadHandler + from app.classes.web.upload_handler import ProxyHandler except ModuleNotFoundError as e: logger.critical("Import Error: Unable to load {} module".format(e, e.name)) @@ -131,6 +132,7 @@ class Webserver: (r'/api/stats/node', NodeStats, handler_args), (r'/ws', SocketHandler, handler_args), (r'/upload', UploadHandler, handler_args), + (r'/proxy', ProxyHandler, handler_args) ] app = tornado.web.Application( diff --git a/app/classes/web/upload_handler.py b/app/classes/web/upload_handler.py index c0e35f07..75a0c02a 100644 --- a/app/classes/web/upload_handler.py +++ b/app/classes/web/upload_handler.py @@ -7,6 +7,7 @@ from tornado.options import parse_command_line, define, options from tornado.web import Application, RequestHandler, stream_request_body import logging +import toro logger = logging.getLogger(__name__) @@ -32,3 +33,56 @@ class UploadHandler(RequestHandler): print("In PUT") logger.info('UploadHandler.put') self.write('ok') + +@stream_request_body +class ProxyHandler(RequestHandler): + def prepare(self): + logger.info('ProxyHandler.prepare') + self.chunks = toro.Queue(1) + self.fetch_future = AsyncHTTPClient().fetch( + 'http://localhost:%d/upload' % options.port, + method='PUT', + body_producer=self.body_producer, + request_timeout=3600.0) + + @gen.coroutine + def body_producer(self, write): + while True: + chunk = yield self.chunks.get() + if chunk is None: + return + yield write(chunk) + + @gen.coroutine + def data_received(self, chunk): + logger.info('ProxyHandler.data_received(%d bytes: %r)', + len(chunk), chunk[:9]) + yield self.chunks.put(chunk) + + @gen.coroutine + def put(self): + logger.info('ProxyHandler.put') + # Write None to the chunk queue to signal body_producer to exit, + # then wait for the request to finish. + yield self.chunks.put(None) + response = yield self.fetch_future + self.set_status(response.code) + self.write(response.body) + +@gen.coroutine +def client(): + @gen.coroutine + def body_producer(write): + for i in range(options.num_chunks): + yield gen.Task(IOLoop.current().call_later, options.client_delay) + chunk = ('chunk %02d ' % i) * 10000 + logger.info('client writing %d bytes: %r', len(chunk), chunk[:9]) + yield write(utf8(chunk)) + + response = yield AsyncHTTPClient().fetch( + 'http://localhost:%d/proxy' % options.port, + method='PUT', + body_producer=body_producer, + request_timeout=3600.0) + logger.info('client finished with response %d: %r', + response.code, response.body) diff --git a/app/frontend/templates/panel/server_files.html b/app/frontend/templates/panel/server_files.html index 2125e8b1..93f65e98 100644 --- a/app/frontend/templates/panel/server_files.html +++ b/app/frontend/templates/panel/server_files.html @@ -548,12 +548,13 @@ files = document.getElementById("upload_files").files for (i = 0; i < files.length; i++){ + var xmlHttpRequest = new XMLHttpRequest(); var file = files[i] var fileName = files[i].name - var target = '/upload' + var target = '/proxy' var mimeType = files[i].type - xmlHttpRequest.open('PUT', target, true); + xmlHttpRequest.open('POSTT', target, true); console.log(xmlHttpRequest); xmlHttpRequest.setRequestHeader('Content-Type', mimeType); xmlHttpRequest.setRequestHeader('Content-Disposition', 'attachment; filename="' + fileName + '"'); diff --git a/requirements.txt b/requirements.txt index 3f6b29f0..6c98e3d5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,10 +16,11 @@ pyminifier==2.1 pyOpenSSL==19.1.0 pyparsing==2.4.7 PyYAML==5.3.1 -requests==2.24.0 +requests~=2.26.0 schedule==0.6.0 six==1.15.0 termcolor==1.1.0 tornado==6.0.4 urllib3==1.25.10 webencodings==0.5.1 +toro~=1.0.1 \ No newline at end of file