@@ -324,10 +324,8 @@ func DialURL(rawurl string, options ...DialOption) (Conn, error) {
324
324
}
325
325
326
326
// DialURLContext connects to a Redis server at the given URL using the Redis
327
- // URI scheme. It supports:
328
- // redis - unencrypted tcp connection
329
- // rediss - TLS encrypted tcp connection
330
- // redis+socket - UNIX socket connection
327
+ // URI scheme. URLs should follow the draft IANA specification for the
328
+ // scheme (https://www.iana.org/assignments/uri-schemes/prov/redis).
331
329
func DialURLContext (ctx context.Context , rawurl string , options ... DialOption ) (Conn , error ) {
332
330
u , err := url .Parse (rawurl )
333
331
if err != nil {
@@ -344,66 +342,68 @@ func DialURLContext(ctx context.Context, rawurl string, options ...DialOption) (
344
342
)
345
343
switch u .Scheme {
346
344
case "redis" , "rediss" :
347
- // As per the IANA draft spec, the host defaults to localhost and
348
- // the port defaults to 6379.
349
- host , port , err := net .SplitHostPort (u .Host )
350
- if err != nil {
351
- // assume port is missing
352
- host = u .Host
353
- port = "6379"
354
- }
355
- if host == "" {
356
- host = "localhost"
357
- }
358
- network = "tcp"
359
- address = net .JoinHostPort (host , port )
360
-
361
- if u .User != nil {
362
- password , isSet := u .User .Password ()
363
- username := u .User .Username ()
364
- if isSet {
365
- if username != "" {
366
- // ACL
367
- options = append (options , DialUsername (username ), DialPassword (password ))
368
- } else {
369
- // requirepass - user-info username:password with blank username
370
- options = append (options , DialPassword (password ))
371
- }
372
- } else if username != "" {
373
- // requirepass - redis-cli compatibility which treats as single arg in user-info as a password
374
- options = append (options , DialPassword (username ))
375
- }
376
- }
377
- match := pathDBRegexp .FindStringSubmatch (u .Path )
378
- if len (match ) == 2 {
379
- if len (match [1 ]) > 0 {
380
- db , err = strconv .Atoi (match [1 ])
345
+ if (u .Host == "" || u .Host == "." ) && len (u .Path ) > 0 && u .Path [0 ] == '/' {
346
+ network = "unix"
347
+
348
+ address = u .Path
349
+ dbParameter := u .Query ().Get ("db" )
350
+ if dbParameter != "" {
351
+ db , err = strconv .Atoi (dbParameter )
381
352
if err != nil {
382
353
return nil , fmt .Errorf ("invalid database: %s" , u .Path [1 :])
383
354
}
355
+ if db != 0 {
356
+ options = append (options , DialDatabase (db ))
357
+ }
384
358
}
385
- if db != 0 {
386
- options = append (options , DialDatabase (db ))
387
- }
388
- } else if u .Path != "" {
389
- return nil , fmt .Errorf ("invalid database: %s" , u .Path [1 :])
390
- }
359
+ } else {
360
+ network = "tcp"
391
361
392
- options = append (options , DialUseTLS (u .Scheme == "rediss" ))
393
- case "redis+socket" :
394
- network = "unix"
395
- address = u .Path
396
- dbParameter := u .Query ().Get ("db" )
397
- if dbParameter != "" {
398
- db , err = strconv .Atoi (dbParameter )
362
+ // As per the IANA draft spec, the host defaults to localhost and
363
+ // the port defaults to 6379.
364
+ host , port , err := net .SplitHostPort (u .Host )
399
365
if err != nil {
400
- return nil , fmt .Errorf ("invalid database: %s" , u .Path [1 :])
366
+ // assume port is missing
367
+ host = u .Host
368
+ port = "6379"
369
+ }
370
+ if host == "" {
371
+ host = "localhost"
401
372
}
402
- if db != 0 {
403
- options = append (options , DialDatabase (db ))
373
+ address = net .JoinHostPort (host , port )
374
+ if u .User != nil {
375
+ password , isSet := u .User .Password ()
376
+ username := u .User .Username ()
377
+ if isSet {
378
+ if username != "" {
379
+ // ACL
380
+ options = append (options , DialUsername (username ), DialPassword (password ))
381
+ } else {
382
+ // requirepass - user-info username:password with blank username
383
+ options = append (options , DialPassword (password ))
384
+ }
385
+ } else if username != "" {
386
+ // requirepass - redis-cli compatibility which treats as single arg in user-info as a password
387
+ options = append (options , DialPassword (username ))
388
+ }
389
+ }
390
+ match := pathDBRegexp .FindStringSubmatch (u .Path )
391
+ if len (match ) == 2 {
392
+ if len (match [1 ]) > 0 {
393
+ db , err = strconv .Atoi (match [1 ])
394
+ if err != nil {
395
+ return nil , fmt .Errorf ("invalid database: %s" , u .Path [1 :])
396
+ }
397
+ }
398
+ if db != 0 {
399
+ options = append (options , DialDatabase (db ))
400
+ }
401
+ } else if u .Path != "" {
402
+ return nil , fmt .Errorf ("invalid database: %s" , u .Path [1 :])
404
403
}
405
404
}
406
- options = append (options , DialUseTLS (false ))
405
+
406
+ options = append (options , DialUseTLS (u .Scheme == "rediss" ))
407
407
default :
408
408
return nil , fmt .Errorf ("invalid redis URL scheme: %s" , u .Scheme )
409
409
}
0 commit comments