stream_set_timeout example



    public function stream_eof(): bool
    {
        return feof($this->handle);
    }

    public function stream_set_option(int $option, int $arg1, int $arg2): bool
    {
        return match ($option) {
            \STREAM_OPTION_BLOCKING => stream_set_blocking($this->handle, $arg1),
            \STREAM_OPTION_READ_TIMEOUT => stream_set_timeout($this->handle, $arg1$arg2),
            \STREAM_OPTION_WRITE_BUFFER => 0 === stream_set_write_buffer($this->handle, $arg2),
            default => false,
        };
    }

    public function stream_stat(): array|false
    {
        if (!$stat = stat($this->path)) {
            return false;
        }

        
$options['hooks']->dispatch('fsockopen.after_request', [&$fake_headers]);
            return '';
        }

        $timeout_sec = (int) floor($options['timeout']);
        if ($timeout_sec === $options['timeout']) {
            $timeout_msec = 0;
        } else {
            $timeout_msec = self::SECOND_IN_MICROSECONDS * $options['timeout'] % self::SECOND_IN_MICROSECONDS;
        }

        stream_set_timeout($socket$timeout_sec$timeout_msec);

        $response   = '';
        $body       = '';
        $headers    = '';
        $this->info = stream_get_meta_data($socket);
        $size       = 0;
        $doingbody  = false;
        $download   = false;
        if ($options['filename']) {
            // phpcs:ignore WordPress.PHP.NoSilencedErrors -- Silenced the PHP native warning in favour of throwing an exception.             $download = @fopen($options['filename'], 'wb');
            
return new WP_Error( 'http_request_failed', $connection_error . ': ' . $connection_error_str );
        }

        // Verify that the SSL certificate is valid for this request.         if ( $secure_transport && $ssl_verify && ! $proxy->is_enabled() ) {
            if ( ! self::verify_ssl_certificate( $handle$parsed_url['host'] ) ) {
                return new WP_Error( 'http_request_failed', __( 'The SSL certificate for the host could not be verified.' ) );
            }
        }

        stream_set_timeout( $handle$timeout$utimeout );

        if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) { // Some proxies require full URL in this field.             $request_path = $url;
        } else {
            $request_path = $parsed_url['path'] . ( isset( $parsed_url['query'] ) ? '?' . $parsed_url['query'] : '' );
        }

        $headers = strtoupper( $parsed_args['method'] ) . ' ' . $request_path . ' HTTP/' . $parsed_args['httpversion'] . "\r\n";

        $include_port_in_host_header = (
            ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) )
            
$timeout = $this->getTimeout();
        set_error_handler(function D$type$msg) {
            throw new TransportException(sprintf('Connection could not be established with host "%s": ', $this->url).$msg);
        });
        try {
            $this->stream = stream_socket_client($this->url, $errno$errstr$timeout, \STREAM_CLIENT_CONNECT, $streamContext);
        } finally {
            restore_error_handler();
        }

        stream_set_blocking($this->stream, true);
        stream_set_timeout($this->stream, $timeout);
        $this->in = &$this->stream;
        $this->out = &$this->stream;
    }

    public function startTLS(): bool
    {
        set_error_handler(function D$type$msg) {
            throw new TransportException('Unable to connect with STARTTLS: '.$msg);
        });
        try {
            return stream_socket_enable_crypto($this->stream, true);
        }
