-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCGIWrapper.py
More file actions
executable file
·683 lines (560 loc) · 22.6 KB
/
Copy pathCGIWrapper.py
File metadata and controls
executable file
·683 lines (560 loc) · 22.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
#!/usr/bin/env python
"""CGIWrapper.py
Webware for Python
See the CGIWrapper.html documentation for more information.
"""
# We first record the starting time, in case we're being run as a CGI script.
from time import time, localtime, asctime
serverStartTime = time()
# Some imports
import cgi, os, sys, traceback
from random import randint
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
if '' not in sys.path:
sys.path.insert(0, '')
from Properties import version
try:
import WebUtils
except ImportError:
sys.path.append(os.path.abspath('..'))
import WebUtils
from WebUtils.HTMLForException import HTMLForException
from MiscUtils.NamedValueAccess import valueForName
class CGIWrapper(object):
"""The CGI Wrapper class.
A CGI wrapper executes a target script and provides various services
for both the script and website developer and the administrator.
See the CGIWrapper.html documentation for full information.
"""
## Init ##
def __init__(self):
self._config = self.config()
## Configuration ##
def defaultConfig(self):
"""Return a dictionary with the default configuration.
Subclasses could override to customize the values
or where they're taken from.
"""
return dict(
ScriptsHomeDir = 'Examples',
ChangeDir = True,
ExtraPaths = [],
ExtraPathsIndex = 1,
LogScripts = True,
ScriptLogFilename = 'Scripts.csv',
ScriptLogColumns = [
'environ.REMOTE_ADDR',
'environ.REQUEST_METHOD', 'environ.REQUEST_URI',
'responseSize', 'scriptName',
'serverStartTimeStamp', 'serverDuration',
'scriptDuration', 'errorOccurred'
],
ClassNames = ['', 'Page'],
ShowDebugInfoOnErrors = True,
UserErrorMessage = 'The site is having technical difficulties'
' with this page. An error has been logged, and the problem'
' will be fixed as soon as possible. Sorry!',
LogErrors = True,
ErrorLogFilename = 'Errors.csv',
SaveErrorMessages = True,
ErrorMessagesDir = 'ErrorMsgs',
EmailErrors = False,
ErrorEmailServer = 'localhost',
ErrorEmailHeaders = {
'From': 'webware@mydomain',
'To': ['webware@mydomain'],
'Reply-To': 'webware@mydomain',
'Content-Type': 'text/html',
'Subject': 'Error'
},
AdminRemoteAddr = ['127.0.0.1']
)
def configFilename(self):
"""Return the filename of the optional configuration file."""
return 'CGIWrapper.config'
def userConfig(self):
"""Return a dictionary with the user configuration.
This are overrides found in the optional configuration file,
or {} if there is no such file. The config filename is taken
from configFilename().
"""
try:
f = open(self.configFilename())
except IOError:
return {}
else:
config = f.read()
config = eval('dict(%s)' % config)
f.close()
assert isinstance(config, dict)
return config
def config(self):
"""Return the configuration for the CGIWrapper.
This is a combination of defaultConfig() and userConfig().
This method does no caching.
"""
config = self.defaultConfig()
config.update(self.userConfig())
return config
def setting(self, name):
"""Return the value of a particular setting in the configuration."""
return self._config[name]
## Utilities ##
def docType(self):
return docType()
def makeHeaders(self):
"""Return a default header dictionary with Content-Type entry."""
return {'Content-Type': 'text/html'}
def makeFieldStorage(self):
"""Return a default field storage object created from the cgi module."""
return cgi.FieldStorage()
def enhanceThePath(self):
"""Enhance sys.path according to our configuration."""
extraPathsIndex = self.setting('ExtraPathsIndex')
sys.path[extraPathsIndex:extraPathsIndex] = self.setting('ExtraPaths')
def environ(self):
"""Get the environment for the request."""
return self._environ
def requireEnvs(self, names):
"""Check that given environment variable names exist.
If they don't, a basic HTML error message is printed and we exit.
"""
badNames = [name for name in names if name not in self._environ]
if badNames:
print 'Content-Type: text/html'
print
print docType()
print '<html><head><title>Error</title></head><body>'
print '<p>ERROR: Missing %s</p>' % ', '.join(badNames)
print '</body></html>'
sys.exit(0)
def scriptPathname(self):
"""Return the full pathname of the target script.
Scripts that start with an underscore are special -- they run
out of the same directory as the CGI Wrapper and are typically
CGI Wrapper support scripts.
"""
# remove the CGI Wrapper's filename part
pathname = os.path.split(self._environ['SCRIPT_FILENAME'])[0]
filename = self._environ['PATH_INFO'][1:]
ext = os.path.splitext(filename)[1]
if ext:
# Hmmm, some kind of extension like maybe '.html'.
# Leave out the 'ScriptsHomeDir' and leave the extension alone.
filename = os.path.join(pathname, filename)
self._servingScript = False
else:
# No extension - we assume a Python CGI script.
if filename.startswith('_'):
# Underscores denote private scripts packaged with CGI Wrapper,
# such as '_admin.py'.
if self.setting('AdminRemoteAddr'):
# Users with the wrong remote address are redirected
# to the access denied script.
self.requireEnvs(['REMOTE_ADDR'])
remoteAddr = self._environ['REMOTE_ADDR'] + '.'
for addr in self.setting('AdminRemoteAddr'):
if remoteAddr.startswith(addr + '.'):
break
else:
filename = '_accessDenied'
filename = os.path.join(pathname, filename + '.py')
else:
# All other scripts are based in the directory named
# by the 'ScriptsHomeDir' setting.
filename = os.path.join(pathname,
self.setting('ScriptsHomeDir'), filename + '.py')
self._servingScript = True
return filename
def writeScriptLog(self):
"""Write an entry to the script log file.
Uses settings ScriptLogFilename and ScriptLogColumns.
"""
filename = self.setting('ScriptLogFilename')
if os.path.exists(filename):
f = open(filename, 'a')
else:
f = open(filename, 'w')
f.write(','.join(self.setting('ScriptLogColumns')) + '\n')
values = []
for column in self.setting('ScriptLogColumns'):
value = valueForName(self, column)
if isinstance(value, float):
# might need more flexibility in the future
value = '%0.4f' % value
else:
value = str(value)
values.append(value)
f.write(','.join(values) + '\n')
f.close()
def version(self):
return '.'.join(map(str, version))
## Exception handling ##
def handleException(self, excInfo):
"""Handle an exception in the target script.
Invoked by self when an exception occurs in the target script.
<code>excInfo</code> is a sys.exc_info()-style tuple of information
about the exception.
"""
# Note the duration of the script and time of the exception
self._scriptEndTime = time()
self.logExceptionToConsole()
self.reset()
print self.htmlErrorPage(
showDebugInfo=self.setting('ShowDebugInfoOnErrors'))
fullErrorMsg = None
if self.setting('SaveErrorMessages'):
fullErrorMsg = self.htmlErrorPage(showDebugInfo=True)
filename = self.saveHTMLErrorPage(fullErrorMsg)
else:
filename = ''
self.logExceptionToDisk(filename)
if self.setting('EmailErrors'):
if fullErrorMsg is None:
fullErrorMsg = self.htmlErrorPage(showDebugInfo=True)
self.emailException(fullErrorMsg)
def logExceptionToConsole(self, stderr=sys.stderr):
"""Log an exception in the target script.
Logs the time, script name and traceback to the console
(typically stderr). This usually results in the information
appearing in the web server's error log. Used by handleException().
"""
# stderr logging
stderr.write('[%s] [error] CGI Wrapper:'
' Error while executing script %s\n' % (
asctime(localtime(self._scriptEndTime)), self._scriptPathname))
traceback.print_exc(file=stderr)
def reset(self):
"""Reset CGI output.
Used by handleException() to clear out the current CGI output results
in preparation of delivering an HTML error message page.
Currently resets headers and deletes cookies, if present.
"""
# Set headers to basic text/html. We don't want stray headers
# from a script that failed.
self._headers = self.makeHeaders()
# Get rid of cookies, too
if 'cookies' in self._namespace:
del self._namespace['cookies']
def htmlErrorPage(self, showDebugInfo=True):
"""Return an HTML page explaining that there is an error.
There could be more options in the future, so using named arguments
(e.g. showDebugInfo=False) is recommended. Invoked by handleException().
"""
html = ['''%s
<html>
<title>Error</title>
<body text="black" bgcolor="white">
%s<p>%s</p>
''' % (docType(), htTitle('Error'), self.setting('UserErrorMessage'))]
if self.setting('ShowDebugInfoOnErrors'):
html.append(self.htmlDebugInfo())
html.append('</body></html>')
return ''.join(html)
def htmlDebugInfo(self):
"""Return an HTML page with debugging info on the current exception.
Used by handleException().
"""
html = ['''
%s<p><i>%s</i></p>
''' % (htTitle('Traceback'), self._scriptPathname)]
html.append(HTMLForException())
html.extend([
htTitle('Misc Info'),
htDictionary({
'time': asctime(localtime(self._scriptEndTime)),
'filename': self._scriptPathname,
'os.getcwd()': os.getcwd(),
'sys.path': sys.path
}),
htTitle('Fields'), htDictionary(self._fields),
htTitle('Headers'), htDictionary(self._headers),
htTitle('Environment'), htDictionary(self._environ, {'PATH': ';'}),
htTitle('Ids'), htTable(osIdTable(), ['name', 'value'])])
return ''.join(html)
def saveHTMLErrorPage(self, html):
"""Save the given HTML error page for later viewing by the developer.
Returns the filename used. Invoked by handleException().
"""
dir = self.setting('ErrorMessagesDir')
if not os.path.exists(dir):
os.makedirs(dir)
filename = os.path.join(dir, self.htmlErrorPageFilename())
try:
f = open(filename, 'w')
try:
f.write(html)
finally:
f.close()
except IOError:
sys.stderr.write('[%s] [error] CGI Wrapper: Cannot save error page (%s)\n'
% (asctime(localtime(time())), filename))
else:
return filename
def htmlErrorPageFilename(self):
"""Construct a filename for an HTML error page.
This filename does not include the 'ErrorMessagesDir' setting.
"""
# Note: Using the timestamp and a random number is a poor technique
# for filename uniqueness, but it is fast and good enough in practice.
return 'Error-%s-%s-%06d.html' % (os.path.split(self._scriptPathname)[1],
'-'.join(map(lambda x: '%02d' % x, localtime(self._scriptEndTime)[:6])),
randint(0, 999999))
def logExceptionToDisk(self, errorMsgFilename=None, excInfo=None):
"""Write exception info to the log file.
Writes a tuple containing (date-time, filename, pathname,
exception-name, exception-data, error report filename)
to the errors file (typically 'Errors.csv') in CSV format.
Invoked by handleException().
"""
if not excInfo:
excInfo = sys.exc_info()
err, msg = excInfo[:2]
if isinstance(err, basestring): # string exception
err, msg = '', str(msg or err)
else:
err, msg = err.__name__, str(msg)
logline = (asctime(localtime(self._scriptEndTime)),
os.path.split(self._scriptPathname)[1], self._scriptPathname,
err, msg, errorMsgFilename or '')
def fixElement(element):
element = str(element)
if ',' in element or '"' in element:
element = element.replace('"', '""')
element = '"%s"' % element
return element
logline = map(fixElement, logline)
filename = self.setting('ErrorLogFilename')
if os.path.exists(filename):
f = open(filename, 'a')
else:
f = open(filename, 'w')
f.write('time,filename,pathname,exception name,'
'exception data,error report filename\n')
f.write(','.join(logline) + '\n')
f.close()
def emailException(self, html, excInfo=None):
"""Email an exception."""
# Construct the message
if not excInfo:
excInfo = sys.exc_info()
headers = self.setting('ErrorEmailHeaders')
msg = []
for key in headers:
if key != 'From' and key != 'To':
msg.append('%s: %s\n' % (key, headers[key]))
msg.append('\n')
msg.append(html)
msg = ''.join(msg)
# Send the message
import smtplib
server = smtplib.SMTP(self.setting('ErrorEmailServer'))
server.set_debuglevel(0)
server.sendmail(headers['From'], headers['To'], msg)
server.quit()
## Serve ##
def serve(self, environ=os.environ):
"""Serve a request."""
# Record the time
if 'isMain' in globals():
self._serverStartTime = serverStartTime
else:
self._serverStartTime = time()
self._serverStartTimeStamp = asctime(localtime(self._serverStartTime))
# Set up environment
self._environ = environ
# Ensure that filenames and paths have been provided
self.requireEnvs(['SCRIPT_FILENAME', 'PATH_INFO'])
# Set up the namespace
self._headers = self.makeHeaders()
self._fields = self.makeFieldStorage()
self._scriptPathname = self.scriptPathname()
self._scriptName = os.path.split(self._scriptPathname)[1]
self._namespace = dict(
headers=self._headers, fields=self._fields,
environ=self._environ, wrapper=self)
info = self._namespace.copy()
# Set up sys.stdout to be captured as a string. This allows scripts
# to set CGI headers at any time, which we then print prior to
# printing the main output. This also allows us to skip on writing
# any of the script's output if there was an error.
#
# This technique was taken from Andrew M. Kuchling's Feb 1998
# WebTechniques article.
#
self._realStdout = sys.stdout
sys.stdout = StringIO()
# Change directories if needed
if self.setting('ChangeDir'):
origDir = os.getcwd()
os.chdir(os.path.split(self._scriptPathname)[0])
else:
origDir = None
# A little more setup
self._errorOccurred = False
self._scriptStartTime = time()
# Run the target script
try:
if self._servingScript:
execfile(self._scriptPathname, self._namespace)
for name in self.setting('ClassNames'):
if not name:
name = os.path.splitext(self._scriptName)[0]
if name in self._namespace:
# our hook for class-oriented scripts
print self._namespace[name](info).html()
break
else:
self._headers = {'Location':
os.path.split(self._environ['SCRIPT_NAME'])[0]
+ self._environ['PATH_INFO']}
# Note the end time of the script
self._scriptEndTime = time()
self._scriptDuration = self._scriptEndTime - self._scriptStartTime
except:
# Note the end time of the script
self._scriptEndTime = time()
self._scriptDuration = self._scriptEndTime - self._scriptStartTime
self._errorOccurred = True
# Not really an error, if it was sys.exit(0)
excInfo = sys.exc_info()
if excInfo[0] == SystemExit:
code = excInfo[1].code
if not code:
self._errorOccurred = False
# Clean up
if self._errorOccurred:
if origDir:
os.chdir(origDir)
origDir = None
# Handle exception
self.handleException(sys.exc_info())
self.deliver()
# Restore original directory
if origDir:
os.chdir(origDir)
# Note the duration of server processing (as late as we possibly can)
self._serverDuration = time() - self._serverStartTime
# Log it
if self.setting('LogScripts'):
self.writeScriptLog()
def deliver(self):
"""Deliver the HTML.
This is used for the output that came from the script being served,
or from our own error reporting.
"""
# Compile the headers & cookies
headers = StringIO()
for header, value in self._headers.items():
headers.write("%s: %s\n" % (header, value))
if 'cookies' in self._namespace:
headers.write(str(self._namespace['cookies']))
headers.write('\n')
# Get the string buffer values
headersOut = headers.getvalue()
stdoutOut = sys.stdout.getvalue()
# Compute size
self._responseSize = len(headersOut) + len(stdoutOut)
# Send to the real stdout
self._realStdout.write(headersOut)
self._realStdout.write(stdoutOut)
## Misc functions ##
def docType():
"""Return a standard HTML document type"""
return ('<!DOCTYPE HTML PUBLIC'
' "-//W3C//DTD HTML 4.01 Transitional//EN"'
' "http://www.w3.org/TR/html4/loose.dtd">')
def htTitle(name):
"""Return an HTML section title."""
return ('<h2 style="color:white;background-color:#993333;'
'font-size:12pt;padding:1pt;font-weight:bold;'
'font-family:Tahoma,Verdana,Arial,Helvetica,sans-serif"'
' align="center">%s</h2>\n' % name)
def htDictionary(d, addSpace=None):
"""Returns an HTML table where each row is a key-value pair."""
if not d:
return '\n'
html = ['<table border="0" cellpadding="2" cellspacing="2">']
for key in sorted(d):
value = d[key]
if addSpace is not None and key in addSpace:
target = addSpace[key]
value = (target + ' ').join(value.split(target))
html.append('<tr><td bgcolor="#BBBBBB">%s</td>'
'<td bgcolor="#EEEEEE">%s </td></tr>\n' % (key, value))
html.append('</table>')
return '\n'.join(html)
def htTable(listOfDicts, keys=None):
"""Return an HTML table for a list of dictionaries.
The listOfDicts parameter is expected to be a list of
dictionaries whose keys are always the same. This function
returns an HTML string with the contents of the table.
If keys is None, the headings are taken from the first row in
alphabetical order.
Returns an empty string if listOfDicts is none or empty.
Deficiencies: There's no way to influence the formatting or to
use column titles that are different from the keys.
"""
if not listOfDicts:
return ''
if keys is None:
keys = sorted(listOfDicts[0])
html = ['<table border="0" cellpadding="2" cellspacing="2">']
html.append('<tr>')
for key in keys:
html.append('<th bgcolor="#BBBBBB">%s</th>' % key)
html.append('</tr>')
for row in listOfDicts:
html.append('<tr>')
for key in keys:
html.append('<td bgcolor="#EEEEEE">%s</td>' % row[key])
html.append('</tr>')
html.append('</table>')
return '\n'.join(html)
def osIdTable():
"""Get all OS id information.
Returns a list of dictionaries containing id information such
as uid, gid, etc., all obtained from the os module.
Dictionary keys are 'name' and 'value'.
"""
funcs = ['getegid', 'geteuid', 'getgid', 'getpgrp',
'getpid', 'getppid', 'getuid']
table = []
for funcName in funcs:
if hasattr(os, funcName):
value = getattr(os, funcName)()
table.append(dict(name=funcName, value=value))
return table
def main():
stdout = sys.stdout
try:
wrapper = CGIWrapper()
wrapper.serve()
except:
# There is already a fancy exception handler in the CGIWrapper for
# uncaught exceptions from target scripts. However, we should also
# catch exceptions here that might come from the wrapper, including
# ones generated while it's handling exceptions.
sys.stderr.write('[%s] [error] CGI Wrapper: Error while executing'
' script (unknown)\n' % asctime(localtime()))
traceback.print_exc(file=sys.stderr)
if sys.exc_info()[0] != SystemExit:
output = traceback.format_exception(*sys.exc_info())
output = ''.join(output)
output = output.replace('&', '&').replace(
'<', '<').replace('>', '>')
stdout.write('''Content-Type: text/html
%s
<html>
<head><title>Error</title>
<body><h2>ERROR</h2>
<pre>%s</pre>
</body>
</html>
''' % (docType(), output))
if __name__ == '__main__':
isMain = True
main()