From: Richard Frith-Macdonald <rfm@gnu.org>
Subject: Fix FTBFS on Debian kfreedsd-* architectures

* Source/GSSocketStream.m: Fix for failure of connect() call.
* Tests/NSURLConnection/test01.m: Add testcases, fix reset of delegate.

Origin: upstream, http://svn.gna.org/viewcvs/gnustep?view=revision&revision=39860, http://svn.gna.org/viewcvs/gnustep?view=revision&revision=39861, http://svn.gna.org/viewcvs/gnustep?view=revision&revision=39862
Last-Update: 2016-06-14


--- a/Source/GSSocketStream.m
+++ b/Source/GSSocketStream.m
@@ -1714,6 +1714,11 @@
   // could be opened because of sibling
   if ([self _isOpened])
     return;
+  if (_sibling && [_sibling streamStatus] == NSStreamStatusError)
+    {
+      [self _setStatus: NSStreamStatusError];
+      return;
+    }
   if (_passive || (_sibling && [_sibling _isOpened]))
     goto open_ok;
   // check sibling status, avoid double connect
@@ -1747,27 +1752,30 @@
             }
         }
 
-      if (_handler == nil)
+      if (nil == _handler)
         {
           [GSTLSHandler tryInput: self output: _sibling];
         }
+
       result = connect([self _sock], &_address.s,
         GSPrivateSockaddrLength(&_address.s));
       if (socketError(result))
         {
-          if (!socketWouldBlock())
+          if (socketWouldBlock())
+            {
+              /* Need to set the status first, so that the run loop can tell
+               * it needs to add the stream as waiting on writable, as an
+               * indication of opened
+               */
+              [self _setStatus: NSStreamStatusOpening];
+            }
+          else
             {
+              /* Had an immediate connect error.
+               */
               [self _recordError];
-              [self _setHandler: nil];
-              [_sibling _setHandler: nil];
-              return;
+              [_sibling _recordError];
             }
-          /*
-           * Need to set the status first, so that the run loop can tell
-           * it needs to add the stream as waiting on writable, as an
-           * indication of opened
-           */
-          [self _setStatus: NSStreamStatusOpening];
 #if	defined(_WIN32)
           WSAEventSelect(_sock, _loopID, FD_ALL_EVENTS);
 #endif
@@ -1776,7 +1784,7 @@
 	      [self _schedule];
 	      return;
 	    }
-          else
+          else if (NSStreamStatusOpening == _currentStatus)
             {
               NSRunLoop *r;
               NSDate    *d;
@@ -1965,6 +1973,10 @@
 	@"Received event for closed stream");
       [_sibling _dispatch];
     }
+  else if ([self streamStatus] == NSStreamStatusError)
+    {
+      [self _sendEvent: NSStreamEventErrorOccurred];
+    }
   else
     {
       WSANETWORKEVENTS events;
@@ -2092,6 +2104,10 @@
     {
       myEvent = NSStreamEventEndEncountered;
     }
+  else if ([self streamStatus] == NSStreamStatusError)
+    {
+      myEvent = NSStreamEventErrorOccurred;
+    }
   else
     {
       [self _setStatus: NSStreamStatusOpen];
@@ -2185,6 +2201,11 @@
   // could be opened because of sibling
   if ([self _isOpened])
     return;
+  if (_sibling && [_sibling streamStatus] == NSStreamStatusError)
+    {
+      [self _setStatus: NSStreamStatusError];
+      return;
+    }
   if (_passive || (_sibling && [_sibling _isOpened]))
     goto open_ok;
   // check sibling status, avoid double connect
@@ -2218,7 +2239,7 @@
             }
         }
 
-      if (_handler == nil)
+      if (nil == _handler)
         {
           [GSTLSHandler tryInput: _sibling output: self];
         }
@@ -2227,19 +2248,22 @@
         GSPrivateSockaddrLength(&_address.s));
       if (socketError(result))
         {
-          if (!socketWouldBlock())
+          if (socketWouldBlock())
             {
+              /*
+               * Need to set the status first, so that the run loop can tell
+               * it needs to add the stream as waiting on writable, as an
+               * indication of opened
+               */
+              [self _setStatus: NSStreamStatusOpening];
+            }
+          else
+            {
+              /* Had an immediate connect error.
+               */
               [self _recordError];
-              [self _setHandler: nil];
-              [_sibling _setHandler: nil];
-              return;
+              [_sibling _recordError];
             }
-          /*
-           * Need to set the status first, so that the run loop can tell
-           * it needs to add the stream as waiting on writable, as an
-           * indication of opened
-           */
-          [self _setStatus: NSStreamStatusOpening];
 #if	defined(_WIN32)
           WSAEventSelect(_sock, _loopID, FD_ALL_EVENTS);
 #endif
@@ -2248,7 +2272,7 @@
 	      [self _schedule];
 	      return;
 	    }
-          else
+          else if (NSStreamStatusOpening == _currentStatus)
             {
               NSRunLoop *r;
               NSDate    *d;
@@ -2278,7 +2302,6 @@
   WSAEventSelect(_sock, _loopID, FD_ALL_EVENTS);
 #endif
   [super open];
-
 }
 
 
@@ -2387,6 +2410,10 @@
 	@"Received event for closed stream");
       [_sibling _dispatch];
     }
+  else if ([self streamStatus] == NSStreamStatusError)
+    {
+      [self _sendEvent: NSStreamEventErrorOccurred];
+    }
   else
     {
       WSANETWORKEVENTS events;
@@ -2512,6 +2539,10 @@
     {
       myEvent = NSStreamEventEndEncountered;
     }
+  else if ([self streamStatus] == NSStreamStatusError)
+    {
+      myEvent = NSStreamEventErrorOccurred;
+    }
   else
     {
       [self _setStatus: NSStreamStatusOpen];
