Skip to content

Commit 0613074

Browse files
committed
RSCBC-147: Add support for http(s):// in connection string
1 parent 9e3ec65 commit 0613074

File tree

2 files changed

+108
-16
lines changed

2 files changed

+108
-16
lines changed

sdk/couchbase-connstr/src/lib.rs

Lines changed: 102 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -198,26 +198,40 @@ pub async fn resolve(
198198
conn_spec: ConnSpec,
199199
dns_config: impl Into<Option<DnsConfig>>,
200200
) -> error::Result<ResolvedConnSpec> {
201-
let (default_port, has_explicit_scheme, use_ssl) = if let Some(scheme) = &conn_spec.scheme {
201+
if let Some(scheme) = &conn_spec.scheme {
202202
match scheme.as_str() {
203-
"couchbase" => (DEFAULT_MEMD_PORT, true, false),
204-
"couchbases" => (DEFAULT_SSL_MEMD_PORT, true, true),
205-
"couchbase2" => {
206-
return handle_couchbase2_scheme(conn_spec);
203+
"couchbase" => {
204+
handle_couchbase_scheme(conn_spec, dns_config, DEFAULT_MEMD_PORT, true, false).await
207205
}
208-
"" => (DEFAULT_MEMD_PORT, false, false),
209-
_ => {
210-
return Err(ErrorKind::InvalidArgument {
211-
msg: "unrecognized scheme".to_string(),
212-
arg: "scheme".to_string(),
213-
}
214-
.into());
206+
"couchbases" => {
207+
handle_couchbase_scheme(conn_spec, dns_config, DEFAULT_SSL_MEMD_PORT, true, true)
208+
.await
215209
}
210+
"couchbase2" => handle_couchbase2_scheme(conn_spec),
211+
"http" => handle_http_scheme(conn_spec, DEFAULT_LEGACY_HTTP_PORT, true, false).await,
212+
"https" => handle_http_scheme(conn_spec, DEFAULT_LEGACY_HTTPS_PORT, true, true).await,
213+
"" => {
214+
handle_couchbase_scheme(conn_spec, dns_config, DEFAULT_MEMD_PORT, false, false)
215+
.await
216+
}
217+
_ => Err(ErrorKind::InvalidArgument {
218+
msg: "unrecognized scheme".to_string(),
219+
arg: "scheme".to_string(),
220+
}
221+
.into()),
216222
}
217223
} else {
218-
(DEFAULT_MEMD_PORT, false, false)
219-
};
224+
handle_couchbase_scheme(conn_spec, dns_config, DEFAULT_MEMD_PORT, false, false).await
225+
}
226+
}
220227

228+
async fn handle_couchbase_scheme(
229+
conn_spec: ConnSpec,
230+
dns_config: impl Into<Option<DnsConfig>>,
231+
default_port: u16,
232+
has_explicit_scheme: bool,
233+
use_ssl: bool,
234+
) -> error::Result<ResolvedConnSpec> {
221235
if let Some(srv_record) = conn_spec.srv_record() {
222236
match lookup_srv(
223237
&srv_record.scheme,
@@ -358,6 +372,80 @@ fn handle_couchbase2_scheme(conn_spec: ConnSpec) -> error::Result<ResolvedConnSp
358372
})
359373
}
360374

375+
async fn handle_http_scheme(
376+
conn_spec: ConnSpec,
377+
default_port: u16,
378+
has_explicit_scheme: bool,
379+
use_ssl: bool,
380+
) -> error::Result<ResolvedConnSpec> {
381+
if conn_spec.hosts.is_empty() {
382+
let (memd_port, http_port) = if use_ssl {
383+
(DEFAULT_SSL_MEMD_PORT, DEFAULT_LEGACY_HTTPS_PORT)
384+
} else {
385+
(DEFAULT_MEMD_PORT, DEFAULT_LEGACY_HTTP_PORT)
386+
};
387+
388+
return Ok(ResolvedConnSpec {
389+
use_ssl,
390+
memd_hosts: vec![Address {
391+
host: "127.0.0.1".to_string(),
392+
port: memd_port,
393+
}],
394+
http_hosts: vec![Address {
395+
host: "127.0.0.1".to_string(),
396+
port: http_port,
397+
}],
398+
couchbase2_host: None,
399+
srv_record: None,
400+
options: conn_spec.options,
401+
});
402+
}
403+
404+
let mut memd_hosts = vec![];
405+
let mut http_hosts = vec![];
406+
for address in conn_spec.hosts {
407+
if let Some(port) = &address.port {
408+
if !has_explicit_scheme && address.port != Some(default_port) {
409+
return Err(ErrorKind::InvalidArgument {
410+
msg: "ambiguous port without scheme".to_string(),
411+
arg: "port".to_string(),
412+
}
413+
.into());
414+
}
415+
416+
http_hosts.push(Address {
417+
host: address.host,
418+
port: *port,
419+
});
420+
} else {
421+
let (memd_port, http_port) = if use_ssl {
422+
(DEFAULT_SSL_MEMD_PORT, DEFAULT_LEGACY_HTTPS_PORT)
423+
} else {
424+
(DEFAULT_MEMD_PORT, DEFAULT_LEGACY_HTTP_PORT)
425+
};
426+
427+
memd_hosts.push(Address {
428+
host: address.host.clone(),
429+
port: memd_port,
430+
});
431+
432+
http_hosts.push(Address {
433+
host: address.host,
434+
port: http_port,
435+
});
436+
}
437+
}
438+
439+
Ok(ResolvedConnSpec {
440+
use_ssl,
441+
memd_hosts,
442+
http_hosts,
443+
couchbase2_host: None,
444+
srv_record: None,
445+
options: conn_spec.options,
446+
})
447+
}
448+
361449
async fn lookup_srv(
362450
scheme: &str,
363451
proto: &str,

sdk/couchbase-core/tests/common/default_agent_options.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ pub async fn create_default_options(config: TestSetupConfig) -> AgentOptions {
2323
};
2424

2525
AgentOptions::new(
26-
SeedConfig::new().memd_addrs(config.memd_addrs.clone()),
26+
SeedConfig::new()
27+
.memd_addrs(config.memd_addrs.clone())
28+
.http_addrs(config.http_addrs.clone()),
2729
Authenticator::PasswordAuthenticator(PasswordAuthenticator {
2830
username: config.username.clone(),
2931
password: config.password.clone(),
@@ -48,7 +50,9 @@ pub async fn create_options_without_bucket(config: TestSetupConfig) -> AgentOpti
4850
};
4951

5052
AgentOptions::new(
51-
SeedConfig::new().memd_addrs(config.memd_addrs.clone()),
53+
SeedConfig::new()
54+
.memd_addrs(config.memd_addrs.clone())
55+
.http_addrs(config.http_addrs.clone()),
5256
Authenticator::PasswordAuthenticator(PasswordAuthenticator {
5357
username: config.username.clone(),
5458
password: config.password.clone(),

0 commit comments

Comments
 (0)