Unify HTTP handling in git-pull-hook
authorJan Dittberner <jandd@cacert.org>
Sat, 14 Apr 2018 18:35:46 +0000 (20:35 +0200)
committerJan Dittberner <jandd@cacert.org>
Sat, 14 Apr 2018 18:35:55 +0000 (20:35 +0200)
sitemodules/profiles/files/puppet_server/git-pull-hook

index 78f75c3..32fd1c4 100755 (executable)
@@ -42,6 +42,11 @@ class GitHookRequestHandler(BaseHTTPRequestHandler):
     with a known authentication token in an "Authentication" HTTP header.
     """
 
     with a known authentication token in an "Authentication" HTTP header.
     """
 
+    def _send_data(self, message):
+        self.send_header("Content-Type", "text/plain; charset=utf8")
+        self.end_headers()
+        self.wfile.write(("%s\r\n" % message).encode('UTF-8'))
+
     def _handle_pull(self):
         try:
             git_proc = Popen(
     def _handle_pull(self):
         try:
             git_proc = Popen(
@@ -55,10 +60,9 @@ class GitHookRequestHandler(BaseHTTPRequestHandler):
         except Exception as e:
             self.log_error("Could not pull changes for %s: %s",
                            GIT_DIRECTORY, e)
         except Exception as e:
             self.log_error("Could not pull changes for %s: %s",
                            GIT_DIRECTORY, e)
+            self._send_data("Error updating the repository.")
         self.send_response(HTTPStatus.OK)
         self.send_response(HTTPStatus.OK)
-        self.send_header('Content-Type', 'text/plain; charset=utf8')
-        self.end_headers()
-        self.wfile.write(("updated %s" % GIT_DIRECTORY).encode('UTF-8'))
+        self._send_data("updated %s" % GIT_DIRECTORY)
 
     # noinspection PyPep8Naming
     def do_GET(self):
 
     # noinspection PyPep8Naming
     def do_GET(self):
@@ -72,16 +76,13 @@ class GitHookRequestHandler(BaseHTTPRequestHandler):
                 self._handle_pull()
             else:
                 self.send_response(HTTPStatus.UNAUTHORIZED)
                 self._handle_pull()
             else:
                 self.send_response(HTTPStatus.UNAUTHORIZED)
-                self.end_headers()
+                self._send_data('You have to send a valid token in the "Authentication" header.')
         elif self.path == '/health':
             self.send_response(HTTPStatus.OK)
         elif self.path == '/health':
             self.send_response(HTTPStatus.OK)
-            self.send_header('Content-Type', 'text/plain; charset=utf8')
-            self.end_headers()
-            self.wfile.write(b"I'm healthy!")
+            self._send_data("I'm healthy!")
         else:
             self.send_error(HTTPStatus.BAD_REQUEST)
         else:
             self.send_error(HTTPStatus.BAD_REQUEST)
-            self.end_headers()
-            self.wfile.write(b"You requested something I do not understand")
+            self._send_data("You requested something I do not understand.")
 
 
 def run(server_class=HTTPServer, handler_class=GitHookRequestHandler):
 
 
 def run(server_class=HTTPServer, handler_class=GitHookRequestHandler):