emptygif
语言: 简体中文

所有语言和框架

Dart standard library (dart:io) Dart

一个用于 Dart standard library (dart:io) 的 Dart 代码片段,以空 GIF 响应对 /pixel.gif 的请求,GIF 在启动时只解码一次。

Dart standard library (dart:io) Dart

文件
server.dart
运行
dart run server.dart
import 'dart:convert';
import 'dart:io';

final gif = base64Decode('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==');

Future<void> main() async {
  final server = await HttpServer.bind(InternetAddress.anyIPv4, 8080);
  await for (final request in server) {
    final response = request.response;
    if (request.method == 'GET' && request.uri.path == '/pixel.gif') {
      response.headers
        ..contentType = ContentType('image', 'gif')
        ..set(HttpHeaders.cacheControlHeader, 'no-store');
      response.contentLength = gif.length;
      response.add(gif);
    } else {
      response.statusCode = HttpStatus.notFound;
    }
    await response.close();
  }
}

我们运行了此代码片段,并比对了它返回的字节。

验证

curl -sI http://localhost:8080/pixel.gif

官方网站: Dart standard library (dart:io)

更多 Dart 示例