dio icon indicating copy to clipboard operation
dio copied to clipboard

[Exceptions] Invalid media type: expected no more input: charset=gbk,utf8

Open zhangzy7 opened this issue 2 years ago • 1 comments

New Issue Checklist

  • [鈭?] I have searched for a similar issue in the project and found none

Issue Info

Info Value
Platform Name windows
Platform Version win10
Dio Version 4.0.4
VSCode VSCode 1.65.1
Repro rate 100%
Repro with our demo prj yes
Demo project link

Issue Description and Steps

鎴戜娇鐢?nginx-1.20.2 鎼簡涓€涓畝鏄搘eb鏈嶅姟鍣紝鍦ㄥ悜鍏跺彂閫佽姹傛椂锛屾姤鍛婁簡寮傚父

DioError (DioError [DioErrorType.other]: Error on line 1, column 23: Invalid media type: expected no more input. 鈺?1 鈹?text/html; charset=gbk,utf8 |.....................................................^ 鈺?/p>

鍦╪ginx鐨勯厤缃枃浠朵腑閰嶇疆gbk鏄负浜嗗鐞嗗缂栫爜璺緞鐨勬儏鍐碉紝dio鏄惁鏀寔杩欓」鍔熻兘锛熸垨鑰呰鏈夋病鏈変粈涔堝姙娉曞鐞嗚繖绉嶉棶棰橈紵


Dio thorw exception when I request to a nginx-1.20.2 server. It set charset=gbk,utf8 to resolve some problem in multi-charset.

DioError (DioError [DioErrorType.other]: Error on line 1, column 23: Invalid media type: expected no more input. 鈺?1 鈹?text/html; charset=gbk,utf8 |....................................................^ 鈺?/p>

May Dio support this feature, or something else I can resolve it ?

zhangzy7 avatar Mar 11 '22 06:03 zhangzy7

@zhangzy7

This seems to be caused by an invalid media type declaration: text/html; charset=gbk,utf8

According to RFC 7231 the grammar for media types the type/subtype pair followed by zero or more "; parameter=value" pairs.

https://datatracker.ietf.org/doc/html/rfc7231#section-3.1.1.2

This is where the error occurred. https://github.com/flutterchina/dio/blob/6d04ec95d951bdecd19a2939d94de217d2d1ff7d/dio/lib/src/transformer.dart#L172

https://pub.dev/documentation/http_parser/latest/http_parser/MediaType/MediaType.parse.html

Parses a media type.

This will throw a FormatError if the media type is invalid.

MediaType.parse('text/html; charset=gbk,utf8');
Error on line 1, column 23: Invalid media type: expected no more input.
  鈺?1 鈹?text/html; charset=gbk,utf8
  鈹?                      ^
  鈺?
import 'package:http_parser/http_parser.dart';
import 'package:test/test.dart';

void main() {
  group('A group of tests', () {
    test('charset=gbk,utf8', () {
      final type = MediaType.parse('text/html; charset=gbk,utf8');
      expect(type.parameters['charset'], 'utf8');
    });

    test('charset=utf8', () {
      final type = MediaType.parse('text/html; charset=utf8');
      expect(type.parameters['charset'], 'utf8');
    });

    test('charset=gbk', () {
      final type = MediaType.parse('text/html; charset=gbk');
      expect(type.parameters['charset'], 'gbk');
    });
    test('two parameters', () {
      final type = MediaType.parse('text/html; charset=gbk;foo=bar');
      expect(type.parameters['charset'], 'gbk');
      expect(type.parameters['foo'], 'bar');
    });
  });
}

amondnet avatar Mar 11 '22 21:03 amondnet