Commits

Anonymous committed e911d6d

* Default session storageType is now ram (Remi)
* logFile config option is now honored - ticket #51 (Remi)
* Added cpg.request.remoteAddr and cpg.request.remoteHost - ticket #34 (Eurleif)
* Improved baseurlfilter to handle x-forwarded-host - ticket #49 (Kender)

Comments (0)

Files changed (6)

     cpg.configOption.staticContentList = []
 
     # Variable used for session handling
-    cpg.configOption.sessionStorageType = ""
+    cpg.configOption.sessionStorageType = "ram"
     cpg.configOption.sessionTimeout = 60 # In minutes
     cpg.configOption.sessionCleanUpDelay = 60 # In minutes
     cpg.configOption.sessionCookieName = "CherryPySession"
     try:
         logToScreen = int(cpg.parsedConfigFile.get('server', 'logToScreen'))
     except:
-        logToScreen = 1
+        logToScreen = True
+    s = nowStr + ' ' + context + ' ' + level + msg
     if logToScreen:
-        print nowStr, context, level, msg
+        print s
+    if cpg.configOption.logFile:
+        f = open(cpg.configOption.logFile, 'ab')
+        f.write(s + '\n')
+        f.close()
 
 def _cpOnError():
     """ Default _cpOnError method """
 
     def address_string(self):
         """ Try to do a reverse DNS based on [server]reverseDNS in the config file """
-        if _reverseDNS: return BaseHTTPServer.BaseHTTPRequestHandler.address_string(self)
-        else: return self.client_address[0]
+        if cpg.configOption.reverseDNS:
+            return BaseHTTPServer.BaseHTTPRequestHandler.address_string(self)
+        else:
+            return self.client_address[0]
 
     def do_GET(self):
         """Serve a GET request."""
         cpg.request.method = 'GET'
-        _cphttptools.doRequest(self.raw_requestline, self.headers, self.rfile, self.wfile)
+        _cphttptools.doRequest(
+            self.client_address[0],
+            self.address_string(),
+            self.raw_requestline,
+            self.headers,
+            self.rfile,
+            self.wfile
+        )
 
     def do_HEAD(self): # Head is not implemented
         """Serve a HEAD request."""
         cpg.request.method = 'HEAD'
-        _cphttptools.doRequest(self.raw_requestline, self.headers, self.rfile, self.wfile)
+        _cphttptools.doRequest(
+            self.client_address[0],
+            self.address_string(),
+            self.raw_requestline,
+            self.headers,
+            self.rfile,
+            self.wfile
+        )
 
     def do_POST(self):
         """Serve a POST request."""
         cpg.request.method = 'POST'
