diff --git a/main.py b/main.py index 9be1a43..79715ca 100644 --- a/main.py +++ b/main.py @@ -71,8 +71,35 @@ def doConn(args, cfrm = None, cto = None): content += str(e) return (HTTP500, HTML, content) - return(HTTP200, JSON if json else XML, content) + return (HTTP200, JSON if json else XML, content) +def doVal(args, cfrm): + try: + try: + name = (cfrm or args["name"][0]).encode("latin-1").decode("utf-8") + except UnicodeDecodeError: + name = cfrm or args["name"][0] + + if not name: + raise ValueError() + + except: + content = "

400 Bad Request

\n" + content += "A \"name\" value is required for this type of request." + return HTTP400, HTML, content + + json = "json" in args + + try: + content = workers.val.worker(name, json) + + except Exception as e: + content = "

500 Internal Server Error

\n" + if "debug" in args: + content += str(e).encode() + return HTTP500, HTML, content + + return HTTP200, JSON if json else XML, content def application(env, re): if env["REQUEST_METHOD"] == "POST": @@ -126,33 +153,9 @@ def application(env, re): return elif rtype.lower() in ["val", "validate"]: - try: - try: - name = (cfrm or args["name"][0]).encode("latin-1").decode("utf-8") - except UnicodeDecodeError: - name = cfrm or args["name"][0] - - if not name: - raise ValueError() - - except: - re("400 Bad Request", []) - yield "

400 Bad Request

".encode() - yield "A \"name\" value is required for this type of request.".encode() - return - - try: - output = workers.val.worker(name, json) - - except Exception as e: - re("500 Internal Server Error", []) - yield "

500 Internal Server Error

".encode() - if "debug" in args: - yield str(e).encode() - return - - re("200 OK", [("Content-Type", "application/json" if json else "text/xml")]) - yield output.encode() + status, ctype, content = doVal(args, cfrm) + re(status, [("Content-Type", ctype)]) + yield content.encode() return elif rtype.lower() in ["closest", "close", "near", "nearby"]: