【问题标题】:gem [Ruby] connecting through webproxy [windows]gem [Ruby] 通过 webproxy [windows] 连接
【发布时间】:2011-06-02 18:55:46
【问题描述】:

我在问这个古老的问题,关于在使用 ruby​​ gem 时如何通过代理。

我尝试设置环境变量HTTP_PROXY:

HTTP_PROXY=myusername:password@proxy.com:8080

但这不起作用。

> gem install rails
ERROR:  While executing gem ... (URI::InvalidURIError)
    bad URI(is not URI?): http://myusername:password@proxy.com:8080

我尝试在开头添加tcp://,但它在错误消息中显示http://tcp://,所以我认为这也是错误的。

所以,我尝试使用以下代码通过 php 自动连接:

<?php
$path = 'http://rubygems.org/';

$auth = base64_encode('myusername:password');

file_put_contents('proxy.log', 'POST::', FILE_APPEND);
file_put_contents('proxy.log', print_r($_POST, true), FILE_APPEND);
file_put_contents('proxy.log', 'GET::', FILE_APPEND);
file_put_contents('proxy.log', print_r($_GET, true), FILE_APPEND);

$aContext = array(
    'http' => array(
        'method' => 'GET',
        'content' => http_build_query($_GET),
        'proxy' => 'tcp://proxy.com:8080',
        'request_fulluri' => true,
        'header' => "Proxy-Authorization: Basic $auth"
    )
);
$cxContext = stream_context_create($aContext);

$sFile = file_get_contents($path, false, $cxContext);

header('Content-type: application/gzip');
echo $sFile;

如果它试图获取特定文件,我使用 apache 处理它。

.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^(.*[^/])$ http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L] 

    RewriteCond $1 !^$
    RewriteCond $1 !^(index\.php)
    RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
# Options -Indexes

但我仍然收到此错误。

> gem sources -ahttp://mylocalhost/ror-proxy/
ERROR:  While executing gem ... (Zlib::GzipFile::Error)
not in gzip format

有什么想法吗?

【问题讨论】:

  • HTTP_PROXY 需要完整的 HTTP URL。试试HTTP_PROXY=http://myusername:password@proxy.com:8080

标签: php ruby windows xampp gem


【解决方案1】:

代理服务器,尤其是身份验证代理服务器可能会给各种自动更新服务带来麻烦。不幸的是,gem 没有正确处理HTTP_PROXY 环境变量。简而言之,它不知道如何处理来自服务器的 BasicAuthentication 响应——即使它尝试过。

长话短说,您必须设置一个本地代理服务器来负责浏览代理。它不能执行任何转换(例如解压缩压缩流)。

myusername:password@company.com:8080 构造不是有效的 URL。我知道这是windows验证代理服务器的快捷方式,但只有@符号后面的部分才是有效的URL。

查看此常见问题解答:http://docs.rubygems.org/read/chapter/15

这个问题很常见。

【讨论】:

  • 次要更正:Gem 进行基本身份验证,但不进行 NTLM 授权,根据您的常见问题参考。
  • 感谢@berin 的回答。我之前实际上读过那个文件。 #apserver,这是该文档中的一个建议,现在似乎是一个死项目。还有其他选择吗?
【解决方案2】:

现在修复 PHP 代理。

当添加一个新的源 gem 时发送出去

specs_4_8_gz

注意_ 代表.(dot)。您需要做的就是更换它。请注意,这仅执行一次。其他 gem 将作为其正确的文件名发送出去。

应该看起来像这样。

if (empty($_GET['_p'])) {
    header('Content-type: text/html');
} else {
    header('Content-type: application/gzip');

    $item = $_GET['_p'];
    $item = '.gz' == substr($item, -3) ? $item: str_replace('_', '.', $item);
    header('Content-Disposition: filename="' . $item . '"');
    $path .= $item;
}

我的完整脚本将发布在 github 上。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    • 2017-01-11
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多