Composer 是如何重新定义 packagist 仓库的?

How does the Composer trick with re-defining the packagist repo work?

在issue (#3156)讨论“composer is very slow" on the Composer GitHub page is suggested

re-define the packagist repo with a https:// url in the global config using:

$ composer config --global repo.packagist composer https://packagist.org

That should work around the downgrade issue, but it'd be interesting to resolve it of course.

它确实带来了明显的速度提升。我刚刚针对 Zend Framework 2 测试了它(见下面的测试)。

它是如何工作的?(为什么禁用 allow_ssl_downgrade 选项会使过程更快?)


编辑

I 运行 composer create-project zendframework/zendframework 结合两个因素:缓存和关闭 allow_ssl_downgrade 重新定义 packagist 仓库。对于由此产生的四个案例,我得到了以下结果:

默认配置:

config: default ([repositories.packagist.url] https?://packagist.org, [repositories.packagist.allow_ssl_downgrade] true)
cache: empty (composer clear-cache)
result: 3m38s

config: default ([repositories.packagist.url] https?://packagist.org, [repositories.packagist.allow_ssl_downgrade] true) 
cache:  not empty
result: 54s

config: changed ([repositories.packagist.url] https://packagist.org)
cache:  empty (composer clear-cache)
result: 3m34s

config: changed ([repositories.packagist.url] https://packagist.org)
cache:  not empty
result: 56s

总结:禁用allow_ssl_downgrade的"trick"没有速度提升。

不过,如果知道:allow_ssl_downgrade 选项实际上做了什么?(这个 "downgrading" 是什么意思?有什么优点和缺点?)

因为您第二次 运行 composer create-project zendframework/zendframework 它从作曲家的缓存中获取了所有内容,而不是再次下载!

你可以看到它输出类似下面的内容,如果你第二次 运行 它说 Loading from cache:

Installing zendframework/zendframework (2.5.2)
  - Installing zendframework/zendframework (2.5.2)
    Loading from cache

确保在测试之间 运行 composer clear-cache 以获得可靠的结果。

编辑//

如果我们查看 Composer 的源代码,我们可以找到 this line:

if ($this->allowSslDowngrade) {
    $this->url = str_replace('https://', 'http://', $this->url);
}

如果allowSslDowngrade = true
主要文件通过 https (see here) 检索,其余文件通过 http 检索,因为这样更快。其他文件的完整性通过 sha256 检查,这应该足以防止 MITM 攻击。

如果allowSslDowngrade = false
一切都通过 https

检索

您的测量结果存在差异可能是因网速或服务器 cpu/network 负载或其他因素不同所致。

你真的清除了你的缓存吗?因为这也会显着减少秒 运行.

的安装时间

我无法通过更改配置和清除两者之间的缓存来创建如此大的差异。