From 276f74bc2c0acc74c4b3a7aad451c041c9742922 Mon Sep 17 00:00:00 2001 From: Dajun Qin Date: Thu, 2 Jun 2016 18:43:02 +0800 Subject: [PATCH 1/3] add parameter unEscapeUser to handle username like testdb%dbuser in Azure --- dsn.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/dsn.go b/dsn.go index 73138bc57..fcd4a66bd 100644 --- a/dsn.go +++ b/dsn.go @@ -51,6 +51,7 @@ type Config struct { MultiStatements bool // Allow multiple statements in one query ParseTime bool // Parse time values to time.Time Strict bool // Return warnings as errors + UnEscapeUser bool // UnEscape username such as testdb%25testuesr:password..... } // FormatDSN formats the given Config into a DSN string which can be passed to @@ -353,6 +354,21 @@ func parseDSNParams(cfg *Config, params string) (err error) { switch value := param[1]; param[0] { // Disable INFILE whitelist / enable all files + case "unEscapeUser": + var isBool bool + cfg.UnEscapeUser, isBool = readBool(value) + if !isBool { + return errors.New("invalid bool value: " + value) + } + + if cfg.UnEscapeUser { + var err error + cfg.User, err = url.QueryUnescape(cfg.User) + if err != nil { + return errors.New("user unescape failed: " + err.Error()) + } + } + case "allowAllFiles": var isBool bool cfg.AllowAllFiles, isBool = readBool(value) From 920b7b467cb2eecaa55b6b8f8ddcae40cd7d29a9 Mon Sep 17 00:00:00 2001 From: Dajun Qin Date: Thu, 2 Jun 2016 18:59:44 +0800 Subject: [PATCH 2/3] Add a test --- dsn_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/dsn_test.go b/dsn_test.go index e6f0f83b1..1d3a36d61 100644 --- a/dsn_test.go +++ b/dsn_test.go @@ -229,3 +229,15 @@ func BenchmarkParseDSN(b *testing.B) { } } } + +func TestUnescapeUser(t *testing.T) { + dsn := "testdb%25dbuser:password@tcp(localhost:5555)/?unEscapeUser=true" + user := "testdb%dbuser" + cfg, err := ParseDSN(dsn) + + if err != nil { + t.Error(err.Error()) + } else if cfg.User != user { + t.Errorf("expected %v, got %v", user, dsn) + } +} From d900367dca31b0ecb762db327b1cbfd5c953acaf Mon Sep 17 00:00:00 2001 From: Dajun Qin Date: Thu, 2 Jun 2016 19:08:48 +0800 Subject: [PATCH 3/3] some changes --- AUTHORS | 2 +- README.md | 10 ++++++++++ dsn.go | 8 ++++---- dsn_test.go | 2 +- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/AUTHORS b/AUTHORS index 3774919d7..39565ef6b 100644 --- a/AUTHORS +++ b/AUTHORS @@ -44,7 +44,7 @@ Stan Putrya Stanley Gunawan Xiaobing Jiang Xiuming Chen - +Dajun Qin # Organizations Barracuda Networks, Inc. diff --git a/README.md b/README.md index c64aae264..b5318878f 100644 --- a/README.md +++ b/README.md @@ -299,6 +299,16 @@ Default: 0 I/O write timeout. The value must be a decimal number with an unit suffix ( *"ms"*, *"s"*, *"m"*, *"h"* ), such as *"30s"*, *"0.5m"* or *"1m30s"*. +##### `unescapeUser` + +``` +Type: bool +Default: false +``` + +`unescapeUser=true` let the library unescape `user`, sometimes the dsn looks like "testdb%25dbuser..." . + + ##### System Variables All other parameters are interpreted as system variables: diff --git a/dsn.go b/dsn.go index fcd4a66bd..df72b9cce 100644 --- a/dsn.go +++ b/dsn.go @@ -51,7 +51,7 @@ type Config struct { MultiStatements bool // Allow multiple statements in one query ParseTime bool // Parse time values to time.Time Strict bool // Return warnings as errors - UnEscapeUser bool // UnEscape username such as testdb%25testuesr:password..... + UnescapeUser bool // UnEscape username such as testdb%25testuesr:password..... } // FormatDSN formats the given Config into a DSN string which can be passed to @@ -354,14 +354,14 @@ func parseDSNParams(cfg *Config, params string) (err error) { switch value := param[1]; param[0] { // Disable INFILE whitelist / enable all files - case "unEscapeUser": + case "unescapeUser": var isBool bool - cfg.UnEscapeUser, isBool = readBool(value) + cfg.UnescapeUser, isBool = readBool(value) if !isBool { return errors.New("invalid bool value: " + value) } - if cfg.UnEscapeUser { + if cfg.UnescapeUser { var err error cfg.User, err = url.QueryUnescape(cfg.User) if err != nil { diff --git a/dsn_test.go b/dsn_test.go index 1d3a36d61..dfe263761 100644 --- a/dsn_test.go +++ b/dsn_test.go @@ -231,7 +231,7 @@ func BenchmarkParseDSN(b *testing.B) { } func TestUnescapeUser(t *testing.T) { - dsn := "testdb%25dbuser:password@tcp(localhost:5555)/?unEscapeUser=true" + dsn := "testdb%25dbuser:password@tcp(localhost:5555)/?unescapeUser=true" user := "testdb%dbuser" cfg, err := ParseDSN(dsn)