summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAssaf Gordon <assafgordon@gmail.com>2016-05-05 04:45:31 (GMT)
committerAssaf Gordon <assafgordon@gmail.com>2016-05-05 04:49:48 (GMT)
commit94b1750b55e9035e67972889a59c336ebb32e138 (patch)
treeb358a0db25192dec8d797d985689822e9366292f
parentc343373ad96bcae25f5cc950ab80e4db1cd0c9e9 (diff)
downloadcgi-tools-94b1750b55e9035e67972889a59c336ebb32e138.zip
cgi-tools-94b1750b55e9035e67972889a59c336ebb32e138.tar.gz
cgi-tools-94b1750b55e9035e67972889a59c336ebb32e138.tar.bz2
http_responses: add 'app code' in error messages
Allow CGI scripts to set custom numeric application-code which will appear before the random request code, example: set_app_code(555) then, when an error occurs: request 555.0123456789: something went wrong The 555 identifies the application (i.e. the CGI script), the other digits are random number which will be shown to the user when an error occurs, but also written to the log (possibly with more detailed error information).
-rw-r--r--cgi_tools/__init__.py2
-rw-r--r--cgi_tools/http_responses.py19
-rwxr-xr-xdemos/cgi-bin/cputime-error.py3
-rwxr-xr-xdemos/cgi-bin/gnu-date-delta.py3
-rwxr-xr-xdemos/cgi-bin/import-test.py3
5 files changed, 22 insertions, 8 deletions
diff --git a/cgi_tools/__init__.py b/cgi_tools/__init__.py
index 2ceee6b..1debcc5 100644
--- a/cgi_tools/__init__.py
+++ b/cgi_tools/__init__.py
@@ -8,7 +8,7 @@ from .system import force_C_locale, set_resource_limits, \
run_cmd_list, check_run_cmd_list
from .http_responses import http_bad_request_error, http_server_error, \
- http_error, log
+ http_error, log, set_app_code
from .validators import valid_regex, valid_int, valid_float
diff --git a/cgi_tools/http_responses.py b/cgi_tools/http_responses.py
index 54f9365..57e332c 100644
--- a/cgi_tools/http_responses.py
+++ b/cgi_tools/http_responses.py
@@ -4,12 +4,21 @@ Copyright (C) 2016 Assaf Gordon (assafgordon@gmail.com)
License: BSD (See LICENSE file)
"""
from __future__ import print_function
-import sys, random
+import sys, random, os
-def generate_request_code():
- return 512 + random.uniform(0,1)
+req_code = ""
+
+def set_app_code(app_code):
+ global req_code
+ req_code = int(app_code) + random.uniform(0,1)
+
+
+# Get 4 random bytes, convert to unsigned 32-bit int, use as seed.
+s = reduce(lambda x,y: x*y, map(ord,os.urandom(4)))
+random.seed(s)
+# Set default application code + random request code
+set_app_code(512)
-req_code = generate_request_code()
def log(*args):
""" Write MSG to STDERR """
@@ -40,6 +49,8 @@ def http_error(http_code,http_status,msg):
print ("Status: %d %s" % (http_code, http_status))
print ("Content-Type: text/plain")
print ("")
+ prefix = "request %s: " % str(req_code)
+ msg = prefix + msg
print (msg)
sys.exit(1)
diff --git a/demos/cgi-bin/cputime-error.py b/demos/cgi-bin/cputime-error.py
index d3abafe..57ae4d8 100755
--- a/demos/cgi-bin/cputime-error.py
+++ b/demos/cgi-bin/cputime-error.py
@@ -7,7 +7,7 @@ License: BSD (See LICENSE file)
from __future__ import print_function
import sys, cgi, math
-from cgi_tools import force_C_locale, set_resource_limits
+from cgi_tools import force_C_locale, set_resource_limits, set_app_code
def waste_time():
"""use %100 of the CPU for a really long time"""
@@ -23,6 +23,7 @@ def cgi_main():
if __name__ == "__main__":
+ set_app_code(123)
# Allow 10 seconds of wall-time, and 1 second of cputime.
set_resource_limits(walltime=10,cputime=1)
force_C_locale()
diff --git a/demos/cgi-bin/gnu-date-delta.py b/demos/cgi-bin/gnu-date-delta.py
index 60563db..919968b 100755
--- a/demos/cgi-bin/gnu-date-delta.py
+++ b/demos/cgi-bin/gnu-date-delta.py
@@ -11,7 +11,7 @@ from jinja2 import Template
from subprocess import Popen, PIPE
from cgi_tools import force_C_locale, set_resource_limits, \
http_bad_request_error, http_server_error, \
- valid_regex, run_cmd_list
+ valid_regex, run_cmd_list, set_app_code
def valid_delta(v):
return valid_regex("[-a-zA-Z0-9: \.\+]+",v)
@@ -107,6 +107,7 @@ def cgi_main():
if __name__ == "__main__":
+ set_app_code(124)
set_resource_limits(walltime=4)
force_C_locale()
cgi_main()
diff --git a/demos/cgi-bin/import-test.py b/demos/cgi-bin/import-test.py
index 5b1937b..ec42bca 100755
--- a/demos/cgi-bin/import-test.py
+++ b/demos/cgi-bin/import-test.py
@@ -5,7 +5,7 @@ Copyright (C) 2016 Assaf Gordon (assafgordon@gmail.com)
License: BSD (See LICENSE file)
"""
-from cgi_tools import force_C_locale, set_resource_limits
+from cgi_tools import force_C_locale, set_resource_limits, set_app_code
def cgi_main():
print "Content-Type: text/plain"
@@ -14,6 +14,7 @@ def cgi_main():
if __name__ == "__main__":
+ set_app_code(122)
set_resource_limits(walltime=3)
force_C_locale()
cgi_main()