Module: check_mk
Branch: master
Commit: 04eecef96f58c050f08c310eba5c5536f8f3e7c6
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=04eecef96f58c0…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu Jul 12 09:10:23 2018 +0200
Moved HTTP status definitions to separate module
Change-Id: I67c30b61929d988d1a5be3c570e77de0e6fb173c
---
web/htdocs/gui_exceptions.py | 10 ++---
web/htdocs/http.py | 75 ++---------------------------------
web/htdocs/http_status.py | 93 ++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 102 insertions(+), 76 deletions(-)
diff --git a/web/htdocs/gui_exceptions.py b/web/htdocs/gui_exceptions.py
index d2c1d1a..716797f 100644
--- a/web/htdocs/gui_exceptions.py
+++ b/web/htdocs/gui_exceptions.py
@@ -25,7 +25,7 @@
# Boston, MA 02110-1301 USA.
-import http
+import http_status
from cmk.exceptions import MKGeneralException, MKException
@@ -41,17 +41,17 @@ class FinalizeRequest(Exception):
"""Is used to end the HTTP request processing from deeper code
levels"""
# TODO: Drop this default and make exit code explicit for all call sites
def __init__(self, code = None):
- super(FinalizeRequest, self).__init__(http.http_status(code))
- self.status = code or http.HTTP_OK
+ super(FinalizeRequest, self).__init__(http_status.status_with_reason(code))
+ self.status = code or http_status.HTTP_OK
class HTTPRedirect(FinalizeRequest):
"""Is used to end the HTTP request processing from deeper code levels
and making the client request another page after receiving the
response."""
- def __init__(self, url, code=http.HTTP_MOVED_TEMPORARILY):
+ def __init__(self, url, code=http_status.HTTP_MOVED_TEMPORARILY):
super(HTTPRedirect, self).__init__(code)
- if code not in [ http.HTTP_MOVED_PERMANENTLY, http.HTTP_MOVED_TEMPORARILY ]:
+ if code not in [ http_status.HTTP_MOVED_PERMANENTLY,
http_status.HTTP_MOVED_TEMPORARILY ]:
raise Exception("Invalid status code: %d" % code)
self.url = url
diff --git a/web/htdocs/http.py b/web/htdocs/http.py
index 5e7387c..7fd4d9e 100644
--- a/web/htdocs/http.py
+++ b/web/htdocs/http.py
@@ -31,74 +31,8 @@ import time
import Cookie
import cgi
import log
-
-# TODO: Use definitions from httplib (Python 3: http.HTTPStatus)
-HTTP_CONTINUE = 100
-HTTP_SWITCHING_PROTOCOLS = 101
-HTTP_PROCESSING = 102
-HTTP_OK = 200
-HTTP_CREATED = 201
-HTTP_ACCEPTED = 202
-HTTP_NON_AUTHORITATIVE = 203
-HTTP_NO_CONTENT = 204
-HTTP_RESET_CONTENT = 205
-HTTP_PARTIAL_CONTENT = 206
-HTTP_MULTI_STATUS = 207
-HTTP_MULTIPLE_CHOICES = 300
-HTTP_MOVED_PERMANENTLY = 301
-HTTP_MOVED_TEMPORARILY = 302
-HTTP_SEE_OTHER = 303
-HTTP_NOT_MODIFIED = 304
-HTTP_USE_PROXY = 305
-HTTP_TEMPORARY_REDIRECT = 307
-HTTP_BAD_REQUEST = 400
-HTTP_UNAUTHORIZED = 401
-HTTP_PAYMENT_REQUIRED = 402
-HTTP_FORBIDDEN = 403
-HTTP_NOT_FOUND = 404
-HTTP_METHOD_NOT_ALLOWED = 405
-HTTP_NOT_ACCEPTABLE = 406
-HTTP_PROXY_AUTHENTICATION_REQUIRED= 407
-HTTP_REQUEST_TIME_OUT = 408
-HTTP_CONFLICT = 409
-HTTP_GONE = 410
-HTTP_LENGTH_REQUIRED = 411
-HTTP_PRECONDITION_FAILED = 412
-HTTP_REQUEST_ENTITY_TOO_LARGE = 413
-HTTP_REQUEST_URI_TOO_LARGE = 414
-HTTP_UNSUPPORTED_MEDIA_TYPE = 415
-HTTP_RANGE_NOT_SATISFIABLE = 416
-HTTP_EXPECTATION_FAILED = 417
-HTTP_UNPROCESSABLE_ENTITY = 422
-HTTP_LOCKED = 423
-HTTP_FAILED_DEPENDENCY = 424
-HTTP_UPGRADE_REQUIRED = 426
-HTTP_INTERNAL_SERVER_ERROR = 500
-HTTP_NOT_IMPLEMENTED = 501
-HTTP_BAD_GATEWAY = 502
-HTTP_SERVICE_UNAVAILABLE = 503
-HTTP_GATEWAY_TIME_OUT = 504
-HTTP_VERSION_NOT_SUPPORTED = 505
-HTTP_VARIANT_ALSO_VARIES = 506
-HTTP_INSUFFICIENT_STORAGE = 507
-HTTP_NOT_EXTENDED = 510
-
-def http_status(code):
- if code == HTTP_OK:
- return '200 OK'
- elif code == HTTP_MOVED_TEMPORARILY:
- return '301 Moved Permanently'
- elif code == HTTP_MOVED_PERMANENTLY:
- return '302 Found'
- elif code == HTTP_NOT_MODIFIED:
- return '304 Not Modified'
- elif code == HTTP_INTERNAL_SERVER_ERROR:
- return '500 Internal Server Error'
- elif code == HTTP_NOT_FOUND:
- return '404 Not Found'
- else:
- return str(code)
-
+import http_status
+from gui_exceptions import HTTPRedirect
class Request(object):
"""Provides information about the users HTTP request to the
application
@@ -354,7 +288,7 @@ class Response(object):
self._request = request
- self._status_code = HTTP_OK
+ self._status_code = http_status.HTTP_OK
self._output = []
self._headers_out = []
@@ -413,7 +347,6 @@ class Response(object):
def http_redirect(self, url):
"""Finalize the currently processed page with a HTTP redirect to
the given URL"""
- from gui_exceptions import HTTPRedirect
raise HTTPRedirect(url)
@@ -425,7 +358,7 @@ class Response(object):
@property
def http_status(self):
"""Provides the HTTP response status header (code incl.
text)"""
- return http_status(self._status_code)
+ return http_status.status_with_reason(self._status_code)
@property
diff --git a/web/htdocs/http_status.py b/web/htdocs/http_status.py
new file mode 100644
index 0000000..4a5dfac
--- /dev/null
+++ b/web/htdocs/http_status.py
@@ -0,0 +1,93 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8; py-indent-offset: 4 -*-
+# +------------------------------------------------------------------+
+# | ____ _ _ __ __ _ __ |
+# | / ___| |__ ___ ___| | __ | \/ | |/ / |
+# | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
+# | | |___| | | | __/ (__| < | | | | . \ |
+# | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
+# | |
+# | Copyright Mathias Kettner 2014 mk(a)mathias-kettner.de |
+# +------------------------------------------------------------------+
+#
+# This file is part of Check_MK.
+# The official homepage is at
http://mathias-kettner.de/check_mk.
+#
+# check_mk is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation in version 2. check_mk is distributed
+# in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
+# out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE. See the GNU General Public License for more de-
+# tails. You should have received a copy of the GNU General Public
+# License along with GNU Make; see the file COPYING. If not, write
+# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+# Boston, MA 02110-1301 USA.
+
+# TODO: Use definitions from httplib (Python 3: http.HTTPStatus)
+HTTP_CONTINUE = 100
+HTTP_SWITCHING_PROTOCOLS = 101
+HTTP_PROCESSING = 102
+HTTP_OK = 200
+HTTP_CREATED = 201
+HTTP_ACCEPTED = 202
+HTTP_NON_AUTHORITATIVE = 203
+HTTP_NO_CONTENT = 204
+HTTP_RESET_CONTENT = 205
+HTTP_PARTIAL_CONTENT = 206
+HTTP_MULTI_STATUS = 207
+HTTP_MULTIPLE_CHOICES = 300
+HTTP_MOVED_PERMANENTLY = 301
+HTTP_MOVED_TEMPORARILY = 302
+HTTP_SEE_OTHER = 303
+HTTP_NOT_MODIFIED = 304
+HTTP_USE_PROXY = 305
+HTTP_TEMPORARY_REDIRECT = 307
+HTTP_BAD_REQUEST = 400
+HTTP_UNAUTHORIZED = 401
+HTTP_PAYMENT_REQUIRED = 402
+HTTP_FORBIDDEN = 403
+HTTP_NOT_FOUND = 404
+HTTP_METHOD_NOT_ALLOWED = 405
+HTTP_NOT_ACCEPTABLE = 406
+HTTP_PROXY_AUTHENTICATION_REQUIRED= 407
+HTTP_REQUEST_TIME_OUT = 408
+HTTP_CONFLICT = 409
+HTTP_GONE = 410
+HTTP_LENGTH_REQUIRED = 411
+HTTP_PRECONDITION_FAILED = 412
+HTTP_REQUEST_ENTITY_TOO_LARGE = 413
+HTTP_REQUEST_URI_TOO_LARGE = 414
+HTTP_UNSUPPORTED_MEDIA_TYPE = 415
+HTTP_RANGE_NOT_SATISFIABLE = 416
+HTTP_EXPECTATION_FAILED = 417
+HTTP_UNPROCESSABLE_ENTITY = 422
+HTTP_LOCKED = 423
+HTTP_FAILED_DEPENDENCY = 424
+HTTP_UPGRADE_REQUIRED = 426
+HTTP_INTERNAL_SERVER_ERROR = 500
+HTTP_NOT_IMPLEMENTED = 501
+HTTP_BAD_GATEWAY = 502
+HTTP_SERVICE_UNAVAILABLE = 503
+HTTP_GATEWAY_TIME_OUT = 504
+HTTP_VERSION_NOT_SUPPORTED = 505
+HTTP_VARIANT_ALSO_VARIES = 506
+HTTP_INSUFFICIENT_STORAGE = 507
+HTTP_NOT_EXTENDED = 510
+
+
+def status_with_reason(code):
+ if code == HTTP_OK:
+ return '200 OK'
+ elif code == HTTP_MOVED_TEMPORARILY:
+ return '301 Moved Permanently'
+ elif code == HTTP_MOVED_PERMANENTLY:
+ return '302 Found'
+ elif code == HTTP_NOT_MODIFIED:
+ return '304 Not Modified'
+ elif code == HTTP_INTERNAL_SERVER_ERROR:
+ return '500 Internal Server Error'
+ elif code == HTTP_NOT_FOUND:
+ return '404 Not Found'
+ else:
+ return str(code)