1# Copyright (C) 2011 Google Inc. All rights reserved. 2# 3# Redistribution and use in source and binary forms, with or without 4# modification, are permitted provided that the following conditions are 5# met: 6# 7# * Redistributions of source code must retain the above copyright 8# notice, this list of conditions and the following disclaimer. 9# * Redistributions in binary form must reproduce the above 10# copyright notice, this list of conditions and the following disclaimer 11# in the documentation and/or other materials provided with the 12# distribution. 13# * Neither the name of Google Inc. nor the names of its 14# contributors may be used to endorse or promote products derived from 15# this software without specific prior written permission. 16# 17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29"""Start and stop the lighttpd server as it is used by the layout tests.""" 30 31import logging 32import time 33 34from webkitpy.layout_tests.servers import server_base 35 36 37_log = logging.getLogger(__name__) 38 39 40class Lighttpd(server_base.ServerBase): 41 42 def __init__(self, port_obj, output_dir): 43 super(Lighttpd, self).__init__(port_obj, output_dir) 44 self._name = 'lighttpd' 45 self._log_prefixes = ('access.log-', 'error.log-') 46 self._pid_file = self._filesystem.join(self._runtime_path, '%s.pid' % self._name) 47 48 self._layout_tests_dir = self._port_obj.layout_tests_dir() 49 50 self._webkit_tests = self._filesystem.join(self._layout_tests_dir, 'http', 'tests') 51 self._js_test_resource = self._filesystem.join(self._layout_tests_dir, 'resources') 52 self._media_resource = self._filesystem.join(self._layout_tests_dir, 'media') 53 54 # Self generated certificate for SSL server. 55 self._pem_file = self._filesystem.join(self._layout_tests_dir, 'http', 'conf', 'httpd2.pem') 56 57 self.mappings = [ 58 {'port': 8000, 'docroot': self._webkit_tests}, 59 {'port': 8080, 'docroot': self._webkit_tests}, 60 {'port': 8443, 'docroot': self._webkit_tests, 'sslcert': self._pem_file}, 61 ] 62 63 self._start_cmd = [ 64 self._port_obj.path_to_lighttpd(), 65 '-f', self._filesystem.join(self._output_dir, 'lighttpd.conf'), 66 '-m', self._port_obj.path_to_lighttpd_modules(), 67 '-D', 68 ] 69 self._env = self._port_obj.setup_environ_for_server() 70 71 def _prepare_config(self): 72 time_str = time.strftime("%d%b%Y-%H%M%S") 73 access_file_name = "access.log-" + time_str + ".txt" 74 access_log = self._filesystem.join(self._output_dir, access_file_name) 75 log_file_name = "error.log-" + time_str + ".txt" 76 error_log = self._filesystem.join(self._output_dir, log_file_name) 77 78 # Write out the config 79 base_conf_file = self._filesystem.join(self._layout_tests_dir, 'http', 'conf', 'lighttpd.conf') 80 out_conf_file = self._filesystem.join(self._output_dir, 'lighttpd.conf') 81 with self._filesystem.open_text_file_for_writing(out_conf_file) as f: 82 base_conf = self._filesystem.read_text_file(base_conf_file) 83 f.write(base_conf) 84 85 # Write out our cgi handlers. Run perl through env so that it 86 # processes the #! line and runs perl with the proper command 87 # line arguments. Emulate apache's mod_asis with a cat cgi handler. 88 f.write(('cgi.assign = ( ".cgi" => "/usr/bin/env",\n' 89 ' ".pl" => "/usr/bin/env",\n' 90 ' ".asis" => "/bin/cat",\n' 91 ' ".php" => "%s" )\n\n') % 92 self._port_obj.path_to_lighttpd_php()) 93 94 # Setup log files 95 f.write(('server.errorlog = "%s"\n' 96 'accesslog.filename = "%s"\n\n') % (error_log, access_log)) 97 98 # Setup upload folders. Upload folder is to hold temporary upload files 99 # and also POST data. This is used to support XHR layout tests that 100 # does POST. 101 f.write(('server.upload-dirs = ( "%s" )\n\n') % (self._output_dir)) 102 103 # Setup a link to where the js test templates are stored 104 f.write(('alias.url = ( "/js-test-resources" => "%s" )\n\n') % 105 (self._js_test_resource)) 106 107 # Setup a link to where the media resources are stored. 108 f.write(('alias.url += ( "/media-resources" => "%s" )\n\n') % 109 (self._media_resource)) 110 111 # dump out of virtual host config at the bottom. 112 for mapping in self.mappings: 113 ssl_setup = '' 114 if 'sslcert' in mapping: 115 ssl_setup = (' ssl.engine = "enable"\n' 116 ' ssl.pemfile = "%s"\n' % mapping['sslcert']) 117 118 f.write(('$SERVER["socket"] == "127.0.0.1:%d" {\n' 119 ' server.document-root = "%s"\n' + 120 ssl_setup + 121 '}\n\n') % (mapping['port'], mapping['docroot'])) 122 123 # Copy liblightcomp.dylib to /tmp/lighttpd/lib to work around the 124 # bug that mod_alias.so loads it from the hard coded path. 125 if self._port_obj.host.platform.is_mac(): 126 tmp_module_path = '/tmp/lighttpd/lib' 127 if not self._filesystem.exists(tmp_module_path): 128 self._filesystem.maybe_make_directory(tmp_module_path) 129 lib_file = 'liblightcomp.dylib' 130 self._filesystem.copyfile(self._filesystem.join(self._port_obj.path_to_lighttpd_modules(), lib_file), 131 self._filesystem.join(tmp_module_path, lib_file)) 132