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

Unified Diff: chrome/test/chromedriver/webserver.py

Issue 11746025: [chromedriver]Implement command: title. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Resolved merge conflict. Created 7 years, 11 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 | « chrome/test/chromedriver/run_py_tests.py ('k') | chrome/test/data/chromedriver/empty.html » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/test/chromedriver/webserver.py
diff --git a/chrome/test/chromedriver/webserver.py b/chrome/test/chromedriver/webserver.py
new file mode 100644
index 0000000000000000000000000000000000000000..e6571f84203c6296c98fd0348be38189aca3f958
--- /dev/null
+++ b/chrome/test/chromedriver/webserver.py
@@ -0,0 +1,65 @@
+# Copyright (c) 2012 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import BaseHTTPServer
+import os
+import threading
+
+
+class _FileRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
+ """Sends back file resources relative to the server's |root_dir|."""
+
+ def do_GET(self):
+ if self.path.endswith('favicon.ico'):
+ self.send_error(404)
+ return
+ path = os.path.join(self.server.root_dir, *self.path.split('/'))
+ with open(path, 'r') as f:
+ data = f.read()
+ self.send_response(200)
+ self.send_header('Content-Length', len(data))
+ self.end_headers()
+ self.wfile.write(data)
+
+
+class WebServer(object):
+ """An HTTP or HTTPS server that serves files on its own thread."""
+
+ def __init__(self, root_dir, server_cert_and_key_path=None):
+ """Starts the web server on its own thread on an ephemeral port.
+ It is an HTTP server if parameter server_cert_and_key_path is not provided.
+ Otherwise, it is an HTTPS server.
+
+ After this function returns, it is safe to assume the server is ready
+ to receive requests.
+
+ Args:
+ root_dir: root path to serve files from. This parameter is required.
+ server_cert_and_key_path: path to a PEM file containing the cert and key.
+ if it is None, start the server as an HTTP one.
+ """
+ self._server = BaseHTTPServer.HTTPServer(
+ ('127.0.0.1', 0), _FileRequestHandler)
+ self._server.root_dir = root_dir
+ if server_cert_and_key_path is not None:
+ self._is_https_enabled = True
+ self._server.socket = ssl.wrap_socket(
+ self._server.socket, certfile=server_cert_and_key_path,
+ server_side=True)
+ else:
+ self._is_https_enabled = False
+
+ self._thread = threading.Thread(target=self._server.serve_forever)
+ self._thread.start()
+
+ def GetUrl(self):
+ """Returns the base URL of the server."""
+ if self._is_https_enabled:
+ return 'https://127.0.0.1:%s' % self._server.server_port
+ return 'http://127.0.0.1:%s' % self._server.server_port
+
+ def Shutdown(self):
+ """Shuts down the server synchronously."""
+ self._server.shutdown()
+ self._thread.join()
« no previous file with comments | « chrome/test/chromedriver/run_py_tests.py ('k') | chrome/test/data/chromedriver/empty.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698