| 1 |
""" |
|---|
| 2 |
Copyright (c) 2004, CherryPy Team (team@cherrypy.org) |
|---|
| 3 |
All rights reserved. |
|---|
| 4 |
|
|---|
| 5 |
Redistribution and use in source and binary forms, with or without modification, |
|---|
| 6 |
are permitted provided that the following conditions are met: |
|---|
| 7 |
|
|---|
| 8 |
* Redistributions of source code must retain the above copyright notice, |
|---|
| 9 |
this list of conditions and the following disclaimer. |
|---|
| 10 |
* Redistributions in binary form must reproduce the above copyright notice, |
|---|
| 11 |
this list of conditions and the following disclaimer in the documentation |
|---|
| 12 |
and/or other materials provided with the distribution. |
|---|
| 13 |
* Neither the name of the CherryPy Team nor the names of its contributors |
|---|
| 14 |
may be used to endorse or promote products derived from this software |
|---|
| 15 |
without specific prior written permission. |
|---|
| 16 |
|
|---|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|---|
| 18 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|---|
| 19 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|---|
| 20 |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
|---|
| 21 |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|---|
| 22 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|---|
| 23 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|---|
| 24 |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
|---|
| 25 |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 26 |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 27 |
""" |
|---|
| 28 |
|
|---|
| 29 |
import _cputil, ConfigParser, cpg |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
def setDefaultConfigOption(): |
|---|
| 33 |
""" Return an EmptyClass instance with the default config options """ |
|---|
| 34 |
|
|---|
| 35 |
cpg.configOption = _cputil.EmptyClass() |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
cpg.configOption.logToScreen = 1 |
|---|
| 41 |
cpg.configOption.logFile = '' |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
cpg.configOption.socketHost = '' |
|---|
| 47 |
cpg.configOption.socketPort = 8080 |
|---|
| 48 |
cpg.configOption.socketFile = '' |
|---|
| 49 |
|
|---|
| 50 |
cpg.configOption.reverseDNS = 0 |
|---|
| 51 |
cpg.configOption.socketQueueSize = 5 |
|---|
| 52 |
cpg.configOption.protocolVersion = "HTTP/1.0" |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
cpg.configOption.threadPool = 0 |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
cpg.configOption.sslKeyFile = "" |
|---|
| 60 |
cpg.configOption.sslCertificateFile = "" |
|---|
| 61 |
cpg.configOption.sslClientCertificateVerification = 0 |
|---|
| 62 |
cpg.configOption.sslCACertificateFile = "" |
|---|
| 63 |
cpg.configOption.sslVerifyDepth = 1 |
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
cpg.configOption.flushCacheDelay=0 |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
cpg.configOption.debugMode=0 |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
cpg.configOption.staticContentList = [] |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
cpg.configOption.sessionStorageType = "" |
|---|
| 76 |
cpg.configOption.sessionTimeout = 60 |
|---|
| 77 |
cpg.configOption.sessionCleanUpDelay = 60 |
|---|
| 78 |
cpg.configOption.sessionCookieName = "CherryPySession" |
|---|
| 79 |
cpg.configOption.sessionStorageFileDir = "" |
|---|
| 80 |
|
|---|
| 81 |
def parseConfigFile(configFile = None, parsedConfigFile = None): |
|---|
| 82 |
""" |
|---|
| 83 |
Parse the config file and set values in cpg.configOption |
|---|
| 84 |
""" |
|---|
| 85 |
_cpLogMessage = _cputil.getSpecialFunction('_cpLogMessage') |
|---|
| 86 |
if configFile: |
|---|
| 87 |
cpg.parsedConfigFile = ConfigParser.ConfigParser() |
|---|
| 88 |
if hasattr(configFile, 'read'): |
|---|
| 89 |
_cpLogMessage("Reading infos from configFile stream", 'CONFIG') |
|---|
| 90 |
cpg.parsedConfigFile.readfp(configFile) |
|---|
| 91 |
else: |
|---|
| 92 |
_cpLogMessage("Reading infos from configFile: %s" % configFile, 'CONFIG') |
|---|
| 93 |
cpg.parsedConfigFile.read(configFile) |
|---|
| 94 |
else: |
|---|
| 95 |
cpg.parsedConfigFile = parsedConfigFile |
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
for sectionName, optionName, valueType in [ |
|---|
| 99 |
('server', 'logToScreen', 'int'), |
|---|
| 100 |
('server', 'logFile', 'str'), |
|---|
| 101 |
('server', 'socketHost', 'str'), |
|---|
| 102 |
('server', 'protocolVersion', 'str'), |
|---|
| 103 |
('server', 'socketPort', 'int'), |
|---|
| 104 |
('server', 'socketFile', 'str'), |
|---|
| 105 |
('server', 'reverseDNS', 'int'), |
|---|
| 106 |
('server', 'threadPool', 'int'), |
|---|
| 107 |
('server', 'sslKeyFile', 'str'), |
|---|
| 108 |
('server', 'sslCertificateFile', 'str'), |
|---|
| 109 |
('server', 'sslClientCertificateVerification', 'int'), |
|---|
| 110 |
('server', 'sslCACertificateFile', 'str'), |
|---|
| 111 |
('server', 'sslVerifyDepth', 'int'), |
|---|
| 112 |
('session', 'storageType', 'str'), |
|---|
| 113 |
('session', 'timeout', 'float'), |
|---|
| 114 |
('session', 'cleanUpDelay', 'float'), |
|---|
| 115 |
('session', 'cookieName', 'str'), |
|---|
| 116 |
('session', 'storageFileDir', 'str') |
|---|
| 117 |
]: |
|---|
| 118 |
try: |
|---|
| 119 |
value = cpg.parsedConfigFile.get(sectionName, optionName) |
|---|
| 120 |
if valueType == 'int': value = int(value) |
|---|
| 121 |
elif valueType == 'float': value = float(value) |
|---|
| 122 |
if sectionName == 'session': |
|---|
| 123 |
optionName = 'session' + optionName[0].upper() + optionName[1:] |
|---|
| 124 |
setattr(cpg.configOption, optionName, value) |
|---|
| 125 |
except: |
|---|
| 126 |
pass |
|---|
| 127 |
|
|---|
| 128 |
try: |
|---|
| 129 |
staticDirList = cpg.parsedConfigFile.options('staticContent') |
|---|
| 130 |
for staticDir in staticDirList: |
|---|
| 131 |
staticDirTarget = cpg.parsedConfigFile.get('staticContent', staticDir) |
|---|
| 132 |
cpg.configOption.staticContentList.append((staticDir, staticDirTarget)) |
|---|
| 133 |
except: pass |
|---|
| 134 |
|
|---|
| 135 |
def outputConfigOptions(): |
|---|
| 136 |
_cpLogMessage = _cputil.getSpecialFunction('_cpLogMessage') |
|---|
| 137 |
_cpLogMessage("Server parameters:", 'CONFIG') |
|---|
| 138 |
_cpLogMessage(" logToScreen: %s" % cpg.configOption.logToScreen, 'CONFIG') |
|---|
| 139 |
_cpLogMessage(" logFile: %s" % cpg.configOption.logFile, 'CONFIG') |
|---|
| 140 |
_cpLogMessage(" protocolVersion: %s" % cpg.configOption.protocolVersion, 'CONFIG') |
|---|
| 141 |
_cpLogMessage(" socketHost: %s" % cpg.configOption.socketHost, 'CONFIG') |
|---|
| 142 |
_cpLogMessage(" socketPort: %s" % cpg.configOption.socketPort, 'CONFIG') |
|---|
| 143 |
_cpLogMessage(" socketFile: %s" % cpg.configOption.socketFile, 'CONFIG') |
|---|
| 144 |
_cpLogMessage(" reverseDNS: %s" % cpg.configOption.reverseDNS, 'CONFIG') |
|---|
| 145 |
_cpLogMessage(" socketQueueSize: %s" % cpg.configOption.socketQueueSize, 'CONFIG') |
|---|
| 146 |
_cpLogMessage(" threadPool: %s" % cpg.configOption.threadPool, 'CONFIG') |
|---|
| 147 |
_cpLogMessage(" sslKeyFile: %s" % cpg.configOption.sslKeyFile, 'CONFIG') |
|---|
| 148 |
if cpg.configOption.sslKeyFile: |
|---|
| 149 |
_cpLogMessage(" sslCertificateFile: %s" % cpg.configOption.sslCertificateFile, 'CONFIG') |
|---|
| 150 |
_cpLogMessage(" sslClientCertificateVerification: %s" % cpg.configOption.sslClientCertificateVerification, 'CONFIG') |
|---|
| 151 |
_cpLogMessage(" sslCACertificateFile: %s" % cpg.configOption.sslCACertificateFile, 'CONFIG') |
|---|
| 152 |
_cpLogMessage(" sslVerifyDepth: %s" % cpg.configOption.sslVerifyDepth, 'CONFIG') |
|---|
| 153 |
_cpLogMessage(" flushCacheDelay: %s min" % cpg.configOption.flushCacheDelay, 'CONFIG') |
|---|
| 154 |
_cpLogMessage(" sessionStorageType: %s" % cpg.configOption.sessionStorageType, 'CONFIG') |
|---|
| 155 |
if cpg.configOption.sessionStorageType: |
|---|
| 156 |
_cpLogMessage(" sessionTimeout: %s min" % cpg.configOption.sessionTimeout, 'CONFIG') |
|---|
| 157 |
_cpLogMessage(" cleanUpDelay: %s min" % cpg.configOption.sessionCleanUpDelay, 'CONFIG') |
|---|
| 158 |
_cpLogMessage(" sessionCookieName: %s" % cpg.configOption.sessionCookieName, 'CONFIG') |
|---|
| 159 |
_cpLogMessage(" sessionStorageFileDir: %s" % cpg.configOption.sessionStorageFileDir, 'CONFIG') |
|---|
| 160 |
_cpLogMessage(" staticContent: %s" % cpg.configOption.staticContentList, 'CONFIG') |
|---|
| 161 |
|
|---|
| 162 |
def dummy(): |
|---|
| 163 |
|
|---|
| 164 |
if _protocolVersion not in ("HTTP/1.1", "HTTP/1.0"): |
|---|
| 165 |
raise "CherryError: protocolVersion must be 'HTTP/1.1' or 'HTTP/1.0'" |
|---|
| 166 |
if _reverseDNS not in (0,1): raise "CherryError: reverseDNS must be '0' or '1'" |
|---|
| 167 |
if _socketFile and not hasattr(socket, 'AF_UNIX'): raise "CherryError: Configuration file has socketFile, but this is only available on Unix machines" |
|---|
| 168 |
if _sslKeyFile: |
|---|
| 169 |
try: |
|---|
| 170 |
global SSL |
|---|
| 171 |
from OpenSSL import SSL |
|---|
| 172 |
except: raise "CherryError: PyOpenSSL 0.5.1 or later must be installed to use SSL. You can get it from http://pyopenssl.sourceforge.net" |
|---|
| 173 |
if _socketPort and _socketFile: raise "CherryError: In configuration file: socketPort and socketFile conflict with each other" |
|---|
| 174 |
if not _socketFile and not _socketPort: _socketPort=8000 |
|---|
| 175 |
if _sslKeyFile and not _sslCertificateFile: raise "CherryError: Configuration file has sslKeyFile but no sslCertificateFile" |
|---|
| 176 |
if _sslCertificateFile and not _sslKeyFile: raise "CherryError: Configuration file has sslCertificateFile but no sslKeyFile" |
|---|
| 177 |
try: sys.stdout.flush() |
|---|
| 178 |
except: pass |
|---|
| 179 |
|
|---|
| 180 |
if _sessionStorageType not in ('', 'custom', 'ram', 'file', 'cookie'): raise "CherryError: Configuration file an invalid sessionStorageType: '%s'"%_sessionStorageType |
|---|
| 181 |
if _sessionStorageType in ('custom', 'ram', 'cookie') and _sessionStorageFileDir!='': raise "CherryError: Configuration file has sessionStorageType set to 'custom, 'ram' or 'cookie' but a sessionStorageFileDir is specified" |
|---|
| 182 |
if _sessionStorageType=='file' and _sessionStorageFileDir=='': raise "CherryError: Configuration file has sessionStorageType set to 'file' but no sessionStorageFileDir" |
|---|
| 183 |
|
|---|