Fixes SSL httpclient and problems with hidden path splitting in OS.
Added babel's libs dir to Nimrod's path.
This commit is contained in:
parent
d7adc7c328
commit
fa198f5be1
3 changed files with 23 additions and 13 deletions
|
|
@ -36,7 +36,7 @@ path="$lib/windows"
|
||||||
path="$lib/posix"
|
path="$lib/posix"
|
||||||
path="$lib/ecmas"
|
path="$lib/ecmas"
|
||||||
path="$lib/pure/unidecode"
|
path="$lib/pure/unidecode"
|
||||||
#recursivePath:"$home/.babel/lib"
|
path="$home/.babel/libs/"
|
||||||
|
|
||||||
@if release or quick:
|
@if release or quick:
|
||||||
obj_checks:off
|
obj_checks:off
|
||||||
|
|
|
||||||
|
|
@ -238,8 +238,15 @@ type
|
||||||
httpCONNECT ## Converts the request connection to a transparent
|
httpCONNECT ## Converts the request connection to a transparent
|
||||||
## TCP/IP tunnel, usually used for proxies.
|
## TCP/IP tunnel, usually used for proxies.
|
||||||
|
|
||||||
|
when not defined(ssl):
|
||||||
|
type PSSLContext = ref object
|
||||||
|
let defaultSSLContext = nil
|
||||||
|
else:
|
||||||
|
let defaultSSLContext = newContext(verifyMode = CVerifyNone)
|
||||||
|
|
||||||
proc request*(url: string, httpMethod = httpGET, extraHeaders = "",
|
proc request*(url: string, httpMethod = httpGET, extraHeaders = "",
|
||||||
body = ""): TResponse =
|
body = "",
|
||||||
|
sslContext: PSSLContext = defaultSSLContext): TResponse =
|
||||||
## | Requests ``url`` with the specified ``httpMethod``.
|
## | Requests ``url`` with the specified ``httpMethod``.
|
||||||
## | Extra headers can be specified and must be seperated by ``\c\L``
|
## | Extra headers can be specified and must be seperated by ``\c\L``
|
||||||
var r = parseUrl(url)
|
var r = parseUrl(url)
|
||||||
|
|
@ -257,7 +264,7 @@ proc request*(url: string, httpMethod = httpGET, extraHeaders = "",
|
||||||
var port = TPort(80)
|
var port = TPort(80)
|
||||||
if r.scheme == "https":
|
if r.scheme == "https":
|
||||||
when defined(ssl):
|
when defined(ssl):
|
||||||
s.wrapSocket(verifyMode = CVerifyNone)
|
sslContext.wrapSocket(s)
|
||||||
else:
|
else:
|
||||||
raise newException(EHttpRequestErr, "SSL support was not compiled in. Cannot connect over SSL.")
|
raise newException(EHttpRequestErr, "SSL support was not compiled in. Cannot connect over SSL.")
|
||||||
port = TPort(443)
|
port = TPort(443)
|
||||||
|
|
@ -277,7 +284,7 @@ proc redirection(status: string): bool =
|
||||||
if status.startsWith(i):
|
if status.startsWith(i):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
proc get*(url: string, maxRedirects = 5): TResponse =
|
proc get*(url: string, maxRedirects = 5, sslContext: PSSLContext = defaultSSLContext): TResponse =
|
||||||
## | GET's the ``url`` and returns a ``TResponse`` object
|
## | GET's the ``url`` and returns a ``TResponse`` object
|
||||||
## | This proc also handles redirection
|
## | This proc also handles redirection
|
||||||
result = request(url)
|
result = request(url)
|
||||||
|
|
@ -285,24 +292,24 @@ proc get*(url: string, maxRedirects = 5): TResponse =
|
||||||
if result.status.redirection():
|
if result.status.redirection():
|
||||||
var locationHeader = result.headers["Location"]
|
var locationHeader = result.headers["Location"]
|
||||||
if locationHeader == "": httpError("location header expected")
|
if locationHeader == "": httpError("location header expected")
|
||||||
result = request(locationHeader)
|
result = request(locationHeader, sslContext = sslContext)
|
||||||
|
|
||||||
proc getContent*(url: string): string =
|
proc getContent*(url: string, sslContext: PSSLContext = defaultSSLContext): string =
|
||||||
## | GET's the body and returns it as a string.
|
## | GET's the body and returns it as a string.
|
||||||
## | Raises exceptions for the status codes ``4xx`` and ``5xx``
|
## | Raises exceptions for the status codes ``4xx`` and ``5xx``
|
||||||
var r = get(url)
|
var r = get(url, sslContext = sslContext)
|
||||||
if r.status[0] in {'4','5'}:
|
if r.status[0] in {'4','5'}:
|
||||||
raise newException(EHTTPRequestErr, r.status)
|
raise newException(EHTTPRequestErr, r.status)
|
||||||
else:
|
else:
|
||||||
return r.body
|
return r.body
|
||||||
|
|
||||||
proc post*(url: string, extraHeaders = "", body = "",
|
proc post*(url: string, extraHeaders = "", body = "",
|
||||||
maxRedirects = 5): TResponse =
|
maxRedirects = 5, sslContext: PSSLContext = defaultSSLContext): TResponse =
|
||||||
## | POST's ``body`` to the ``url`` and returns a ``TResponse`` object.
|
## | POST's ``body`` to the ``url`` and returns a ``TResponse`` object.
|
||||||
## | This proc adds the necessary Content-Length header.
|
## | This proc adds the necessary Content-Length header.
|
||||||
## | This proc also handles redirection.
|
## | This proc also handles redirection.
|
||||||
var xh = extraHeaders & "Content-Length: " & $len(body) & "\c\L"
|
var xh = extraHeaders & "Content-Length: " & $len(body) & "\c\L"
|
||||||
result = request(url, httpPOST, xh, body)
|
result = request(url, httpPOST, xh, body, sslContext)
|
||||||
for i in 1..maxRedirects:
|
for i in 1..maxRedirects:
|
||||||
if result.status.redirection():
|
if result.status.redirection():
|
||||||
var locationHeader = result.headers["Location"]
|
var locationHeader = result.headers["Location"]
|
||||||
|
|
@ -310,7 +317,8 @@ proc post*(url: string, extraHeaders = "", body = "",
|
||||||
var meth = if result.status != "307": httpGet else: httpPost
|
var meth = if result.status != "307": httpGet else: httpPost
|
||||||
result = request(locationHeader, meth, xh, body)
|
result = request(locationHeader, meth, xh, body)
|
||||||
|
|
||||||
proc postContent*(url: string, extraHeaders = "", body = ""): string =
|
proc postContent*(url: string, extraHeaders = "", body = "",
|
||||||
|
sslContext: PSSLContext = defaultSSLContext): string =
|
||||||
## | POST's ``body`` to ``url`` and returns the response's body as a string
|
## | POST's ``body`` to ``url`` and returns the response's body as a string
|
||||||
## | Raises exceptions for the status codes ``4xx`` and ``5xx``
|
## | Raises exceptions for the status codes ``4xx`` and ``5xx``
|
||||||
var r = post(url, extraHeaders, body)
|
var r = post(url, extraHeaders, body)
|
||||||
|
|
@ -319,11 +327,12 @@ proc postContent*(url: string, extraHeaders = "", body = ""): string =
|
||||||
else:
|
else:
|
||||||
return r.body
|
return r.body
|
||||||
|
|
||||||
proc downloadFile*(url: string, outputFilename: string) =
|
proc downloadFile*(url: string, outputFilename: string,
|
||||||
|
sslContext: PSSLContext = defaultSSLContext) =
|
||||||
## Downloads ``url`` and saves it to ``outputFilename``
|
## Downloads ``url`` and saves it to ``outputFilename``
|
||||||
var f: TFile
|
var f: TFile
|
||||||
if open(f, outputFilename, fmWrite):
|
if open(f, outputFilename, fmWrite):
|
||||||
f.write(getContent(url))
|
f.write(getContent(url, sslContext))
|
||||||
f.close()
|
f.close()
|
||||||
else:
|
else:
|
||||||
fileError("Unable to open file")
|
fileError("Unable to open file")
|
||||||
|
|
|
||||||
|
|
@ -554,7 +554,8 @@ proc splitFile*(path: string): tuple[dir, name, ext: string] {.
|
||||||
var dotPos = path.len
|
var dotPos = path.len
|
||||||
for i in countdown(len(path)-1, 0):
|
for i in countdown(len(path)-1, 0):
|
||||||
if path[i] == ExtSep:
|
if path[i] == ExtSep:
|
||||||
if dotPos == path.len and i > 0: dotPos = i
|
if dotPos == path.len and i > 0 and
|
||||||
|
path[i-1] notin {dirsep, altsep}: dotPos = i
|
||||||
elif path[i] in {dirsep, altsep}:
|
elif path[i] in {dirsep, altsep}:
|
||||||
sepPos = i
|
sepPos = i
|
||||||
break
|
break
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue