diff --git a/src/Connection.php b/src/Connection.php index 75228b67..8d68aa9b 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -20,8 +20,11 @@ public function handleClose() return; } - // http://chat.stackoverflow.com/transcript/message/7727858#7727858 - stream_socket_shutdown($this->stream, STREAM_SHUT_RDWR); + // Try to cleanly shut down socket and ignore any errors in case other + // side already closed. Shutting down may return to blocking mode on + // some legacy versions, so reset to non-blocking just in case before + // continuing to close the socket resource. + @stream_socket_shutdown($this->stream, STREAM_SHUT_RDWR); stream_set_blocking($this->stream, false); fclose($this->stream); } diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index d5999693..d571cf0b 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -81,6 +81,24 @@ public function gettingEncryptedStuffFromGoogleShouldWorkIfHostIsResolvedFirst() $this->assertRegExp('#^HTTP/1\.0#', $response); } + /** @test */ + public function gettingPlaintextStuffFromEncryptedGoogleShouldNotWork() + { + $loop = new StreamSelectLoop(); + $connector = new Connector($loop); + + $conn = Block\await($connector->connect('google.com:443'), $loop); + + $this->assertContains(':443', $conn->getRemoteAddress()); + $this->assertNotEquals('google.com:443', $conn->getRemoteAddress()); + + $conn->write("GET / HTTP/1.0\r\n\r\n"); + + $response = Block\await(BufferedSink::createPromise($conn), $loop, self::TIMEOUT); + + $this->assertNotRegExp('#^HTTP/1\.0#', $response); + } + /** @test */ public function testConnectingFailsIfDnsUsesInvalidResolver() {