Path: redb-route/redb.Route.Http/HttpConnectionFactory.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Http/HttpConnectionFactory.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Expressions;
namespace redb.Route.Http;
///
/// Named connection factory for HTTP. Register it in the route registry and reference it by name
/// from the endpoint URI (connectionFactory=my-api) so Basic/Bearer credentials and the TLS
/// certificate password never have to appear in the URI, and therefore can never reach logs,
/// telemetry, or the Tsak dashboard.
///
/// Note: the request/listen address (host and port) is deliberately NOT part of this factory — for
/// the HTTP connector it comes from the endpoint path (http://0.0.0.0:5088/api) and stays
/// there, so a factory can never silently redirect a route to a different host.
///
///
/// Example:
///
/// context.AddToRegistry("billing-api", new HttpConnectionFactory
/// {
/// AuthScheme = HttpAuthScheme.Bearer,
/// AuthToken = Environment.GetEnvironmentVariable("BILLING_TOKEN")
/// });
///
/// // route carries no credentials at all:
/// r.To("http://billing.corp.local/invoices?connectionFactory=billing-api")
///
///
///
public sealed class HttpConnectionFactory
{
/// Authentication scheme (None, Basic, Bearer).
public HttpAuthScheme AuthScheme { get; set; } = HttpAuthScheme.None;
/// Username for Basic auth.
public string? Username { get; set; }
/// Password for Basic auth.
[Sensitive]
public string? Password { get; set; }
///
/// Bearer token for Bearer auth. Supports ${...} expressions exactly like the URI form
/// (e.g. ${env:BILLING_TOKEN}, ${header.jwt}) — an expression is resolved per
/// request, a plain value is used as-is.
///
[Sensitive]
public string? AuthToken { get; set; }
/// Request timeout in milliseconds (default 30000).
public int Timeout { get; set; } = 30_000;
/// Serve/connect over TLS.
public bool Ssl { get; set; }
/// Path to the TLS certificate.
public string? SslCertPath { get; set; }
/// Password for the TLS certificate.
[Sensitive]
public string? SslCertPassword { get; set; }
///
/// Copies this factory's authentication and TLS settings onto the endpoint options, but only
/// for parameters the endpoint URI did not set explicitly — an inline URI value always wins,
/// so existing routes keep their behaviour.
///
internal void ApplyTo(HttpEndpointOptions options, EndpointUri uri)
{
var supplied = uri.RawParameters;
if (!supplied.ContainsKey(nameof(options.AuthScheme))) options.AuthScheme = AuthScheme;
if (!supplied.ContainsKey(nameof(options.Username))) options.Username = Username;
if (!supplied.ContainsKey(nameof(options.Password))) options.Password = Password;
if (!supplied.ContainsKey(nameof(options.AuthToken)) && AuthToken is not null)
{
// Mirror EndpointOptions.BindDynamicValue: a ${...} value becomes a per-request
// expression, anything else is wrapped as a static value.
options.AuthToken = AuthToken.Contains("${", StringComparison.Ordinal)
? DynamicValue.FromExpression(new StringExpression(AuthToken))
: DynamicValue.FromStatic(AuthToken);
}
if (!supplied.ContainsKey(nameof(options.Timeout))) options.Timeout = Timeout;
if (!supplied.ContainsKey(nameof(options.Ssl))) options.Ssl = Ssl;
if (!supplied.ContainsKey(nameof(options.SslCertPath))) options.SslCertPath = SslCertPath;
if (!supplied.ContainsKey(nameof(options.SslCertPassword)))
options.SslCertPassword = SslCertPassword;
}
}