Repository: mitmproxy/mitmproxy

Issue:
Crash while running smoke tests
```
Traceback (most recent call last):
  File "...mitmproxy/mitmproxy/net/tcp.py", line 211, in read
    data = self.o.read(rlen)
  File "...mitmproxy/venv3.5/lib/python3.5/site-packages/OpenSSL/SSL.py", line 1304, in recv
    self._raise_ssl_error(self._ssl, result)
  File "...mitmproxy/venv3.5/lib/python3.5/site-packages/OpenSSL/SSL.py", line 1166, in _raise_ssl_error
    raise SysCallError(errno, errorcode.get(errno))
OpenSSL.SSL.SysCallError: (54, 'ECONNRESET')

During handling of the above exception, another exception occurred:                                                                                   [59/220]

Traceback (most recent call last):
  File "...mitmproxy/mitmproxy/proxy/server.py", line 119, in handle
    root_layer()
  File "...mitmproxy/mitmproxy/proxy/modes/http_proxy.py", line 9, in __call__
    layer()
  File "...mitmproxy/mitmproxy/proxy/protocol/tls.py", line 383, in __call__
    layer()
  File "...mitmproxy/mitmproxy/proxy/protocol/http1.py", line 72, in __call__
    layer()
  File "...mitmproxy/mitmproxy/proxy/protocol/http.py", line 177, in __call__
    if not self._process_flow(flow):
  File "...mitmproxy/mitmproxy/proxy/protocol/http.py", line 246, in _process_flow
    return self.handle_regular_connect(f)
  File "...mitmproxy/mitmproxy/proxy/protocol/http.py", line 204, in handle_regular_connect
    layer()
  File "...mitmproxy/mitmproxy/proxy/protocol/tls.py", line 383, in __call__
    layer()
  File "...mitmproxy/mitmproxy/proxy/protocol/http1.py", line 72, in __call__
    layer()
  File "...mitmproxy/mitmproxy/proxy/protocol/http.py", line 177, in __call__
    if not self._process_flow(flow):
  File "...mitmproxy/mitmproxy/proxy/protocol/http.py", line 228, in _process_flow
    request = self.read_request_headers(f)
  File "...mitmproxy/mitmproxy/proxy/protocol/http1.py", line 14, in read_request_headers
    http1.read_request_head(self.client_conn.rfile)
  File "...mitmproxy/mitmproxy/net/http/http1/read.py", line 52, in read_request_head
    form, method, scheme, host, port, path, http_version = _read_request_line(rfile)
  File "...mitmproxy/mitmproxy/net/http/http1/read.py", line 240, in _read_request_line
    line = _get_first_line(rfile)
  File "...mitmproxy/mitmproxy/net/http/http1/read.py", line 227, in _get_first_line
    line = rfile.readline()
  File "...mitmproxy/mitmproxy/net/tcp.py", line 251, in readline
    ch = self.read(1)
  File "...mitmproxy/mitmproxy/net/tcp.py", line 233, in read
    raise exceptions.TlsException(str(e))
mitmproxy.exceptions.TlsException: (54, 'ECONNRESET')

mitmproxy has crashed!
Please lodge a bug report at: https://github.com/mitmproxy/mitmproxy
```


##### System information
Mitmproxy version: 0.19
Python version: 3.5.2
Platform: Darwin-16.1.0-x86_64-i386-64bit
SSL version: OpenSSL 1.0.2j  26 Sep 2016
Mac version: 10.12.1 ('', '', '') x86_64


Discussion:

Resolving patch:
diff --git a/mitmproxy/proxy/protocol/http.py b/mitmproxy/proxy/protocol/http.py
--- a/mitmproxy/proxy/protocol/http.py
+++ b/mitmproxy/proxy/protocol/http.py
@@ -182,6 +182,17 @@ def handle_regular_connect(self, f):
 
         try:
             self.set_server((f.request.host, f.request.port))
+
+            if f.response:
+                resp = f.response
+            else:
+                resp = http.make_connect_response(f.request.data.http_version)
+
+            self.send_response(resp)
+
+            if is_ok(resp.status_code):
+                layer = self.ctx.next_layer(self)
+                layer()
         except (
             exceptions.ProtocolException, exceptions.NetlibException
         ) as e:
@@ -192,17 +203,6 @@ def handle_regular_connect(self, f):
             self.channel.ask("error", f)
             return False
 
-        if f.response:
-            resp = f.response
-        else:
-            resp = http.make_connect_response(f.request.data.http_version)
-
-        self.send_response(resp)
-
-        if is_ok(resp.status_code):
-            layer = self.ctx.next_layer(self)
-            layer()
-
         return False
 
     def handle_upstream_connect(self, f):


Test patch: