Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#17): the file is now read as a stream and it is sent correctly to the user #20

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions packages/serinus/lib/src/http/internal_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,7 @@ class InternalRequest {
/// String body = await request.body();
/// ```
Future<String> body() async {
final data = await bytes();
if (data.isEmpty) {
return '';
}
final en = encoding ?? utf8;
return en.decode(data);
return await (encoding ?? utf8).decoder.bind(original).join();
}

/// This method is used to get the body of the request as a [dynamic] json object
Expand All @@ -145,7 +140,8 @@ class InternalRequest {
/// it is used internally by the [body], the [json] and the [stream] methods
Future<Uint8List> bytes() async {
try {
_bytes ??= await original.firstWhere((element) => element.isNotEmpty);
final data = await body();
_bytes ??= Uint8List.fromList((encoding ?? utf8).encode(data));
return _bytes!;
} catch (_) {
return Uint8List(0);
Expand Down
28 changes: 21 additions & 7 deletions packages/serinus/lib/src/http/internal_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,21 @@ class InternalResponse {
/// After sending the data, the response will be closed.
Future<void> send(List<int> data) async {
_original.add(data);
_events.add(ResponseEvent.data);
_original.close();
_events.add(ResponseEvent.afterSend);
_events.add(ResponseEvent.close);
}

/// This method is used to send a stream of data to the response.
///
/// After sending the stream, the response will be closed.
Future<void> sendStream(Stream<List<int>> stream) async {
await _original.addStream(stream);
_events.add(ResponseEvent.data);
_original.close();
_events.add(ResponseEvent.afterSend);
_events.add(ResponseEvent.close);
}

/// This method is used to set the status code of the response.
Expand Down Expand Up @@ -97,9 +111,6 @@ class InternalResponse {
final rendered = await (result.data is View
? viewEngine!.render(result.data)
: viewEngine!.renderString(result.data));
_events.add(ResponseEvent.data);
_events.add(ResponseEvent.afterSend);
_events.add(ResponseEvent.close);
return send(utf8.encode(rendered));
}
headers(result.headers);
Expand All @@ -111,8 +122,13 @@ class InternalResponse {
if (result.contentLength != null) {
_original.headers.contentLength = result.contentLength!;
}
var data = result.data;
var coding = _original.headers['transfer-encoding']?.join(';');
final data = result.data;
final coding = _original.headers['transfer-encoding']?.join(';');
if(data is File) {
final readPipe = data.openRead();
await sendStream(readPipe);
return;
}
if (coding != null && !equalsIgnoreAsciiCase(coding, 'identity')) {
// If the response is already in a chunked encoding, de-chunk it because
// otherwise `dart:io` will try to add another layer of chunking.
Expand All @@ -131,8 +147,6 @@ class InternalResponse {
_original.headers.set(HttpHeaders.dateHeader, DateTime.now().toUtc());
}
_events.add(ResponseEvent.data);
_events.add(ResponseEvent.afterSend);
_events.add(ResponseEvent.close);
return send(utf8.encode(data.toString()));
}
}
2 changes: 1 addition & 1 deletion packages/serinus/lib/src/http/response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class Response {
factory Response.file(File file,
{int statusCode = 200, ContentType? contentType}) {
return Response._(
file.readAsBytesSync(), statusCode, contentType ?? ContentType.binary);
file, statusCode, contentType ?? ContentType.binary);
}

/// Factory constructor to create a response with a redirect status code.
Expand Down
2 changes: 1 addition & 1 deletion packages/serinus/test/http/responses_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void main() async {

expect(
response.headers.contentType?.mimeType, 'application/octet-stream');
expect(body, '[111, 107, 33]');
expect(body, 'ok!');
});
test(
'''when a 'Response.redirect' is called, then the request should be redirected to the corresponding route''',
Expand Down
Loading