#!/usr/bin/python3

# Copyright 2019 Fiona Klute
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""HTTP handling components for mod_gnutls tests."""

import socket
import subprocess

from http.client import HTTPConnection
from threading import Thread

class HTTPSubprocessConnection(HTTPConnection):
    """An HTTPConnection that transports data through a subprocess instead
    of a socket. The mod_gnutls test suite uses it to transport data
    through gnutls-cli instead of the ssl module.

    """
    def __init__(self, command, host, port=None,
                 output_filter=None,
                 timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
                 blocksize=8192):
        super(HTTPSubprocessConnection, self).__init__(host, port, timeout,
                                                       source_address=None,
                                                       blocksize=blocksize)
        # "command" must be a list containing binary and command line
        # parameters
        self.command = command
        # This will be the subprocess reference when connected
        self._sproc = None
        # The subprocess return code is stored here on close()
        self.returncode = None
        # The set_tunnel method of the super class is not supported
        # (see exception doc)
        self.set_tunnel = None
        # This method will be run in a separate process and filter the
        # stdout of self._sproc. Its arguments are self._sproc.stdout
        # and the socket back to the HTTP connection (write-only).
        self._output_filter = output_filter
        # output filter thread
        self._fthread = None

    def connect(self):
        s_local, s_remote = socket.socketpair(socket.AF_UNIX,
                                              socket.SOCK_STREAM)
        s_local.settimeout(self.timeout)

        # TODO: Maybe capture stderr?
        if self._output_filter:
            self._sproc = subprocess.Popen(self.command, stdout=subprocess.PIPE,
                                           stdin=s_remote, close_fds=True,
                                           bufsize=0)
            self._fthread = Thread(target=self._output_filter,
                                   args=(self._sproc.stdout, s_remote))
            self._fthread.start()
        else:
            self._sproc = subprocess.Popen(self.command, stdout=s_remote,
                                           stdin=s_remote, close_fds=True,
                                           bufsize=0)
        self.sock = s_local

    def close(self):
        # close socket to subprocess for writing
        if self.sock:
            self.sock.shutdown(socket.SHUT_WR)

        # Wait for the process to stop, send SIGTERM/SIGKILL if
        # necessary
        if self._sproc:
            try:
                self.returncode = self._sproc.wait(self.timeout)
            except subprocess.TimeoutExpired:
                try:
                    self._sproc.terminate()
                    self.returncode = self._sproc.wait(self.timeout)
                except subprocess.TimeoutExpired:
                    self._sproc.kill()
                    self.returncode = self._sproc.wait(self.timeout)

        # filter thread receives HUP on pipe when the subprocess
        # terminates
        if self._fthread:
            self._fthread.join()

        # close the connection in the super class, which also calls
        # self.sock.close()
        super().close()

@@ -674,16 +674,27 @@
 bprocess
+%0Aimport sys
 %0A%0Afrom h
@@ -2158,16 +2158,166 @@
 d = None
+%0A        # Error stream handler thread. This is needed to synchronize%0A        # output between Python and the subprocess.%0A        self._ethread = None
 %0A%0A    de
@@ -2654,16 +2654,83 @@
 s.PIPE,%0A
+                                           stderr=subprocess.PIPE,%0A
         
@@ -3107,16 +3107,83 @@
 remote,%0A
+                                           stderr=subprocess.PIPE,%0A
         
@@ -3295,32 +3295,175 @@
      bufsize=0)%0A
+        self._ethread = Thread(target=_stderr_writer,%0A                               args=(self._sproc.stderr,))%0A        self._ethread.start()%0A
         self.soc
@@ -4288,16 +4288,75 @@
 d.join()
+%0A        if self._ethread:%0A            self._ethread.join()
 %0A%0A      
@@ -4437,32 +4437,32 @@
 lf.sock.close()%0A
-
         super().
@@ -4461,16 +4461,423 @@
 super().close()%0A
+%0A%0A%0Adef _stderr_writer(stream):%0A    %22%22%22Flush incoming data to sys.stderr.%0A%0A    This is a workaround to prevent output from gnutls-cli and the%0A    Python interpreter overwriting each other in the test%0A    logs. Forcing gnutls-cli stderr through Python ensures%0A    synchronization (via global interpreter lock).%0A    %22%22%22%0A    for line in stream:%0A        print(line.decode(), file=sys.stderr, end='', flush=True)%0A

Handle subprocess stderr via a thread