diff options
author | Dominik Picheta <dominikpicheta@googlemail.com> | 2012-06-03 19:09:42 +0100 |
---|---|---|
committer | Dominik Picheta <dominikpicheta@googlemail.com> | 2012-06-03 19:09:42 +0100 |
commit | 3294cb10a944e9c80ccb87173bbd0896597447c9 (patch) | |
tree | dd3df1ceac0a66e1c5745d041ac7952a9e44f125 /lib/pure/httpclient.nim | |
parent | bb850aafec622401d26e052cbd8cd6a7fef36156 (diff) | |
download | Nim-3294cb10a944e9c80ccb87173bbd0896597447c9.tar.gz |
Sockets are now buffered and have ssl support through openssl.
Diffstat (limited to 'lib/pure/httpclient.nim')
-rwxr-xr-x | lib/pure/httpclient.nim | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim index 3af08f040..c4dbd8509 100755 --- a/lib/pure/httpclient.nim +++ b/lib/pure/httpclient.nim @@ -42,6 +42,14 @@ ## body.add("--xyz--") ## ## echo(postContent("http://validator.w3.org/check", headers, body)) +## +## SSL/TLS support +## =============== +## This requires the OpenSSL library, fortunately it's widely used and installed +## on many operating systems. httpclient will use SSL automatically if you give +## any of the functions a url with the ``https`` schema, for example: +## ``https://github.com/``, you also have to compile with ``ssl`` defined like so: +## ``nimrod c -d:ssl ...``. import sockets, strutils, parseurl, parseutils, strtabs @@ -152,7 +160,7 @@ proc parseBody(d: var string, start: int, s: TSocket, result.add(moreData) proc parseResponse(s: TSocket): TResponse = - var d = s.recv.string + var d = s.recv.string # Warning: without a Connection: Close header this will not work. var i = 0 # Parse the version @@ -241,11 +249,19 @@ proc request*(url: string, httpMethod = httpGET, extraHeaders = "", headers.add(" HTTP/1.1\c\L") add(headers, "Host: " & r.hostname & "\c\L") + add(headers, "Connection: Close\c\L") add(headers, extraHeaders) add(headers, "\c\L") var s = socket() - s.connect(r.hostname, TPort(80)) + var port = TPort(80) + if r.scheme == "https": + when defined(ssl): + s.wrapSocket(verifyMode = CVerifyNone) + port = TPort(443) + if r.port != "": + port = TPort(r.port.parseInt) + s.connect(r.hostname, port) s.send(headers) if body != "": s.send(body) |