def _WprHost(wpr_archive_path, record=False, network_condition_name=None, disable_script_injection=False, wpr_ca_cert_path=None, out_log_path=None): assert wpr_archive_path def PathWorkaround(path): # webpagereplay.ReplayServer is doing a os.path.exist(os.path.dirname(p)) # that fails if p = 'my_file.txt' because os.path.dirname(p) = '' != '.'. # This workaround just sends absolute path to work around this bug. return os.path.abspath(path) wpr_server_args = ['--use_closest_match'] if record: wpr_server_args.append('--record') if os.path.exists(wpr_archive_path): os.remove(wpr_archive_path) else: assert os.path.exists(wpr_archive_path) if network_condition_name: condition = emulation.NETWORK_CONDITIONS[network_condition_name] if record: logging.warning('WPR network condition is ignored when recording.') else: wpr_server_args.extend([ '--down', emulation.BandwidthToString(condition['download']), '--up', emulation.BandwidthToString(condition['upload']), '--delay_ms', str(condition['latency']), '--shaping_type', 'proxy']) if disable_script_injection: # Remove default WPR injected scripts like deterministic.js which # overrides Math.random. wpr_server_args.extend(['--inject_scripts', '']) if wpr_ca_cert_path: wpr_server_args.extend(['--should_generate_certs', '--https_root_ca_cert_path=' + PathWorkaround(wpr_ca_cert_path)]) if out_log_path: # --log_level debug to extract the served URLs requests from the log. wpr_server_args.extend(['--log_level', 'debug', '--log_file', PathWorkaround(out_log_path)]) # Don't append to previously existing log. if os.path.exists(out_log_path): os.remove(out_log_path) # Set up WPR server and device forwarder. server = wpr_server.ReplayServer(PathWorkaround(wpr_archive_path), '127.0.0.1', 0, 0, None, wpr_server_args) http_port, https_port = server.StartServer()[:-1] logging.info('WPR server listening on HTTP=%s, HTTPS=%s (options=%s)' % ( http_port, https_port, wpr_server_args)) try: yield http_port, https_port finally: server.StopServer()
def _StartReplayServer(self): """Start the replay server and return the started local_ports.""" self._StopReplayServer() # In case it was already running. self._wpr_server = wpr_server.ReplayServer( self._archive_path, self.host_ip, http_port=0, https_port=0, dns_port=None, replay_options=self._ReplayCommandLineArgs()) return self._wpr_server.StartServer()
def _StartReplayServer(self): """Start the replay server and return the started local_ports.""" self._StopReplayServer() # In case it was already running. local_ports = self._wpr_port_pairs.local_ports self._wpr_server = wpr_server.ReplayServer( self._archive_path, self.host_ip, local_ports.http, local_ports.https, local_ports.dns, self._ReplayCommandLineArgs()) return self._wpr_server.StartServer()
def _BringUpWpr(self): """Start the WPR server on the host and the forwarder on the device.""" print 'Starting WPR on host...' _DownloadFromCloudStorage(self._WPR_BUCKET, self._wpr_archive_hash) args = ['--use_closest_match'] if self._is_test_ca_installed: args.extend([ '--should_generate_certs', '--https_root_ca_cert_path=' + self._wpr_ca_cert_path ]) self._wpr_server = wpr_server.ReplayServer(self._wpr_archive, '127.0.0.1', 0, 0, None, args) ports = self._wpr_server.StartServer()[:-1] self._host_http_port = ports[0] self._host_https_port = ports[1]