<commit_before><?php

/*
 * This file is part of Composer.
 *
 * (c) <NAME> <<EMAIL>>
 *     <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Composer\Util;

use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Downloader\TransportException;

/**
 * @author <NAME> <<EMAIL>>
 */
class RemoteFilesystem
{
    private $io;
    private $firstCall;
    private $bytesMax;
    private $originUrl;
    private $fileUrl;
    private $fileName;
    private $result;
    private $progress;
    private $lastProgress;

    /**
     * Constructor.
     *
     * @param IOInterface  $io  The IO instance
     */
    public function __construct(IOInterface $io)
    {
        $this->io = $io;
    }

    /**
     * Copy the remote file in local.
     *
     * @param string  $originUrl The orgin URL
     * @param string  $fileUrl   The file URL
     * @param string  $fileName  the local filename
     * @param boolean $progress  Display the progression
     *
     * @return Boolean true
     */
    public function copy($originUrl, $fileUrl, $fileName, $progress = true)
    {
        $this->get($originUrl, $fileUrl, $fileName, $progress);

        return $this->result;
    }

    /**
     * Get the content.
     *
     * @param string  $originUrl The orgin URL
     * @param string  $fileUrl   The file URL
     * @param boolean $progress  Display the progression
     *
     * @return string The content
     */
    public function getContents($originUrl, $fileUrl, $progress = true)
    {
        $this->get($originUrl, $fileUrl, null, $progress);

        return $this->result;
    }

    /**
     * Get file content or copy action.
     *
     * @param string  $originUrl The orgin URL
     * @param string  $fileUrl   The file URL
     * @param string  $fileName  the local filename
     * @param boolean $progress  Display the progression
     *
     * @throws TransportException When the file could not be downloaded
     */
    protected function get($originUrl, $fileUrl, $fileName = null, $progress = true)
    {
        $this->bytesMax = 0;
        $this->result = null;
        $this->originUrl = $originUrl;
        $this->fileUrl = $fileUrl;
        $this->fileName = $fileName;
        $this->progress = $progress;
        $this->lastProgress = null;

        $options = $this->getOptionsForUrl($originUrl);
        $ctx = StreamContextFactory::getContext($options, array('notification' => array($this, 'callbackGet')));

        if ($this->progress) {
            $this->io->write("    Downloading: <comment>connection...</comment>", false);
        }

        $result = @file_get_contents($fileUrl, false, $ctx);

        // fix for 5.4.0 https://bugs.php.net/bug.php?id=61336
        if (!empty($http_response_header[0]) && preg_match('{^HTTP/\S+ 404}i', $http_response_header[0])) {
            $result = false;
        }

        // decode gzip
        if (false !== $result && extension_loaded('zlib') && substr($fileUrl, 0, 4) === 'http') {
            $decode = false;
            foreach ($http_response_header as $header) {
                if (preg_match('{^content-encoding: *gzip *$}i', $header)) {
                    $decode = true;
                    continue;
                } elseif (preg_match('{^HTTP/}i', $header)) {
                    $decode = false;
                }
            }

            if ($decode) {
                if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
                    $result = zlib_decode($result);
                } else {
                    // work around issue with gzuncompress & co that do not work with all gzip checksums
                    $result = file_get_contents('compress.zlib://data:application/octet-stream;base64,'.base64_encode($result));
                }
            }
        }

        // handle copy command if download was successful
        if (false !== $result && null !== $fileName) {
            $result = (Boolean) @file_put_contents($fileName, $result);
        }

        // avoid overriding if content was loaded by a sub-call to get()
        if (null === $this->result) {
            $this->result = $result;
        }

        if ($this->progress) {
            $this->io->write('');<commit_msg>Fix progress display getting stuck at 95%<commit_after><?php

/*
 * This file is part of Composer.
 *
 * (c) <NAME> <<EMAIL>>
 *     <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Composer\Util;

use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Downloader\TransportException;

/**
 * @author <NAME> <<EMAIL>>
 */
class RemoteFilesystem
{
    private $io;
    private $firstCall;
    private $bytesMax;
    private $originUrl;
    private $fileUrl;
    private $fileName;
    private $result;
    private $progress;
    private $lastProgress;

    /**
     * Constructor.
     *
     * @param IOInterface  $io  The IO instance
     */
    public function __construct(IOInterface $io)
    {
        $this->io = $io;
    }

    /**
     * Copy the remote file in local.
     *
     * @param string  $originUrl The orgin URL
     * @param string  $fileUrl   The file URL
     * @param string  $fileName  the local filename
     * @param boolean $progress  Display the progression
     *
     * @return Boolean true
     */
    public function copy($originUrl, $fileUrl, $fileName, $progress = true)
    {
        $this->get($originUrl, $fileUrl, $fileName, $progress);

        return $this->result;
    }

    /**
     * Get the content.
     *
     * @param string  $originUrl The orgin URL
     * @param string  $fileUrl   The file URL
     * @param boolean $progress  Display the progression
     *
     * @return string The content
     */
    public function getContents($originUrl, $fileUrl, $progress = true)
    {
        $this->get($originUrl, $fileUrl, null, $progress);

        return $this->result;
    }

    /**
     * Get file content or copy action.
     *
     * @param string  $originUrl The orgin URL
     * @param string  $fileUrl   The file URL
     * @param string  $fileName  the local filename
     * @param boolean $progress  Display the progression
     *
     * @throws TransportException When the file could not be downloaded
     */
    protected function get($originUrl, $fileUrl, $fileName = null, $progress = true)
    {
        $this->bytesMax = 0;
        $this->result = null;
        $this->originUrl = $originUrl;
        $this->fileUrl = $fileUrl;
        $this->fileName = $fileName;
        $this->progress = $progress;
        $this->lastProgress = null;

        $options = $this->getOptionsForUrl($originUrl);
        $ctx = StreamContextFactory::getContext($options, array('notification' => array($this, 'callbackGet')));

        if ($this->progress) {
            $this->io->write("    Downloading: <comment>connection...</comment>", false);
        }

        $result = @file_get_contents($fileUrl, false, $ctx);

        // fix for 5.4.0 https://bugs.php.net/bug.php?id=61336
        if (!empty($http_response_header[0]) && preg_match('{^HTTP/\S+ 404}i', $http_response_header[0])) {
            $result = false;
        }

        // decode gzip
        if (false !== $result && extension_loaded('zlib') && substr($fileUrl, 0, 4) === 'http') {
            $decode = false;
            foreach ($http_response_header as $header) {
                if (preg_match('{^content-encoding: *gzip *$}i', $header)) {
                    $decode = true;
                    continue;
                } elseif (preg_match('{^HTTP/}i', $header)) {
                    $decode = false;
                }
            }

            if ($decode) {
                if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
                    $result = zlib_decode($result);
                } else {
                    // work around issue with gzuncompress & co that do not work with all gzip checksums
                    $result = file_get_contents('compress.zlib://data:application/octet-stream;base64,'.base64_encode($result));
                }
            }
        }

        // handle copy command if download was successful
        if (false !== $result && null !== $fileName) {
            $result = (Boolean) @file_put_contents($fileName, $result);
        }

        // avoid overriding if content was loaded by a sub-call to get()
        if (null === $this->result) {
            $this->result = $result;
        }

        if ($this->progress) {
            $this->io->overwrite("    Downloading: <comment>100%</comment>");
{'fraction_non_alphanumeric': 0.13171179440110142, 'fraction_numerical': 0.005851307939421753, 'mean_word_length': 2.0769502294387574, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 73, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3417886', 'n_tokens_mistral': 2563, 'n_tokens_neox': 2492, 'n_words': 677}