Implement proxy support (configurable). Ref #86.

This commit is contained in:
Dominik Picheta 2015-12-29 15:31:22 +00:00
commit 50d2637b04
4 changed files with 41 additions and 4 deletions

View file

@ -306,6 +306,10 @@ You can currently configure the following in this file:
* ``cloneUsingHttps`` - Whether to replace any ``git://`` inside URLs with
``https://``.
**Default: true**
* ``httpProxy`` - The URL of the proxy to use when downloading package listings.
Nimble will also attempt to read the ``http_proxy`` and ``https_proxy``
environment variables.
**Default: ""**
## Creating Packages

View file

@ -2,7 +2,7 @@
# BSD License. Look at license.txt for more info.
import httpclient, parseopt, os, strutils, osproc, pegs, tables, parseutils,
strtabs, json, algorithm, sets
strtabs, json, algorithm, sets, uri
from sequtils import toSeq
@ -66,8 +66,16 @@ proc update(options: Options) =
let url = list.urls[i]
echo("Trying ", url, "...")
let tempPath = options.getNimbleDir() / "packages_temp.json"
# Grab the proxy
let proxy = getProxy(options)
if not proxy.isNil:
var maskedUrl = proxy.url
if maskedUrl.password.len > 0: maskedUrl.password = "***"
echo("Using proxy ", maskedUrl)
try:
downloadFile(url, tempPath)
downloadFile(url, tempPath, proxy = getProxy(options))
except:
if i == <list.urls.len:
raise

View file

@ -1,6 +1,6 @@
# Copyright (C) Dominik Picheta. All rights reserved.
# BSD License. Look at license.txt for more info.
import parsecfg, streams, strutils, os, tables
import parsecfg, streams, strutils, os, tables, Uri
import tools, version, nimbletypes
@ -10,6 +10,7 @@ type
chcp*: bool # Whether to change the code page in .cmd files on Win.
packageLists*: Table[string, PackageList] ## URLs to packages.json files
cloneUsingHttps*: bool # Whether to replace git:// for https://
httpProxy*: Uri # Proxy for package list downloads.
PackageList* = object
name*: string
@ -21,6 +22,8 @@ proc initConfig(): Config =
else:
result.nimbleDir = getHomeDir() / ".babel"
result.httpProxy = initUri()
result.chcp = true
result.cloneUsingHttps = true
@ -83,6 +86,8 @@ proc parseConfig*(): Config =
result.chcp = parseBool(e.value)
of "cloneusinghttps":
result.cloneUsingHttps = parseBool(e.value)
of "httpproxy":
result.httpProxy = parseUri(e.value)
of "name":
case currentSection.normalize
of "packagelist":

View file

@ -1,7 +1,8 @@
# Copyright (C) Dominik Picheta. All rights reserved.
# BSD License. Look at license.txt for more info.
import json, strutils, os, parseopt, strtabs
import json, strutils, os, parseopt, strtabs, uri
from httpclient import Proxy, newProxy
import nimblepkg/config, nimblepkg/version,
nimblepkg/tools
@ -295,3 +296,22 @@ proc parseCmdLine*(): Options =
# Rename deprecated babel dir.
renameBabelToNimble(result)
proc getProxy*(options: Options): Proxy =
## Returns ``nil`` if no proxy is specified.
var url = initUri()
if ($options.config.httpProxy).len > 0:
url = options.config.httpProxy
else:
try:
if existsEnv("http_proxy"):
parseUri(getEnv("http_proxy"), url)
elif existsEnv("https_proxy"):
parseUri(getEnv("https_proxy"), url)
except ValueError:
echo("WARNING: Unable to parse proxy from environment: ",
getCurrentExceptionMsg())
if ($url).len > 0:
return newProxy($url, url.username & ":" & url.password)
else:
return nil