From f69c2569d65c5d9512d8fda0af4e58820faa4498 Mon Sep 17 00:00:00 2001 From: Aaron Tan Date: Fri, 17 Mar 2017 17:54:29 -0400 Subject: [PATCH] Smarter log aggregator host name generation. --- awx/main/utils/handlers.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/awx/main/utils/handlers.py b/awx/main/utils/handlers.py index b1b96b492f..2346ee5c30 100644 --- a/awx/main/utils/handlers.py +++ b/awx/main/utils/handlers.py @@ -6,6 +6,7 @@ import logging import json import requests import time +import re from concurrent.futures import ThreadPoolExecutor from copy import copy @@ -118,10 +119,18 @@ class BaseHTTPSHandler(logging.Handler): def get_http_host(self): host = self.host or '' - if not host.startswith('http'): - host = 'http://%s' % self.host - if self.port != 80 and self.port is not None: - host = '%s:%s' % (host, str(self.port)) + # Force using http(s) protocol + if re.match(r'https?://', host) is None: + if re.match(r'[a-zA-Z]+://', host) is None: + host = 'http://%s' % self.host + else: + host = re.sub(r'[a-zA-Z]+://', r'http://', host, count=1) + # Insert self.port if its special and port number not given in host + if (self.port != 80 and self.port is not None and + re.match(r'https?://[^:/]+:[0-9]+', host) is None): + host = re.sub(r'https?://[^/]+', + lambda m: '%s:%s' % (m.group(0), str(self.port), + host, count=1) return host def get_post_kwargs(self, payload_input):