Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 815

Show
Ignore:
Timestamp:
11/12/05 06:55:17
Author:
lawouach
Message:

Moved the code handling OPTIONS * request to _cputil to a special function attribute called _cpGlobalInformation.

Added support for printing the HTTP request headers when an error is logged. This can be controlled via server.showRequestHeaders in the config settings.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/_cphttptools.py

    r812 r815  
    6363            # right away. 
    6464            self.processRequestLine(requestLine) 
    65              
    66             # if we receive OPTIONS * HTTP/1.1 
    67             # then we should simply answer right away 
    68             if self.method == 'OPTIONS': 
    69                 # this normally should be * but processRequestLine 
    70                 # switched it to 'global' for the configuration handling 
    71                 if self.path == 'global': 
    72                     # OPTIONS is defined in HTTP 1.1 
    73                     response = cherrypy.response 
    74                     if response.version >= '1.1': 
    75                         response.body = [] 
    76                         response.status = '200 OK' 
    77                         response.headerMap['Allow'] = 'HEAD, GET, POST, PUT, OPTIONS' 
    78                         response.headerMap['Content-Length'] = 0 
    79                         response.headerMap['Content-Type'] = 'text/plain' 
    80                         response.finalize() 
    81                         _cputil.getSpecialAttribute("_cpLogAccess")() 
    82                         return 
     65 
     66            # handles "OPTIONS * HTTP/1.1" requests 
     67            # we could avoid testing for the method value here but 
     68            # we still do it to avoid an useless call to getSpecialAttribute 
     69            # OPTIONS request should be very rare compare to regular requests 
     70            # let's not add too much overhead 
     71            if cherrypy.request.method == 'OPTIONS': 
     72                if _cputil.getSpecialAttribute("_cpGlobalInformation")() == True: 
     73                    # if we had such a request we don't have to keep processing 
     74                    return 
    8375             
    8476            try: 
  • trunk/cherrypy/_cputil.py

    r807 r815  
    7171        raise cherrypy.HTTPError(500, msg) 
    7272 
     73def _cpGlobalInformation(): 
     74    """Handles OPTIONS * HTTP/1.1 requests""" 
     75    request = cherrypy.request 
     76    if request.method == 'OPTIONS': 
     77        # this normally should be * but processRequestLine 
     78        # switched it to 'global' for the configuration handling 
     79        if request.path == 'global': 
     80            # OPTIONS is defined in HTTP 1.1 
     81            if request.version >= '1.1': 
     82                response = cherrypy.response 
     83                response.body = [] 
     84                response.status = '200 OK' 
     85                response.headerMap['Allow'] = 'HEAD, GET, POST, PUT, OPTIONS' 
     86                response.headerMap['Content-Length'] = 0 
     87                response.headerMap['Content-Type'] = 'text/plain' 
     88                response.finalize() 
     89                getSpecialAttribute("_cpLogAccess")() 
     90                return True 
     91    return False 
     92 
    7393def logtime(): 
    7494    return '%04d/%02d/%02d %02d:%02d:%02d' % time.localtime(time.time())[:6] 
     
    103123     
    104124    level = _log_severity_levels.get(severity, "UNKNOWN") 
    105     s = logtime() + ' ' + context + ' ' + level + ' ' + msg 
     125     
     126    requestheaders = '' 
     127    if cherrypy.config.get('server.showRequestHeaders', True): 
     128        requestheaders = format_request_headers() 
     129         
     130    s = ' '.join((logtime(), context, level, requestheaders, msg)) 
    106131     
    107132    if cherrypy.config.get('server.logToScreen', True): 
     
    273298    return "".join(traceback.format_exception(*exc)) 
    274299 
     300def format_request_headers(): 
     301    """returns the list of request headers as a string separated by new lines, one header on each line""" 
     302    if hasattr(cherrypy.request, 'headerMap'): 
     303        headers = '\n'.join(': '.join((header, value)) for header, value in cherrypy.request.headerMap.items()) 
     304        return ''.join(('\n', headers, '\n', '\n')) 
     305    return '' 
     306 
    275307def bareError(extrabody=None): 
    276308    """Produce status, headers, body for a critical error. 
  • trunk/cherrypy/config.py

    r807 r815  
    3131        'server.logFileNotFound': True, 
    3232        'server.showTracebacks': True, 
     33        'server.showRequestHeaders': True, 
    3334        }, 
    3435    "staging": { 
     
    3738        'server.logFileNotFound': False, 
    3839        'server.showTracebacks': False, 
     40        'server.showRequestHeaders': False, 
    3941        }, 
    4042    "production": { 
     
    4345        'server.logFileNotFound': False, 
    4446        'server.showTracebacks': False, 
     47        'server.showRequestHeaders': False, 
    4548        }, 
    4649    } 

Hosted by WebFaction

Log in as guest/cpguest to create tickets