improve http response codes

avoid confusion with nginx errors and honor actual definitions of 4xx codes
This commit is contained in:
2019-10-10 11:46:32 +02:00
parent 09df250018
commit 3498668763

View File

@@ -13,6 +13,11 @@ SEPERATOR = ","
COMMENT_INDICATOR = "#" COMMENT_INDICATOR = "#"
config = {} config = {}
HTTP_FORBIDDEN = 401
HTTP_NOT_FOUND = 404
HTTP_UNPROCESSABLE = 422
HTTP_INTERNAL_ERR = 500
##### FRONTEND PATHS ######## ##### FRONTEND PATHS ########
@app.route('/', methods=["GET","POST"]) @app.route('/', methods=["GET","POST"])
def rootPage(): def rootPage():
@@ -23,7 +28,7 @@ def rootPage():
if data == None: if data == None:
retString = "POST-request is missing payload." retString = "POST-request is missing payload."
print(retString, file=sys.stderr) print(retString, file=sys.stderr)
return (retString, 400) return (retString, HTTP_UNPROCESSABLE)
print(json.dumps(flask.request.json, indent=4, sort_keys=True)) print(json.dumps(flask.request.json, indent=4, sort_keys=True))
@@ -34,13 +39,13 @@ def rootPage():
except KeyError: except KeyError:
retString = "Rejected: missing project/{} json path".format(PROJECT_IDENTIFIER) retString = "Rejected: missing project/{} json path".format(PROJECT_IDENTIFIER)
print(retString, file=sys.stderr) print(retString, file=sys.stderr)
return (retString, 400) return (retString, HTTP_UNPROCESSABLE)
# check for project in config # # check for project in config #
if not project or project not in config: if not project or project not in config:
retString = "Rejected: project not identified in config" retString = "Rejected: project not identified in config"
print(retString, file=sys.stderr) print(retString, file=sys.stderr)
return (retString, 401) return (retString, HTTP_NOT_FOUND)
token, scriptName = config[project] token, scriptName = config[project]
@@ -48,11 +53,11 @@ def rootPage():
if TOKEN_HEADER not in flask.request.headers: if TOKEN_HEADER not in flask.request.headers:
retString = "Rejected: secret token not found in request" retString = "Rejected: secret token not found in request"
print(retString, file=sys.stderr) print(retString, file=sys.stderr)
return (retString, 402) return (retString, HTTP_FORBIDDEN)
elif token != flask.request.headers[TOKEN_HEADER]: elif token != flask.request.headers[TOKEN_HEADER]:
retString = "Rejected: secret token found but is mismatch" retString = "Rejected: secret token found but is mismatch"
print(retString, file=sys.stderr) print(retString, file=sys.stderr)
return (retString, 403) return (retString, HTTP_FORBIDDEN)
# try to execute script # # try to execute script #
try: try:
@@ -60,7 +65,7 @@ def rootPage():
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
retString = "Failed: script execution on the server failed" retString = "Failed: script execution on the server failed"
print(retString, file=sys.stderr) print(retString, file=sys.stderr)
return (retString, 501) return (retString, HTTP_INTERNAL_ERR)
# signal successfull completion # # signal successfull completion #
return ("Success", 200) return ("Success", 200)