-        _cphttptools.doRequest(self.raw_requestline, self.headers, self.rfile, self.wfile)
+        _cphttptools.doRequest(
+            self.client_address[0],
+            self.address_string(),
+            self.raw_requestline,
+            self.headers,
+            self.rfile,
+            self.wfile
+        )
 
     def setup(self):
         """ We have to override this to handle SSL
         cpg.request.queryString = cpg.request.path[i+1:]
         cpg.request.path = cpg.request.path[:i]
 
-def cookHeaders(headers, requestLine):
+def cookHeaders(clientAddress, remoteHost, headers, requestLine):
     """Process the headers into the request.headerMap"""
     cpg.request.headerMap = {}
     cpg.request.simpleCookie = Cookie.SimpleCookie()
     for cookie in cookieList:
         cpg.request.simpleCookie.load(cookie)
 
-    if not cpg.request.headerMap.has_key('Remote-Addr'):
-        try:
-            cpg.request.headerMap['Remote-Addr'] = self.client_address[0]
-            cpg.request.headerMap['Remote-Host'] = self.address_string()
-        except: pass
+    cpg.request.remoteAddr = clientAddress
+    cpg.request.remoteHost = remoteHost
 
     # Set peer_certificate (in SSL mode) so the web app can examinate the client certificate
     try: cpg.request.peerCertificate = self.request.get_peer_certificate()
     except: pass
 
-    _cputil.getSpecialFunction('_cpLogMessage')("%s - %s" % (cpg.request.headerMap.get('Remote-Addr', ''), requestLine[:-2]), "HTTP")
+    _cputil.getSpecialFunction('_cpLogMessage')("%s - %s" % (cpg.request.remoteAddr, requestLine[:-2]), "HTTP")
 
 
 def parsePostData(rfile):
     normalizedKey = '-'.join([s.capitalize() for s in key.split('-')])
     cpg.request.headerMap[normalizedKey] = value
 
-def initRequest(requestLine, headers, rfile, wfile):
+def initRequest(clientAddress, remoteHost, requestLine, headers, rfile, wfile):
     parseFirstLine(requestLine)
-    cookHeaders(headers, requestLine)
+    cookHeaders(clientAddress, remoteHost, headers, requestLine)
 
     cpg.request.base = "http://" + cpg.request.headerMap['Host']
     cpg.request.browserUrl = cpg.request.base + cpg.request.browserUrl
 
     applyFilterList('afterRequestBody')
 
-def doRequest(requestLine, headers, rfile, wfile):
-    initRequest(requestLine, headers, rfile, wfile)
+def doRequest(clientAddress, remoteHost, requestLine, headers, rfile, wfile):
+    initRequest(clientAddress, remoteHost, requestLine, headers, rfile, wfile)
 
     # Prepare response variables
     now = time.time()
             _cputil.getSpecialFunction('_cpOnError')()
 
             # Save session data
-            if cpg.configOption.sessionStorageType and not cpg.request.isStatic:
-                sessionId = cpg.response.simpleCookie[cpg.configOption.sessionCookieName].value
-                expirationTime = time.time() + cpg.configOption.sessionTimeout * 60
-                _cputil.getSpecialFunction('_cpSaveSessionData')(sessionId, cpg.request.sessionMap, expirationTime)
+            # if cpg.configOption.sessionStorageType and not cpg.request.isStatic:
+            #     sessionId = cpg.response.simpleCookie[cpg.configOption.sessionCookieName].value
+            #     expirationTime = time.time() + cpg.configOption.sessionTimeout * 60
+            #     _cputil.getSpecialFunction('_cpSaveSessionData')(sessionId, cpg.request.sessionMap, expirationTime)
 
             wfile.write('%s %s\r\n' % (cpg.response.headerMap['protocolVersion'], cpg.response.headerMap['Status']))
             if cpg.response.headerMap.has_key('Content-Length') and cpg.response.headerMap['Content-Length'] == 0:

lib/filter/baseurlfilter.py

     Useful when running a CP server behind Apache.
     """
 
-    def __init__(self, baseUrl = 'http://localhost'):
+    def __init__(self, baseUrl = 'http://localhost', useXForwardedHost = True):
         # New baseUrl
         self.baseUrl = baseUrl
+        self.useXForwardedHost = useXForwardedHost
 
     def afterRequestHeader(self):
+        if self.useXForwardedHost:
+            newBaseUrl = cpg.request.headerMap.get(
+                "X-Forwarded-Host", self.baseUrl)
+        else:
+            newBaseUrl = self.baseUrl
+
         cpg.request.browserUrl = cpg.request.browserUrl.replace(
-            cpg.request.base, self.baseUrl)
-        cpg.request.base = self.baseUrl
+            cpg.request.base, newBaseUrl)
+        cpg.request.base = newBaseUrl

lib/filter/gzipfilter.py

         self.mimeTypeList = mimeTypeList
 
     def beforeResponse(self):
+        if not cpg.response.body:
+            # Response body is empty (might be a 304 for instance)
+            return
         ct = cpg.response.headerMap.get('Content-Type').split(';')[0]
         ae = cpg.request.headerMap.get('Accept-Encoding', '')
         if (ct in self.mimeTypeList) and ('gzip' in ae):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.