# -*- coding: utf-8 -*-
# $Id$
"""
Test Manager Core - Web Server Abstraction Base Class.
"""
__copyright__ = \
"""
Copyright (C) 2012-2014 Oracle Corporation
This file is part of VirtualBox Open Source Edition (OSE), as
available from http://www.virtualbox.org. This file is free software;
General Public License (GPL) as published by the Free Software
Foundation, in version 2 as it comes in the "COPYING" file of the
VirtualBox OSE distribution. VirtualBox OSE is distributed in the
hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
The contents of this file may alternatively be used under the terms
of the Common Development and Distribution License Version 1.0
(CDDL) only, as it comes in the "COPYING.CDDL" file of the
VirtualBox OSE distribution, in which case the provisions of the
CDDL are applicable instead of those of the GPL.
You may elect to license modified versions of this file under the
terms and conditions of either the GPL or the CDDL or both.
"""
# Standard python imports.
import cgitb
import codecs;
import os
import sys
# Validation Kit imports.
"""
For exceptions raised by glue code.
"""
pass;
"""
Web server interface abstraction and some HTML utils.
"""
## Enables more debug output.
## The maximum number of characters to cache.
## Special getUserName return value.
# Debug
self._afnDebugInfo = [];
# HTTP header.
self._dHeaderFields = \
{ \
};
# Body.
self._sBodyType = None;
# Output.
#
# Get stuff.
#
"""
Returns a dictionary with the query parameters.
The parameter name is the key, the values are given as lists. If a
parameter is given more than once, the value is appended to the
existing dictionary entry.
"""
return dict();
"""
Returns the client address, as a string.
"""
raise WebServerGlueException('getClientAddr is not implemented');
"""
Gets the HTTP request method.
"""
return 'POST';
"""
Gets login name provided by Apache.
Returns kUnknownUser if not logged on.
"""
return WebServerGlueBase.ksUnknownUser;
"""
Gets scheme name (aka. access protocol) from request URL, i.e. 'http' or 'https'.
See also urlparse.scheme.
"""
return 'http';
"""
Gets the network location (server host name / ip) from the request URL.
See also urlparse.netloc.
"""
raise WebServerGlueException('getUrlNetLoc is not implemented');
"""
Gets the hirarchical path (relative to server) from the request URL.
See also urlparse.path.
Note! This includes the leading slash.
"""
raise WebServerGlueException('getUrlPath is not implemented');
"""
Gets the hirarchical base path (relative to server) from the request URL.
Note! This includes both a leading an trailing slash.
"""
if iLastSlash >= 0:
return sPath + '/';
"""
Gets the URL being accessed, sans parameters.
For instance this will return, "http://localhost/testmanager/admin.cgi"
when "http://localhost/testmanager/admin.cgi?blah=blah" is being access.
"""
"""
Gets the base URL (with trailing slash).
For instance this will return, "http://localhost/testmanager/" when
"http://localhost/testmanager/admin.cgi?blah=blah" is being access.
"""
"""
Gets the User-Agent field of the HTTP header, returning empty string
if not present.
"""
return '';
"""
Gets the Content-Type field of the HTTP header, parsed into a type
string and a dictionary.
"""
"""
Gets the content length.
Returns int.
"""
return 0;
"""
Returns file object for reading the HTML body.
"""
raise WebServerGlueException('getUrlPath is not implemented');
#
# Output stuff.
#
"""
Worker function which child classes can override.
"""
return True;
"""
Flushes the HTTP header.
"""
return None;
"""
Sets a header field.
"""
return True;
"""
Sets up redirection of the page.
Raises an exception if called too late.
"""
raise WebServerGlueException('setRedirect called after the header was written');
if iCode != 302:
return True;
"""
Worker function which child classes can override.
"""
return True;
"""
Writes chunk of HTML, making sure the HTTP header is flushed first.
"""
if self._sBodyType is None:
raise WebServerGlueException('Cannot use writeParameter when body type is "%s"' % (self._sBodyType, ));
return True;
"""
Writes a raw chunk the document. Can be binary or any encoding.
No caching.
"""
if self._sBodyType is None:
raise WebServerGlueException('Cannot use writeParameter when body type is "%s"' % (self._sBodyType, ));
self.flushHeader();
return True;
"""
Writes one or more reply parameters in a form style response. The names
and values in dParams are unencoded, this method takes care of that.
Note! This automatically changes the content type to
'application/x-www-form-urlencoded', if the header hasn't been flushed
already.
"""
if self._sBodyType is None:
if not self._fHeaderWrittenOut:
raise WebServerGlueException('Cannot use writeParams when content-type is "%s"' % \
raise WebServerGlueException('Cannot use writeParams when body type is "%s"' % (self._sBodyType, ));
return True;
"""
Flush the output.
"""
self.flushHeader();
return None;
#
# Paths.
#
"""
Gets the path to the TM 'webui' directory.
"""
#
# Error stuff & Debugging.
#
"""
Writes the error to a log file.
"""
# Easy solution for log file size: Only one report.
except: pass;
# Try write the log file.
try:
if aXcptInfo[0] is not None:
except:
return fRc;
"""
Displays a page with an error message.
"""
if sLogFile is not None:
# Reset buffering, hoping that nothing was flushed yet.
self._sBodyType = None;
if not self._fHeaderWrittenOut:
else:
# Write the error page.
'<body><h1>Test Manager Error:</h1>\n' +
else:
'===================\n'
'\n'
if aXcptInfo[0] is not None:
else:
'---------\n'
'\n');
else:
'----------\n'
'\n');
try:
"""
Dumps useful debug info.
"""
if fnWrite is None:
fnWrite('<html><head><title>Test Manage Debug Info</title></head>\n<body>\n');
fnWrite('</body></html>');
"""
Dumps dictionary.
"""
if fnWrite is None:
if fSorted:
fnWrite('<h2>%s</h2>\n'
'<table border="1"><tr><th>name</th><th>value</th></tr>\n' % (sName,));
+ '</td></tr>\n');
fnWrite('</table>\n');
else:
fnWrite('------------------------------------------------------------------------\n');
fnWrite('\n');
return True;
"""
Dumps array.
"""
if fnWrite is None:
fnWrite('<h2>%s</h2>\n'
'<table border="1"><tr><th>index</th><th>value</th></tr>\n' % (sName,));
fnWrite(' <tr><td>' + str(i) + '</td><td>' + webutils.escapeElem(str(aoStuff[i])) + '</td></tr>\n');
fnWrite('</table>\n');
else:
fnWrite('Index Value\n');
fnWrite('------------------------------------------------------------------------\n');
fnWrite('\n');
return True;
""" Dumps request parameters. """
if fnWrite is None:
try:
else:
return False;
""" Dumps os.environ. """
""" Dumps sys.argv. """
"""
Dump python info.
"""
dInfo = {};
"""
Dumps stuff to the error page and debug info page.
Should be extended by child classes when possible.
"""
return True;
"""
Prints to debug log (usually apache error log).
"""
else:
return True;
"""
Registers a debug info method for calling when the error page is shown.
The fnDebugInfo function takes two parameters. The first is this
object, the second is a boolean indicating html (True) or text (False)
output. The return value is ignored.
"""
return True;
"""
Unregisters a debug info method previously registered by
registerDebugInfoCallback.
"""
except: pass;
return True;