开发者

gem [Ruby] connecting through webproxy [windows]

I'm asking this age-old question on how go through your proxy when using ruby gem.

i tried setting the env variable HTTP_PROXY:

HTTP_PROXY=myusername:password@proxy.com:8080

but this does not work.

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

i tried using adding the tcp:// at the start but it shows http://tcp:// in the error message, so i assumed that was wrong too.

So, i tried an automatic connect via php using this code:

<?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_c开发者_Go百科ontext_create($aContext);

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

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

and incase it is trying to get a specific file, i handled it using 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

but still i get this error.

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

Any ideas?


Proxy servers, particularly authenticating proxy servers can throw a monkey wrench in all sorts of automatic update services. Unfortunately, gem does not properly deal with the HTTP_PROXY environment variable. In short, it doesn't know what to do with a BasicAuthentication response from a server--even though it tries.

Long story short, you'll have to set up a local proxy server that takes care of navigating through the proxy. It can't do anything that performs any transformations (like decompressing a compressed stream).

The myusername:password@company.com:8080 construct is not a valid URL. I know that it is a shortcut for windows authenticating proxy servers, but only the portion after the @ symbol is a valid URL.

Check out this FAQ: http://docs.rubygems.org/read/chapter/15

The problem is common enough.


Fixed PHP proxy now.

when adding a new source gem sends out

specs_4_8_gz

notice that it that _ represents .(dot). all you need to do is replace it. note that this is only done once. other gems will be sent out as their proper file name.

should look something like this.

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;
}

my full script will be posted on github.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