Incompatibility between the Dart shelf and rpc packages? ('access-control-request-method' header)

205 views Asked by At

There seems to be an incompatibility between the shelf and rpc packages.

In rpc [0.4.1] config/api.dart: The handleHttpOptionsRequest expects the 'access-control-request-method' header to be a List:

    Future<HttpApiResponse> handleHttpOptionsRequest(
          ParsedHttpApiRequest request) async {
        var requestedHttpMethods = request.headers['access-control-request-method'];
        List<String> allowed = [];
        assert('OPTIONS'.allMatches(request.methodKey).length == 1);
        if (requestedHttpMethods != null) {
          requestedHttpMethods.forEach((httpMethod) {
            var methodKey =
                request.methodKey.replaceFirst('OPTIONS', httpMethod);
            final List<ApiConfigMethod> methods = _methodMap[methodKey];
            if (methods != null) {
              for (var method in methods) {
                if (method.matches(request)) {
                  allowed.add(httpMethod);
                  break;
               }
              }
            }
          });
        }

When using the shelf [0.5.7] package, the ParsedHttpApiRequest which is passed to this method contains an 'access-control-request-method' header which is a String.

This generates an exception...

When using rpc with dart.io HttpServer, the code works fine and ParsedHttpApiRequest has a List of Strings (with a single element) as value of the 'access-control-request-method' header.

Anyone uses dart rpc + shelf successfully? If so, any idea on how to avoid the exception?

=== [update] After the rpc fix, the previously mentioned issue seems to be fixed. Thanks for that! I now run into another issue, but I did not yet investigate in detail. Stack trace is:

shelf [0.5.7] shelf_rpc [0.0.3] rpc [0.4.2]:

     Error thrown by handler.
     type 'List' is not a subtype of type 'String' of 'value'.
     package:collection/src/canonicalized_map.dart 66:30  CanonicalizedMap.[]=
     package:collection/src/canonicalized_map.dart 71:39  CanonicalizedMap.addAll.<fn>
     dart:collection                                      _CompactLinkedHashMap.forEach
     package:collection/src/canonicalized_map.dart 71:18  CanonicalizedMap.addAll
     package:collection/src/canonicalized_map.dart 57:11  CanonicalizedMap.CanonicalizedMap.from
     package:shelf/src/response.dart 215:9                Response.Response
     package:shelf_rpc/shelf_rpc.dart 18:24               createRpcHandler.<fn>.<fn>

=> see incompatibility when running dart rpc and shelf (with shelf_rpc) related to headers which are lists (and not Strings)

1

There are 1 answers

6
Gustav Wibling On BEST ANSWER

This is a bug in the RPC package. I will fix this ASAP and publish a new version of the RPC package.

You are welcome to file this kind of issue under github at:

https://github.com/dart-lang/rpc/issues

Cheers, /gustav