Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(829)

Unified Diff: mojo/devtools/common/devtoolslib/http_server.py

Issue 1279543003: Devtools: gzip the file being served using a temporary file. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Address Ben's comments. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/devtools/common/devtoolslib/http_server.py
diff --git a/mojo/devtools/common/devtoolslib/http_server.py b/mojo/devtools/common/devtoolslib/http_server.py
index a74be0de59309829195b894e55dbfeb528ae9011..482e2db53108fe8aa120ba37e650543ce7c9d81c 100644
--- a/mojo/devtools/common/devtoolslib/http_server.py
+++ b/mojo/devtools/common/devtoolslib/http_server.py
@@ -14,6 +14,7 @@ import os.path
import shutil
import socket
import threading
+import tempfile
import SimpleHTTPServer
import SocketServer
@@ -66,10 +67,13 @@ def _get_handler_class_for_path(mappings):
class RequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
"""Handler for SocketServer.TCPServer that will serve the files from
local directiories over http.
+
+ A new instance is created for each request.
"""
def __init__(self, *args, **kwargs):
self.etag = None
+ self.gzipped_file = None
SimpleHTTPServer.SimpleHTTPRequestHandler.__init__(self, *args, **kwargs)
def get_etag(self):
@@ -147,13 +151,13 @@ def _get_handler_class_for_path(mappings):
normalized_path[len(prefix):])
if os.path.isfile(candidate):
if gzipped:
- gz_result = candidate + '.gz'
- if (not os.path.isfile(gz_result) or
- os.path.getmtime(gz_result) <= os.path.getmtime(candidate)):
- with open(candidate, 'rb') as f:
- with gzip.open(gz_result, 'wb') as zf:
- shutil.copyfileobj(f, zf)
- return gz_result
+ if not self.gzipped_file:
+ self.gzipped_file = tempfile.NamedTemporaryFile(delete=False)
+ with open(candidate, 'rb') as source:
+ with gzip.GzipFile(fileobj=self.gzipped_file) as target:
+ shutil.copyfileobj(source, target)
+ self.gzipped_file.close()
+ return self.gzipped_file.name
return candidate
else:
self.send_response(404)
@@ -174,6 +178,10 @@ def _get_handler_class_for_path(mappings):
"""Override the base class method to disable logging."""
pass
+ def __del__(self):
+ if self.gzipped_file:
+ os.remove(self.gzipped_file.name)
+
RequestHandler.protocol_version = 'HTTP/1.1'
return RequestHandler
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698