Skip to content

Commit

Permalink
feat(http2_channel.dart): add support for prefixPath in ClientChannel…
Browse files Browse the repository at this point in the history
… to allow setting a prefix path for all requests

feat(http2_connection.dart): add support for prefixPath in Http2ClientConnection to include a prefix path in request paths
feat(xhr_transport.dart): update requestUri construction to include prefixPath in XhrClientConnection
feat(web_channel.dart): add validation for prefixPath in GrpcWebClientChannel to ensure it starts and ends with /
  • Loading branch information
GuillermoDLCO committed Apr 12, 2024
1 parent d3c3411 commit fdc836e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
9 changes: 7 additions & 2 deletions lib/src/client/http2_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,23 @@ class ClientChannel extends ClientChannelBase {
/// case it can be a Unix Domain Socket address.
final Object host;
final int port;
final String prefixPath;
final ChannelOptions options;

ClientChannel(
this.host, {
this.port = 443,
this.prefixPath = '',
this.options = const ChannelOptions(),
super.channelShutdownHandler,
});
}) : assert(
prefixPath.isEmpty ||
prefixPath.isNotEmpty && !prefixPath.endsWith('/'),
'prefixPath must be empty or does not start and end with /');

@override
ClientConnection createConnection() =>
Http2ClientConnection(host, port, options);
Http2ClientConnection(host, port, options, prefixPath: '/$prefixPath');
}

class ClientTransportConnectorChannel extends ClientChannelBase {
Expand Down
10 changes: 7 additions & 3 deletions lib/src/client/http2_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,15 @@ class Http2ClientConnection implements connection.ClientConnection {

ClientKeepAlive? keepAliveManager;

Http2ClientConnection(Object host, int port, this.options)
final String prefixPath;

Http2ClientConnection(Object host, int port, this.options,
{this.prefixPath = ''})
: _transportConnector = SocketTransportConnector(host, port, options);

Http2ClientConnection.fromClientTransportConnector(
this._transportConnector, this.options);
this._transportConnector, this.options,
{this.prefixPath = ''});

ChannelCredentials get credentials => options.credentials;

Expand Down Expand Up @@ -178,7 +182,7 @@ class Http2ClientConnection implements connection.ClientConnection {
final headers = createCallHeaders(
credentials.isSecure,
_transportConnector.authority,
path,
prefixPath + path,
timeout,
metadata,
compressionCodec,
Expand Down
7 changes: 1 addition & 6 deletions lib/src/client/transport/xhr_transport.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,7 @@ class XhrClientConnection implements ClientConnection {
metadata['X-Grpc-Web'] = '1';
}

if (prefixPath.isNotEmpty && prefixPath.endsWith('/')) {
throw ArgumentError.value(
prefixPath, 'prefixPath', 'must not end with a slash');
}

var requestUri = uri.resolve('$prefixPath$path');
var requestUri = uri.resolve(prefixPath + path);
if (callOptions is WebCallOptions &&
callOptions.bypassCorsPreflight == true) {
requestUri = cors.moveHttpHeadersToQueryParam(metadata, requestUri);
Expand Down
6 changes: 5 additions & 1 deletion lib/src/client/web_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ class GrpcWebClientChannel extends ClientChannelBase {
final String prefixPath;

GrpcWebClientChannel.xhr(this.uri,
{this.prefixPath = '', super.channelShutdownHandler});
{this.prefixPath = '', super.channelShutdownHandler})
: assert(
prefixPath.isEmpty ||
prefixPath.isNotEmpty && !prefixPath.endsWith('/'),
'prefixPath must be empty or does not start and end with /');

@override
ClientConnection createConnection() {
Expand Down

0 comments on commit fdc836e

Please sign in to comment.