shelf icon indicating copy to clipboard operation
shelf copied to clipboard

FormatException when setting the transfer encoding header to chunked

Open bitjunk opened this issue 3 years ago • 0 comments
trafficstars

A FormatException is thrown when the transfer encoding header is set.

Sample Code:

Future<Response> _fetchFileHandler(Request req) async {

     final Directory directory = await getApplicationDocumentsDirectory(); // Fetch AppData directory path
     final File file = File('${directory.path}/my_file.txt'); // Open a sample text file
     await file.writeAsString("Hello"); // Write random string
     Stream<List<int>> sampleStream = file.openRead(); //Read stream

     Response response = Response.ok(
       sampleStream,
       headers: {
         'Content-Type': 'multipart/form-data',
         'Cache-Control': 'no-store',
         'Transfer-Encoding': 'chunked'  // Setting this doesn't work as expected
       },
       context: {
         'shelf.io.buffer_output': false,
       },
     );
     return response;
   }

Error log:

E/flutter (12437): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: FormatException: Invalid hexadecimal byte 0x48. (at offset 0)
E/flutter (12437): #0      _Sink._digitForByte (package:http_parser/src/chunked_coding/decoder.dart:175:5)
E/flutter (12437): #1      _Sink._decode (package:http_parser/src/chunked_coding/decoder.dart:88:19)
E/flutter (12437): #2      _Sink.addSlice (package:http_parser/src/chunked_coding/decoder.dart:56:20)
E/flutter (12437): #3      _Sink.add (package:http_parser/src/chunked_coding/decoder.dart:51:32)
E/flutter (12437): #4      _ConverterStreamEventSink.add (dart:convert/chunked_conversion.dart:72:18)
E/flutter (12437): #5      _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:111:24)
E/flutter (12437): #6      _rootRunUnary (dart:async/zone.dart:1434:47)
E/flutter (12437): #7      _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter (12437): #8      _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244:7)
E/flutter (12437): #9      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
E/flutter (12437): #10     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
E/flutter (12437): #11     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:774:19)
E/flutter (12437): #12     _StreamController._add (dart:async/stream_controller.dart:648:7)
E/flutter (12437): #13     _StreamController.add (dart:async/stream_controller.dart:596:5)
E/flutter (12437): #14     _FileStream._readBlock.<anonymous closure> (dart:io/file_impl.dart:98:19)
E/flutter (12437): #15     _rootRunUnary (dart:async/zone.dart:1434:47)
E/flutter (12437): #16     _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter (12437): <asynchronous suspension>

Note: Not setting the Transfer-Encoding header manually does work and the response headers seems to contain that by default. Response headers post a successful call.

HTTP/1.1 200 OK
x-powered-by: Dart with package:shelf
cache-control: no-store
date: xxxxxxxxxxxxxxx
transfer-encoding: chunked
x-frame-options: SAMEORIGIN
content-type: multipart/form-data
x-xss-protection: 1; mode=block
x-content-type-options: nosniff

bitjunk avatar Aug 14 '22 02:08 bitjunk