运行dart rpc和shelf(with shelf_rpc)时不兼容与列表(而不是字符串)的头相关 [英] incompatibility when running dart rpc and shelf (with shelf_rpc) related to headers which are lists (and not Strings)

查看:180
本文介绍了运行dart rpc和shelf(with shelf_rpc)时不兼容与列表(而不是字符串)的头相关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行dart rpc和shelf(with shelf_rpc)时,不兼容
与列表(而不是字符串)相关。

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

似乎有运行dart rpc和shelf(with shelf_rpc)时不兼容
与列表(而不是字符串)相关的头。

It seems that there is an incompatibility when running dart rpc and shelf (with shelf_rpc) related to headers which are lists (and not Strings).

抛出的错误是: 0.5.7],shelf_rpc [0.0.3],rpc [0.4.2]:):

Error thrown is ( for 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>

此问题的解决方法是更改​​shelf_rpc.dart以通过字符串替换列表:

A workaround around this problem is to change shelf_rpc.dart to replace the Lists by Strings:

    // Copyright (c) 2015, the Dart project authors.  Please see the AUTHORS file
    // for details. All rights reserved. Use of this source code is governed by a
    // BSD-style license that can be found in the LICENSE file.

    import "package:shelf/shelf.dart";
    import "package:rpc/rpc.dart";

    /// Creates a Shelf [Handler] that translates Shelf [Request]s to rpc's
    /// [HttpApiRequest] executes the request on the given [ApiServer] and then
    /// translates the returned rpc's [HttpApiResponse] to a Shelf [Response].
    Handler createRpcHandler(ApiServer apiServer) {
      return (Request request) {
        try {
          var apiRequest = new HttpApiRequest(request.method, request.requestedUri,
              request.headers, request.read());
          return apiServer.handleHttpApiRequest(apiRequest).then(
              (apiResponse) {
            // EXTRA: print and work-around
            printHeaders(apiResponse.headers, true);
            printHeaders(apiResponse.headers, false);
            // EXTRA <end>        
                return new Response(apiResponse.status, body: apiResponse.body,
                                    headers: apiResponse.headers);
              });
        } catch (e) {
          // Should never happen since the apiServer.handleHttpRequest method
          // always returns a response.
          return new Response.internalServerError(body: e.toString());
        }
      };
    }

    // EXTRA WORKAROUND: print headers & replace Lists by Strings
    printHeaders(Map headers, bool replaceListsBytStrings) {
      print('--HEADERS start---');
      headers.forEach(
                     (key, value) {
                   print('key: $key - value: $value - type: ${value.runtimeType}');
                   if ( (replaceListsBytStrings) && (value is List) ) {
                     String str = value.toString().substring(1, value.toString().length-1);
                     headers[key] = str;
                   }
                 });  
      print('--HEADERS end---');
    }

输出:

    --HEADERS start---
    key: content-type - value: application/json; charset=utf-8 - type: String
    key: cache-control - value: no-cache, no-store, must-revalidate - type: String
    key: pragma - value: no-cache - type: String
    key: expires - value: 0 - type: String
    key: access-control-allow-credentials - value: true - type: String
    key: access-control-allow-origin - value: * - type: String
    key: allow - value: [GET] - type: List
    key: access-control-allow-methods - value: [GET] - type: List
    key: access-control-allow-headers - value: origin, x-requested-with, content-type, accept - type: String
    key: Access-Control-Allow-Headers - value: null,Authorization, content-type - type: String
    --HEADERS end---
    --HEADERS start---
    key: content-type - value: application/json; charset=utf-8 - type: String
    key: cache-control - value: no-cache, no-store, must-revalidate - type: String
    key: pragma - value: no-cache - type: String
    key: expires - value: 0 - type: String
    key: access-control-allow-credentials - value: true - type: String
    key: access-control-allow-origin - value: * - type: String
    key: allow - value: GET - type: String
    key: access-control-allow-methods - value: GET - type: String
    key: access-control-allow-headers - value: origin, x-requested-with, content-type, accept - type: String
    key: Access-Control-Allow-Headers - value: null,Authorization, content-type - type: String
    --HEADERS end---


推荐答案

这应该在最新版本的Dart RPC包(v0.4.3)中修复。请尝试一下,让我知道它是如何工作的。

This should be fixed in the latest version of the Dart RPC package (v0.4.3). Please try it out and let me know how it works.

/ gustav

这篇关于运行dart rpc和shelf(with shelf_rpc)时不兼容与列表(而不是字符串)的头相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