def router(path="/"): updated_register = healthcheck(register) host_header = request.headers["Host"] for entry in config["hosts"]: if host_header == entry["host"]: healthy_server = get_healthy_server(entry["host"], updated_register) if not healthy_server: return "No Backends servers available", 503 headers = process_rules(config, host_header, {k: v for k, v in request.headers.items()}, "header") params = process_rules(config, host_header, {k: v for k, v in request.args.items()}, "param") rewrite_path = "" if path == "v1": rewrite_path = process_rewrite_rules(config, host_header, path) response = requests.get("http://{}{}".format( healthy_server.endpoint, "/" + rewrite_path), headers=headers, params=params) return response.content, response.status_code for entry in config["paths"]: if ("/" + path) == entry["path"]: healthy_server = get_healthy_server(entry["path"], register) if not healthy_server: return "No Backends servers available", 503 response = requests.get("http://{}".format( healthy_server.endpoint)) return response.content, response.status_code return "Not Found", 404
def test_get_healthy_server(): host = "www.apple.com" healthy_server = Server("localhost:8081") unhealthy_server = Server("localhost:8082") unhealthy_server.healthy = False register = {"www.mango.com": [healthy_server, unhealthy_server], "www.apple.com": [healthy_server, healthy_server], "www.orange.com": [unhealthy_server, unhealthy_server], "/mango": [healthy_server, unhealthy_server], "/apple": [unhealthy_server, unhealthy_server]} assert get_healthy_server("www.mango.com", register) == healthy_server assert get_healthy_server("www.apple.com", register) == healthy_server assert get_healthy_server("www.orange.com", register) == None assert get_healthy_server("/mango", register) == healthy_server assert get_healthy_server("/apple", register) == None
def test_get_healthy_server(): healthy_server = Server('localhost:8081') unhealthy_server = Server('localhost:8082') unhealthy_server.healthy = False register = { 'www.mango.com': [healthy_server, unhealthy_server], 'www.apple.com': [healthy_server, healthy_server], 'www.orange.com': [unhealthy_server, unhealthy_server], '/mango': [healthy_server, unhealthy_server], '/apple': [unhealthy_server, unhealthy_server] } assert get_healthy_server('www.mango.com', register) == healthy_server assert get_healthy_server('www.apple.com', register) == healthy_server assert get_healthy_server('www.orange.com', register) is None assert get_healthy_server('/mango', register) == healthy_server assert get_healthy_server('/apple', register) is None
def test_get_healthy_server(): healthy_server = Server("localhost:8081") unhealthy_server = Server("localhost:8082") unhealthy_server.healthy = False register = { "www.anthrax.com": [healthy_server, unhealthy_server], "www.metallica.com": [healthy_server, healthy_server], "www.slayer.com": [unhealthy_server, unhealthy_server], "/anthrax": [healthy_server, unhealthy_server], "/metallica": [unhealthy_server, unhealthy_server], } assert get_healthy_server("www.anthrax.com", register) == healthy_server assert get_healthy_server("www.metallica.com", register) == healthy_server assert get_healthy_server("www.slayer.com", register) is None assert get_healthy_server("/anthrax", register) == healthy_server assert get_healthy_server("/metallica", register) is None
def router(path='/'): updated_register = healthcheck(register) host_header = request.headers['Host'] if not process_firewall_rules_flag( config, host_header, request.environ['REMOTE_ADDR'], f'/{path}'): return 'Forbidden', 403 for entry in config['hosts']: if host_header == entry['host']: healthy_server = get_healthy_server(entry['host'], updated_register) if not healthy_server: return 'No backend servers available.', 503 headers = process_rules(config, host_header, {k: v for k, v in request.headers.items()}, 'header') params = process_rules(config, host_header, {k: v for k, v in request.args.items()}, 'param') rewrite_path = '' if path == 'v1': rewrite_path = process_rewrite_rules(config, host_header, path) response = requests.get( f'http://{healthy_server.endpoint}/{rewrite_path}', headers=headers, params=params) return response.content, response.status_code for entry in config['paths']: if ('/' + path) == entry['path']: healthy_server = get_healthy_server(entry['path'], register) if not healthy_server: return 'No backend servers available.', 503 healthy_server.open_connections += 1 response = requests.get(f'http://{healthy_server.endpoint}') healthy_server.open_connections -= 1 return response.content, response.status_code return 'Not Found', 404
def router(path="/"): updated_register = healthcheck(register) host_header = request.headers["Host"] header_dictionary = {k: v for k, v in request.headers.items()} if not process_firewall_rules_flag( config, host_header, request.environ["REMOTE_ADDR"], f"/{path}", header_dictionary, ): return "Forbidden", 403 for entry in config["hosts"]: try: algo = entry["algo"] except KeyError: pass if algo == "weight": try: weights = [weights for weights in entry["weights"]] except KeyError: weights = None else: weights = None if host_header == entry["host"]: healthy_server = get_healthy_server(entry["host"], updated_register, algo, weights) if not healthy_server: return "No backend servers available.", 503 headers = process_rules( config, host_header, {k: v for k, v in request.headers.items()}, "header", ) params = process_rules(config, host_header, {k: v for k, v in request.args.items()}, "param") post_data = process_rules(config, host_header, {k: v for k, v in request.data}, "post_data") cookies = process_rules(config, host_header, {k: v for k, v in request.cookies}, "cookie") rewrite_path = "" if path == "v1": rewrite_path = process_rewrite_rules(config, host_header, path) response = requests.get( f"http://{healthy_server.endpoint}/{rewrite_path}", headers=headers, params=params, data=post_data, cookies=cookies, ) return response.content, response.status_code for entry in config["paths"]: if ("/" + path) == entry["path"]: healthy_server = get_healthy_server(entry["path"], register) if not healthy_server: return "No backend servers available", 503 healthy_server.open_connections += 1 response = requests.get(f"http://{healthy_server.endpoint}") healthy_server.open_connections -= 1 return response.content, response.status_code return "Not Found", 404