1 /*
2  * Hunt - A redis client library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11  
12 module hunt.redis.util.RedisURIHelper;
13 
14 import hunt.net.util.HttpURI;
15 import hunt.Exceptions;
16 
17 import std.array;
18 import std.conv;
19 import std.string;
20 
21 /**
22  * 
23  */
24 class RedisURIHelper {
25 
26     private enum int DEFAULT_DB = 0;
27 
28     private enum string REDIS = "redis";
29     private enum string REDISS = "rediss";
30 
31     private this(){
32         throw new InstantiationError( "Must not instantiate this class" );
33     }
34 
35     static string getPassword(HttpURI uri) {
36         string userInfo = uri.getUserInfo();
37         if (userInfo !is null) {
38             return userInfo.split(":")[1];
39         }
40         return null;
41     }
42 
43     static int getDBIndex(HttpURI uri) {
44         string[] pathSplit = uri.getPath().split("/");
45         if (pathSplit.length > 1) {
46             string dbIndexStr = pathSplit[1];
47             if (dbIndexStr.empty()) {
48                 return DEFAULT_DB;
49             }
50             return to!int(dbIndexStr);
51         } else {
52             return DEFAULT_DB;
53         }
54     }
55 
56     static bool isValid(HttpURI uri) {
57         if (isEmpty(uri.getScheme()) || isEmpty(uri.getHost()) || uri.getPort() == -1) {
58             return false;
59         }
60 
61         return true;
62     }
63 
64     private static bool isEmpty(string value) {
65         return value.empty();
66     }
67 
68     static bool isRedisScheme(HttpURI uri) {
69         return REDIS == uri.getScheme();
70     }
71 
72     static bool isRedisSSLScheme(HttpURI uri) {
73         return REDISS == uri.getScheme();
74     }
75 
76 }