summaryrefslogtreecommitdiffstats
path: root/gitautodeploy/httpserver.py
diff options
context:
space:
mode:
authorOliver Poignant <oliver@poignant.se>2016-05-05 00:52:19 +0200
committerOliver Poignant <oliver@poignant.se>2016-05-05 00:52:19 +0200
commit70f8d7d01873e8d66b9a3ce7497f2f857e573984 (patch)
treeca44cde75ebd3701a48e5986853fb62c89c7040c /gitautodeploy/httpserver.py
parent7c2e8c6c6300dee3618c6ea39150bb1f4d2ecefb (diff)
downloadGit-Auto-Deploy-70f8d7d01873e8d66b9a3ce7497f2f857e573984.zip
Git-Auto-Deploy-70f8d7d01873e8d66b9a3ce7497f2f857e573984.tar.gz
Git-Auto-Deploy-70f8d7d01873e8d66b9a3ce7497f2f857e573984.tar.bz2
Handle invalid web hook requests better. Fixed issue with shutdown.
Diffstat (limited to 'gitautodeploy/httpserver.py')
-rw-r--r--gitautodeploy/httpserver.py20
1 files changed, 17 insertions, 3 deletions
diff --git a/gitautodeploy/httpserver.py b/gitautodeploy/httpserver.py
index 19ac418..813c4f6 100644
--- a/gitautodeploy/httpserver.py
+++ b/gitautodeploy/httpserver.py
@@ -16,17 +16,26 @@ class WebhookRequestHandler(BaseHTTPRequestHandler):
import logging
logger = logging.getLogger()
+ logger.info('Incoming request from %s:%s' % (self.client_address[0], self.client_address[1]))
content_type = self.headers.getheader('content-type')
content_length = int(self.headers.getheader('content-length'))
request_body = self.rfile.read(content_length)
-
+
# Extract request headers and make all keys to lowercase (makes them easier to compare)
request_headers = dict(self.headers)
request_headers = dict((k.lower(), v) for k, v in request_headers.iteritems())
- ServiceRequestParser = self.figure_out_service_from_request(request_headers, request_body)
-
+ try:
+ ServiceRequestParser = self.figure_out_service_from_request(request_headers, request_body)
+
+ except ValueError, e:
+ self.send_response(400)
+ self.send_header('Content-type', 'text/plain')
+ self.end_headers()
+ logger.warning('Unable to process incoming request from %s:%s' % (self.client_address[0], self.client_address[1]))
+ return
+
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
@@ -36,6 +45,8 @@ class WebhookRequestHandler(BaseHTTPRequestHandler):
logger.error('Unable to find appropriate handler for request. The source service is not supported.')
return
+ logger.info('Using %s to handle the request.' % ServiceRequestParser.__name__)
+
# Could be GitHubParser, GitLabParser or other
repo_configs, ref, action, repo_urls = ServiceRequestParser(self._config).get_repo_params_from_request(request_headers, request_body)
@@ -74,6 +85,9 @@ class WebhookRequestHandler(BaseHTTPRequestHandler):
logger = logging.getLogger()
data = json.loads(request_body)
+ if not isinstance(data, dict):
+ raise ValueError("Invalid JSON object")
+
user_agent = 'user-agent' in request_headers and request_headers['user-agent']
content_type = 'content-type' in request_headers and request_headers['content-type']