Skip to content

Commit

Permalink
Implement proxy support (configurable). Ref #86.
Browse files Browse the repository at this point in the history
  • Loading branch information
dom96 committed Dec 29, 2015
1 parent 2f57dd1 commit 50d2637
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
4 changes: 4 additions & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 10 additions & 2 deletions src/nimble.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/nimblepkg/config.nim
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -21,6 +22,8 @@ proc initConfig(): Config =
else:
result.nimbleDir = getHomeDir() / ".babel"

result.httpProxy = initUri()

result.chcp = true
result.cloneUsingHttps = true

Expand Down Expand Up @@ -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":
Expand Down
22 changes: 21 additions & 1 deletion src/nimblepkg/options.nim
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

0 comments on commit 50d2637

Please sign in to comment.