From d7e2d141e120cae31768a50820e708dc8de4c9f8 Mon Sep 17 00:00:00 2001 From: Bruce Doan Date: Sat, 1 Jul 2017 11:15:42 +0700 Subject: [PATCH] rethinkdb.nimble --- .gitignore | 2 +- private/connection.nim | 118 ++++++++++++++++++++++++++++++++--------- private/rql.nim | 2 +- 3 files changed, 96 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 2580f07..0aa81e7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ nimcache/ rethinkdb_data -tests/test.nim +tests/test* diff --git a/private/connection.nim b/private/connection.nim index 9ac0ba3..ef00002 100644 --- a/private/connection.nim +++ b/private/connection.nim @@ -1,13 +1,17 @@ import asyncnet, strutils, logging, json, struct, tables +import scram/client when not compileOption("threads"): import asyncdispatch else: import net - import ql2, types, utils, datum +const + HANDSHAKE_FIRST_MESSAGE = "{\"protocol_version\": 0,\"authentication_method\": \"SCRAM-SHA-256\",\"authentication\": \"$#\"}\x0" + HANDSHAKE_FINAL_MESSAGE = "{\"authentication\": \"$#\"}\x0" + when not compileOption("threads"): const BUFFER_SIZE = 512 @@ -15,7 +19,8 @@ when not compileOption("threads"): RethinkClient* = ref object of RootObj address: string port: Port - auth: string + username: string + password: string options: TableRef[string, MutableDatum] sock: AsyncSocket sockConnected: bool @@ -25,13 +30,15 @@ else: RethinkClient* = ref object of RootObj address: string port: Port - auth: string + username: string + password: string options: TableRef[string, MutableDatum] sock: Socket sockConnected: bool queryToken: uint64 type + RqlAuthError* = object of SystemError RqlDriverError* = object of SystemError RqlClientError* = object of SystemError RqlCompileError* = object of SystemError @@ -101,13 +108,12 @@ proc use*(r: RethinkClient, s: string) = term.args = @[newDatum(s)] r.addOption("db", &term) -proc newRethinkClient*(address = "127.0.0.1", port = Port(28015), auth = "", db = ""): RethinkClient = +proc newRethinkClient*(address = "127.0.0.1", port = Port(28015), db: string = nil): RethinkClient = ## Init new client instance assert address != "" result = new(RethinkClient) result.address = address result.port = port - result.auth = auth result.options = newTable[string, MutableDatum]() when not compileOption("threads"): result.sock = newAsyncSocket() @@ -122,36 +128,92 @@ when not compileOption("threads"): proc handshake(r: RethinkClient) {.async.} = when defined(debug): L.log(lvlDebug, "Preparing handshake...") - var data: string - if r.auth.len > 0: - data = pack(" 0: - data = pack("