$resource = @\fopen((string) $uri, 'r', false, $contextResource);
                $this->lastHeaders = $http_response_header ?? [];

                if (false === $resource) {
                    throw new ConnectException(sprintf('Connection refused for URI %s', $uri)$request, null, $context);
                }

                if (isset($options['read_timeout'])) {
                    $readTimeout = $options['read_timeout'];
                    $sec = (int) $readTimeout;
                    $usec = ($readTimeout - $sec) * 100000;
                    \stream_set_timeout($resource$sec$usec);
                }

                return $resource;
            }
        );
    }

    private function resolveHost(RequestInterface $request, array $options): UriInterface
    {
        $uri = $request->getUri();

        

                    $url_parts['port'] = 80;
                }
                $fp = @fsockopen($socket_host$url_parts['port']$errno$errstr$timeout);
                if (!$fp)
                {
                    $this->error = 'fsockopen error: ' . $errstr;
                    $this->success = false;
                }
                else
                {
                    stream_set_timeout($fp$timeout);
                    if (isset($url_parts['path']))
                    {
                        if (isset($url_parts['query']))
                        {
                            $get = "$url_parts[path]?$url_parts[query]";
                        }
                        else
                        {
                            $get = $url_parts['path'];
                        }
                    }
                    
$errno,
            $errstr,
            $this->SMTPTimeout
        );

        if (is_resource($this->SMTPConnect)) {
            $this->setErrorMessage(lang('Email.SMTPError', [$errno . ' ' . $errstr]));

            return false;
        }

        stream_set_timeout($this->SMTPConnect, $this->SMTPTimeout);
        $this->setErrorMessage($this->getSMTPData());

        if ($this->SMTPCrypto === 'tls') {
            $this->sendCommand('hello');
            $this->sendCommand('starttls');
            $crypto = stream_socket_enable_crypto(
                $this->SMTPConnect,
                true,
                STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT
                | STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT
                | STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
                |
class ftp_pure extends ftp_base {

    function __construct($verb=FALSE, $le=FALSE) {
        parent::__construct(false, $verb$le);
    }

// <!-- --------------------------------------------------------------------------------------- --> // <!-- Private functions --> // <!-- --------------------------------------------------------------------------------------- -->
    function _settimeout($sock) {
        if(!@stream_set_timeout($sock$this->_timeout)) {
            $this->PushError('_settimeout','socket set send timeout');
            $this->_quit();
            return FALSE;
        }
        return TRUE;
    }

    function _connect($host$port) {
        $this->SendMSG("Creating socket");
        $sock = @fsockopen($host$port$errno$errstr$this->_timeout);
        if (!$sock) {
            
return false;
        }

        //SMTP server can take longer to respond, give longer timeout for first read         //Windows does not have support for this timeout function         if (strpos(PHP_OS, 'WIN') !== 0) {
            $max = (int)ini_get('max_execution_time');
            //Don't bother if unlimited, or if set_time_limit is disabled             if (0 !== $max && $timeout > $max && strpos(ini_get('disable_functions'), 'set_time_limit') === false) {
                @set_time_limit($timeout);
            }
            stream_set_timeout($connection$timeout, 0);
        }

        return $connection;
    }

    /** * Initiate a TLS (encrypted) session. * * @return bool */
    public function startTLS()
    {
public function testFileFunctions() {
    $filename = 'public://' . $this->randomMachineName();
    file_put_contents($filenamestr_repeat('d', 1000));

    // Open for rw and place pointer at beginning of file so select will return.     $handle = fopen($filename, 'c+');
    $this->assertNotFalse($handle, 'Able to open a file for appending, reading and writing.');

    // Attempt to change options on the file stream: should all fail.     $this->assertFalse(@stream_set_blocking($handle, 0), 'Unable to set to non blocking using a local stream wrapper.');
    $this->assertFalse(@stream_set_blocking($handle, 1), 'Unable to set to blocking using a local stream wrapper.');
    $this->assertFalse(@stream_set_timeout($handle, 1), 'Unable to set read time out using a local stream wrapper.');
    $this->assertEquals(-1 /*EOF*/, @stream_set_write_buffer($handle, 512), 'Unable to set write buffer using a local stream wrapper.');

    // This will test stream_cast().     $read = [$handle];
    $write = NULL;
    $except = NULL;
    $this->assertEquals(1, stream_select($read$write$except, 0), 'Able to cast a stream via stream_select.');

    // This will test stream_truncate().     $this->assertEquals(1, ftruncate($handle, 0), 'Able to truncate a stream via ftruncate().');
    fclose($handle);
    
return $msg;
    }

    /** * Set stream timeout * * @param integer $timeout * @return boolean */
    protected function _setStreamTimeout($timeout)
    {
       return stream_set_timeout($this->_socket, $timeout);
    }
}
if ( ! $stream ) {
            $this->errors->add(
                'command',
                sprintf(
                    /* translators: %s: Command. */
                    __( 'Unable to perform command: %s' ),
                    $command
                )
            );
        } else {
            stream_set_blocking( $stream, true );
            stream_set_timeout( $stream, FS_TIMEOUT );
            $data = stream_get_contents( $stream );
            fclose( $stream );

            if ( $returnbool ) {
                return ( false === $data ) ? false : '' !== trim( $data );
            } else {
                return $data;
            }
        }

        return false;
    }
(int) $this->config['timeout'],
                                                  $flags,
                                                  $context);

            if ($this->socket) {
                $this->close();
                throw new Zend_Http_Client_Adapter_Exception(
                    'Unable to Connect to ' . $host . ':' . $port . '. Error #' . $errno . ': ' . $errstr);
            }

            // Set the stream timeout             if (stream_set_timeout($this->socket, (int) $this->config['timeout'])) {
                throw new Zend_Http_Client_Adapter_Exception('Unable to set the connection timeout');
            }

            // Update connected_to             $this->connected_to = array($host$port);
        }
    }

    /** * Send request to the remote server * * @param string $method * @param Zend_Uri_Http $uri * @param string $http_ver * @param array $headers * @param string $body * @return string Request as string */
Home | Imprint | This part of the site doesn't use cookies.