1#!/usr/bin/env python
2# Copyright (C) 2010 Google Inc. All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8#     * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10#     * Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following disclaimer
12# in the documentation and/or other materials provided with the
13# distribution.
14#     * Neither the name of Google Inc. nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30"""Base class with common routines between the Apache and Lighttpd servers."""
31
32import logging
33import os
34import time
35import urllib
36
37from webkitpy.common.system import filesystem
38
39_log = logging.getLogger("webkitpy.layout_tests.port.http_server_base")
40
41
42class HttpServerBase(object):
43
44    def __init__(self, port_obj):
45        self._port_obj = port_obj
46
47    def wait_for_action(self, action):
48        """Repeat the action for 20 seconds or until it succeeds. Returns
49        whether it succeeded."""
50        start_time = time.time()
51        while time.time() - start_time < 20:
52            if action():
53                return True
54            _log.debug("Waiting for action: %s" % action)
55            time.sleep(1)
56
57        return False
58
59    def is_server_running_on_all_ports(self):
60        """Returns whether the server is running on all the desired ports."""
61        for mapping in self.mappings:
62            if 'sslcert' in mapping:
63                http_suffix = 's'
64            else:
65                http_suffix = ''
66
67            url = 'http%s://127.0.0.1:%d/' % (http_suffix, mapping['port'])
68
69            try:
70                response = urllib.urlopen(url, proxies={})
71                _log.debug("Server running at %s" % url)
72            except IOError, e:
73                _log.debug("Server NOT running at %s: %s" % (url, e))
74                return False
75
76        return True
77
78    def remove_log_files(self, folder, starts_with):
79        files = os.listdir(folder)
80        for file in files:
81            if file.startswith(starts_with):
82                full_path = os.path.join(folder, file)
83                filesystem.FileSystem().remove(full_path)
84