summaryrefslogtreecommitdiffstats
path: root/WebKitTools/Scripts/webkitpy/layout_tests/port/websocket_server.py
blob: 81bf39ec27521088190b2b9d7a08dde6b44fd1b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env python
# Copyright (C) 2010 Google Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""A class to help start/stop the PyWebSocket server used by layout tests."""


from __future__ import with_statement

import codecs
import logging
import optparse
import os
import subprocess
import sys
import tempfile
import time
import urllib

import factory
import http_server

from webkitpy.common.system.executive import Executive
from webkitpy.thirdparty.autoinstalled.pywebsocket import mod_pywebsocket


_log = logging.getLogger("webkitpy.layout_tests.port.websocket_server")

_WS_LOG_PREFIX = 'pywebsocket.ws.log-'
_WSS_LOG_PREFIX = 'pywebsocket.wss.log-'

_DEFAULT_WS_PORT = 8880
_DEFAULT_WSS_PORT = 9323


def url_is_alive(url):
    """Checks to see if we get an http response from |url|.
    We poll the url 5 times with a 1 second delay.  If we don't
    get a reply in that time, we give up and assume the httpd
    didn't start properly.

    Args:
      url: The URL to check.
    Return:
      True if the url is alive.
    """
    sleep_time = 0.5
    wait_time = 5
    while wait_time > 0:
        try:
            response = urllib.urlopen(url)
            # Server is up and responding.
            return True
        except IOError:
            pass
        # Wait for sleep_time before trying again.
        wait_time -= sleep_time
        time.sleep(sleep_time)

    return False


class PyWebSocketNotStarted(Exception):
    pass


class PyWebSocketNotFound(Exception):
    pass


class PyWebSocket(http_server.Lighttpd):

    def __init__(self, port_obj, output_dir, port=_DEFAULT_WS_PORT,
                 root=None, use_tls=False,
                 pidfile=None):
        """Args:
          output_dir: the absolute path to the layout test result directory
        """
        http_server.Lighttpd.__init__(self, port_obj, output_dir,
                                      port=_DEFAULT_WS_PORT,
                                      root=root)
        self._output_dir = output_dir
        self._process = None
        self._port = port
        self._root = root
        self._use_tls = use_tls
        self._private_key = self._pem_file
        self._certificate = self._pem_file
        if self._port:
            self._port = int(self._port)
        if self._use_tls:
            self._server_name = 'PyWebSocket(Secure)'
        else:
            self._server_name = 'PyWebSocket'
        self._pidfile = pidfile
        self._wsout = None

        # Webkit tests
        if self._root:
            self._layout_tests = os.path.abspath(self._root)
            self._web_socket_tests = os.path.abspath(
                os.path.join(self._root, 'websocket', 'tests'))
        else:
            try:
                self._layout_tests = self._port_obj.layout_tests_dir()
                self._web_socket_tests = os.path.join(self._layout_tests,
                     'websocket', 'tests')
            except:
                self._web_socket_tests = None

    def start(self):
        if not self._web_socket_tests:
            _log.info('No need to start %s server.' % self._server_name)
            return
        if self.is_running():
            raise PyWebSocketNotStarted('%s is already running.' %
                                        self._server_name)

        time_str = time.strftime('%d%b%Y-%H%M%S')
        if self._use_tls:
            log_prefix = _WSS_LOG_PREFIX
        else:
            log_prefix = _WS_LOG_PREFIX
        log_file_name = log_prefix + time_str

        # Remove old log files. We only need to keep the last ones.
        self.remove_log_files(self._output_dir, log_prefix)

        error_log = os.path.join(self._output_dir, log_file_name + "-err.txt")

        output_log = os.path.join(self._output_dir, log_file_name + "-out.txt")
        self._wsout = codecs.open(output_log, "w", "utf-8")

        python_interp = sys.executable
        pywebsocket_base = os.path.join(
            os.path.dirname(os.path.dirname(os.path.dirname(
            os.path.abspath(__file__)))), 'thirdparty',
            'autoinstalled', 'pywebsocket')
        pywebsocket_script = os.path.join(pywebsocket_base, 'mod_pywebsocket',
            'standalone.py')
        start_cmd = [
            python_interp, pywebsocket_script,
            '--server-host', '127.0.0.1',
            '--port', str(self._port),
            '--document-root', self._layout_tests,
            '--scan-dir', self._web_socket_tests,
            '--cgi-paths', '/websocket/tests',
            '--log-file', error_log,
        ]

        handler_map_file = os.path.join(self._web_socket_tests,
                                        'handler_map.txt')
        if os.path.exists(handler_map_file):
            _log.debug('Using handler_map_file: %s' % handler_map_file)
            start_cmd.append('--websock-handlers-map-file')
            start_cmd.append(handler_map_file)
        else:
            _log.warning('No handler_map_file found')

        if self._use_tls:
            start_cmd.extend(['-t', '-k', self._private_key,
                              '-c', self._certificate])

        env = self._port_obj.setup_environ_for_server()
        env['PYTHONPATH'] = (pywebsocket_base + os.path.pathsep +
                             env.get('PYTHONPATH', ''))

        _log.debug('Starting %s server on %d.' % (
                   self._server_name, self._port))
        _log.debug('cmdline: %s' % ' '.join(start_cmd))
        # FIXME: We should direct this call through Executive for testing.
        # Note: Not thread safe: http://bugs.python.org/issue2320
        self._process = subprocess.Popen(start_cmd,
                                         stdin=open(os.devnull, 'r'),
                                         stdout=self._wsout,
                                         stderr=subprocess.STDOUT,
                                         env=env)

        if self._use_tls:
            url = 'https'
        else:
            url = 'http'
        url = url + '://127.0.0.1:%d/' % self._port
        if not url_is_alive(url):
            if self._process.returncode == None:
                # FIXME: We should use a non-static Executive for easier
                # testing.
                Executive().kill_process(self._process.pid)
            with codecs.open(output_log, "r", "utf-8") as fp:
                for line in fp:
                    _log.error(line)
            raise PyWebSocketNotStarted(
                'Failed to start %s server on port %s.' %
                    (self._server_name, self._port))

        # Our process terminated already
        if self._process.returncode != None:
            raise PyWebSocketNotStarted(
                'Failed to start %s server.' % self._server_name)
        if self._pidfile:
            with codecs.open(self._pidfile, "w", "ascii") as file:
                file.write("%d" % self._process.pid)

    def stop(self, force=False):
        if not force and not self.is_running():
            return

        pid = None
        if self._process:
            pid = self._process.pid
        elif self._pidfile:
            with codecs.open(self._pidfile, "r", "ascii") as file:
                pid = int(file.read().strip())

        if not pid:
            raise PyWebSocketNotFound(
                'Failed to find %s server pid.' % self._server_name)

        _log.debug('Shutting down %s server %d.' % (self._server_name, pid))
        # FIXME: We should use a non-static Executive for easier testing.
        Executive().kill_process(pid)

        if self._process:
            # wait() is not threadsafe and can throw OSError due to:
            # http://bugs.python.org/issue1731717
            self._process.wait()
            self._process = None

        if self._wsout:
            self._wsout.close()
            self._wsout = None