From 89845e2e9a1052a1bb3f3f3f42abfe07ee1a8ae0 Mon Sep 17 00:00:00 2001
From: "tony@chromium.org"
 <tony@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>
Date: Tue, 25 Aug 2009 18:22:41 +0000
Subject: [PATCH] Step 1 in porting page cycler http to linux.  Add http
 startup to the the platform utils.

BUG=15470

Review URL: http://codereview.chromium.org/174378


git-svn-id: svn://svn.chromium.org/chrome/trunk/src@24254 0039d316-1c4b-4281-b951-d872f2087c98
---
 .../google/httpd_config/httpd2_linux.conf     | 139 ++++++++++++++++++
 tools/python/google/platform_utils.py         |   4 +-
 tools/python/google/platform_utils_linux.py   | 137 +++++++++++++++++
 3 files changed, 278 insertions(+), 2 deletions(-)
 create mode 100644 tools/python/google/httpd_config/httpd2_linux.conf
 create mode 100644 tools/python/google/platform_utils_linux.py

diff --git a/tools/python/google/httpd_config/httpd2_linux.conf b/tools/python/google/httpd_config/httpd2_linux.conf
new file mode 100644
index 0000000000000..53d2bb8b9955a
--- /dev/null
+++ b/tools/python/google/httpd_config/httpd2_linux.conf
@@ -0,0 +1,139 @@
+Listen 127.0.0.1:8000
+
+
+#
+# Timeout: The number of seconds before receives and sends time out.
+#
+Timeout 300
+
+#
+# KeepAlive: Whether or not to allow persistent connections (more than
+# one request per connection). Set to "Off" to deactivate.
+#
+KeepAlive On
+
+#
+# MaxKeepAliveRequests: The maximum number of requests to allow
+# during a persistent connection. Set to 0 to allow an unlimited amount.
+# We recommend you leave this number high, for maximum performance.
+#
+MaxKeepAliveRequests 100
+
+#
+# KeepAliveTimeout: Number of seconds to wait for the next request from the
+# same client on the same connection.
+#
+KeepAliveTimeout 15
+
+##
+## Server-Pool Size Regulation (MPM specific)
+## 
+
+# prefork MPM
+# StartServers: number of server processes to start
+# MinSpareServers: minimum number of server processes which are kept spare
+# MaxSpareServers: maximum number of server processes which are kept spare
+# MaxClients: maximum number of server processes allowed to start
+# MaxRequestsPerChild: maximum number of requests a server process serves
+<IfModule mpm_prefork_module>
+    StartServers          5
+    MinSpareServers       5
+    MaxSpareServers      10
+    MaxClients          150
+    MaxRequestsPerChild   0
+</IfModule>
+
+# worker MPM
+# StartServers: initial number of server processes to start
+# MaxClients: maximum number of simultaneous client connections
+# MinSpareThreads: minimum number of worker threads which are kept spare
+# MaxSpareThreads: maximum number of worker threads which are kept spare
+# ThreadsPerChild: constant number of worker threads in each server process
+# MaxRequestsPerChild: maximum number of requests a server process serves
+<IfModule mpm_worker_module>
+    StartServers          2
+    MaxClients          150
+    MinSpareThreads      25
+    MaxSpareThreads      75 
+    ThreadsPerChild      25
+    MaxRequestsPerChild   0
+</IfModule>
+
+#
+# AccessFileName: The name of the file to look for in each directory
+# for additional configuration directives.  See also the AllowOverride
+# directive.
+#
+
+AccessFileName .htaccess
+
+#
+# The following lines prevent .htaccess and .htpasswd files from being 
+# viewed by Web clients. 
+#
+<Files ~ "^\.ht">
+    Order allow,deny
+    Deny from all
+</Files>
+
+#
+# DefaultType is the default MIME type the server will use for a document
+# if it cannot otherwise determine one, such as from filename extensions.
+# If your server contains mostly text or HTML documents, "text/plain" is
+# a good value.  If most of your content is binary, such as applications
+# or images, you may want to use "application/octet-stream" instead to
+# keep browsers from trying to display binary files as though they are
+# text.
+#
+DefaultType text/plain
+
+
+#
+# HostnameLookups: Log the names of clients or just their IP addresses
+# e.g., www.apache.org (on) or 204.62.129.132 (off).
+# The default is off because it'd be overall better for the net if people
+# had to knowingly turn this feature on, since enabling it means that
+# each client request will result in AT LEAST one lookup request to the
+# nameserver.
+#
+HostnameLookups Off
+
+#
+# LogLevel: Control the number of messages logged to the error_log.
+# Possible values include: debug, info, notice, warn, error, crit,
+# alert, emerg.
+#
+LogLevel warn
+
+Include /etc/apache2/mods-enabled/*.load
+Include /etc/apache2/mods-enabled/*.conf
+
+#
+# The following directives define some format nicknames for use with
+# a CustomLog directive (see below).
+# If you are behind a reverse proxy, you might want to change %h into %{X-Forwarded-For}i
+#
+LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
+LogFormat "%h %l %u %t \"%r\" %>s %b" common
+LogFormat "%{Referer}i -> %U" referer
+LogFormat "%{User-agent}i" agent
+
+#
+# ServerTokens
+# This directive configures what you return as the Server HTTP response
+# Header. The default is 'Full' which sends information about the OS-Type
+# and compiled in modules.
+# Set to one of:  Full | OS | Minor | Minimal | Major | Prod
+# where Full conveys the most information, and Prod the least.
+#
+ServerTokens Full
+
+#
+# Optionally add a line containing the server version and virtual host
+# name to server-generated pages (internal error documents, FTP directory 
+# listings, mod_status and mod_info output etc., but not CGI generated 
+# documents or custom error documents).
+# Set to "EMail" to also include a mailto: link to the ServerAdmin.
+# Set to one of:  On | Off | EMail
+#
+ServerSignature On
diff --git a/tools/python/google/platform_utils.py b/tools/python/google/platform_utils.py
index 5bbcdf800ec45..b2caa0f6fd96d 100644
--- a/tools/python/google/platform_utils.py
+++ b/tools/python/google/platform_utils.py
@@ -20,5 +20,5 @@ if sys.platform in ('cygwin', 'win32'):
   from platform_utils_win import *
 elif sys.platform == 'darwin':
   from platform_utils_mac import *
-
-
+elif sys.platform == 'linux2':
+  from platform_utils_linux import *
diff --git a/tools/python/google/platform_utils_linux.py b/tools/python/google/platform_utils_linux.py
new file mode 100644
index 0000000000000..a735111c57540
--- /dev/null
+++ b/tools/python/google/platform_utils_linux.py
@@ -0,0 +1,137 @@
+#!/usr/bin/python
+# Copyright (c) 2006-2009 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.
+
+"""Platform-specific utility methods shared by several scripts."""
+
+import os
+import subprocess
+
+import google.path_utils
+
+
+class PlatformUtility(object):
+  def __init__(self, base_dir):
+    """Args:
+         base_dir: the base dir for running tests.
+    """
+    self._base_dir = base_dir
+    self._httpd_cmd_string = None  # used for starting/stopping httpd
+    self._bash = "/bin/bash"
+
+  def _UnixRoot(self):
+    """Returns the path to root."""
+    return "/"
+
+  def GetFilesystemRoot(self):
+    """Returns the root directory of the file system."""
+    return self._UnixRoot()
+
+  def GetTempDirectory(self):
+    """Returns the file system temp directory
+
+    Note that this does not use a random subdirectory, so it's not
+    intrinsically secure.  If you need a secure subdir, use the tempfile
+    package.
+    """
+    return os.getenv("TMPDIR", "/tmp")
+
+  def FilenameToUri(self, path, use_http=False, use_ssl=False, port=8000):
+    """Convert a filesystem path to a URI.
+
+    Args:
+      path: For an http URI, the path relative to the httpd server's
+          DocumentRoot; for a file URI, the full path to the file.
+      use_http: if True, returns a URI of the form http://127.0.0.1:8000/.
+          If False, returns a file:/// URI.
+      use_ssl: if True, returns HTTPS URL (https://127.0.0.1:8000/).
+          This parameter is ignored if use_http=False.
+      port: The port number to append when returning an HTTP URI
+    """
+    if use_http:
+      protocol = 'http'
+      if use_ssl:
+        protocol = 'https'
+      return "%s://127.0.0.1:%d/%s" % (protocol, port, path)
+    return "file://" + path
+
+  def GetStartHttpdCommand(self, output_dir,
+                           httpd_conf_path, mime_types_path,
+                           document_root=None, apache2=False):
+    """Prepares the config file and output directory to start an httpd server.
+    Returns a list of strings containing the server's command line+args.
+
+    Args:
+      output_dir: the path to the server's output directory, for log files.
+          It will be created if necessary.
+      httpd_conf_path: full path to the httpd.conf file to be used.
+      mime_types_path: full path to the mime.types file to be used.
+      document_root: full path to the DocumentRoot.  If None, the DocumentRoot
+          from the httpd.conf file will be used.  Note that the httpd.conf
+          file alongside this script does not specify any DocumentRoot, so if
+          you're using that one, be sure to specify a document_root here.
+      apache2: boolean if true will cause this function to return start
+               command for Apache 2.x as opposed to Apache 1.3.x. This flag
+               is ignored on Linux (but preserved here for compatibility in
+               function signature with win), where apache2 is used always
+    """
+
+    exe_name = "apache2"
+    cert_file = google.path_utils.FindUpward(self._base_dir, 'tools',
+                                             'python', 'google',
+                                             'httpd_config', 'httpd2.pem')
+    httpd_vars = {
+      "httpd_executable_path":
+          os.path.join(self._UnixRoot(), "usr", "sbin", exe_name),
+      "httpd_conf_path": httpd_conf_path,
+      "ssl_certificate_file": cert_file,
+      "document_root" : document_root,
+      "server_root": os.path.join(self._UnixRoot(), "usr"),
+      "mime_types_path": mime_types_path,
+      "output_dir": output_dir,
+      "user": os.environ.get("USER", "#%d" % os.geteuid()),
+      "lock_file": os.path.join(output_dir, "accept.lock"),
+    }
+
+    google.path_utils.MaybeMakeDirectory(output_dir)
+
+    # We have to wrap the command in bash
+    # -C: process directive before reading config files
+    # -c: process directive after reading config files
+    # Apache wouldn't run CGIs with permissions==700 unless we add
+    # -c User "<username>"
+    httpd_cmd_string = (
+      '%(httpd_executable_path)s'
+      ' -f %(httpd_conf_path)s'
+      ' -c \'TypesConfig "%(mime_types_path)s"\''
+      ' -c \'CustomLog "%(output_dir)s/access_log.txt" common\''
+      ' -c \'ErrorLog "%(output_dir)s/error_log.txt"\''
+      ' -c \'PidFile "%(output_dir)s/httpd.pid"\''
+      ' -C \'User "%(user)s"\''
+      ' -C \'ServerRoot "%(server_root)s"\''
+      ' -c \'LockFile "%(lock_file)s"\''
+      ' -c \'SSLCertificateFile "%(ssl_certificate_file)s"\''
+    )
+
+    if document_root:
+      httpd_cmd_string += ' -C \'DocumentRoot "%(document_root)s"\''
+    # Save a copy of httpd_cmd_string to use for stopping httpd
+    self._httpd_cmd_string = httpd_cmd_string % httpd_vars
+
+    httpd_cmd = [self._bash, "-c", self._httpd_cmd_string]
+    return httpd_cmd
+
+  def GetStopHttpdCommand(self):
+    """Returns a list of strings that contains the command line+args needed to
+    stop the http server used in the http tests.
+
+    This tries to fetch the pid of httpd (if available) and returns the
+    command to kill it. If pid is not available, kill all httpd processes
+    """
+
+    if not self._httpd_cmd_string:
+      return ["true"]   # Haven't been asked for the start cmd yet. Just pass.
+    # Add a sleep after the shutdown because sometimes it takes some time for
+    # the port to be available again.
+    return [self._bash, "-c", self._httpd_cmd_string + ' -k stop && sleep 5']
-- 
GitLab