flutter_cached_network_image icon indicating copy to clipboard operation
flutter_cached_network_image copied to clipboard

Need supporting http requests and https requests which have invalid ssl certs

Open guolingege opened this issue 4 years ago • 2 comments

as title, expecting adding it as an option thanks

guolingege avatar Oct 30 '21 02:10 guolingege

Is there a temporary solution?

bhlin790 avatar Jan 06 '22 09:01 bhlin790

// Copyright 2014 The Flutter Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file.

import 'dart:async'; import 'dart:io'; import 'dart:typed_data'; import 'dart:ui' as ui;

import 'package:flutter/foundation.dart';

import 'package:flutter/painting.dart';

/// The dart:io implementation of [image_provider.NetworkImage]. class NetworkImageSSL extends ImageProvider<NetworkImage> implements NetworkImage {   /// Creates an object that fetches the image at the given URL.   ///   /// The arguments [url] and [scale] must not be null.   const NetworkImageSSL(this.url, { this.scale = 1.0, this.headers })       : assert(url != null),         assert(scale != null);

  @override   final String url;

  @override   final double scale;

  @override   final Map<String, String> headers;

  @override   Future<NetworkImage> obtainKey(ImageConfiguration configuration) {     return SynchronousFuture<NetworkImage>(this);   }

  @override   ImageStreamCompleter load(NetworkImage key, DecoderCallback decode) {     // Ownership of this controller is handed off to [_loadAsync]; it is that     // method's responsibility to close the controller's stream when the image     // has been loaded or an error is thrown.     final StreamController<ImageChunkEvent> chunkEvents = StreamController<ImageChunkEvent>();

    return MultiFrameImageStreamCompleter(       codec: _loadAsync(key as NetworkImage, chunkEvents, decode),       chunkEvents: chunkEvents.stream,       scale: key.scale,       informationCollector: () {         return <DiagnosticsNode>[           DiagnosticsProperty<ImageProvider>('Image provider', this),           DiagnosticsProperty<NetworkImage>('Image key', key),         ];       },     );   }

  // Do not access this field directly; use [_httpClient] instead.   // We set autoUncompress to false to ensure that we can trust the value of   // the Content-Length HTTP header. We automatically uncompress the content   // in our call to [consolidateHttpClientResponseBytes].   static final HttpClient _sharedHttpClient = HttpClient()     ..badCertificateCallback =     ((X509Certificate cert, String host, int port) => true)     ..autoUncompress = false;

  static HttpClient get _httpClient {     HttpClient client = _sharedHttpClient;     assert(() {       if (debugNetworkImageHttpClientProvider != null)         client = debugNetworkImageHttpClientProvider();       return true;     }());     return client;   }

  Future<ui.Codec> _loadAsync(       NetworkImage key,       StreamController<ImageChunkEvent> chunkEvents,       DecoderCallback decode,       ) async {     try {       assert(key == this);

      final Uri resolved = Uri.base.resolve(key.url);       final HttpClientRequest request = await _httpClient.getUrl(resolved);       headers?.forEach((String name, String value) {         request.headers.add(name, value);       });       final HttpClientResponse response = await request.close();       if (response.statusCode != HttpStatus.ok) {         // The network may be only temporarily unavailable, or the file will be         // added on the server later. Avoid having future calls to resolve         // fail to check the network again.         PaintingBinding.instance.imageCache.evict(key);         throw NetworkImageLoadException(statusCode: response.statusCode, uri: resolved);       }

      final Uint8List bytes = await consolidateHttpClientResponseBytes(         response,         onBytesReceived: (int cumulative, int total) {           chunkEvents.add(ImageChunkEvent(             cumulativeBytesLoaded: cumulative,             expectedTotalBytes: total,           ));         },       );       if (bytes.lengthInBytes == 0)         throw Exception('NetworkImage is an empty file: $resolved');

      return decode(bytes);     } finally {       chunkEvents.close();     }   }

  @override   bool operator ==(Object other) {     if (other.runtimeType != runtimeType)       return false;     return other is NetworkImage         && other.url == url         && other.scale == scale;   }

  @override   int get hashCode => ui.hashValues(url, scale);

  @override   String toString() => '${objectRuntimeType(this, 'NetworkImage')}("$url", scale: $scale)'; }

------------------ Original ------------------ From: bhlin790 @.> Date: Thu,Jan 6,2022 5:54 PM To: Baseflow/flutter_cached_network_image @.> Cc: 欧巴刚弄死他 @.>, Author @.> Subject: Re: [Baseflow/flutter_cached_network_image] Need supporting httprequests and https requests which have invalid ssl certs (Issue #668)

Is there a temporary solution?

— Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you authored the thread.Message ID: @.***>

guolingege avatar Jan 10 '22 09:01 guolingege

bring this issue back from in-active status. Anyway to bypass this for flutter web?

giaotuancse avatar Oct 02 '23 04:10 giaotuancse

resolved in a global function:

import 'dart:io';
import 'dart:developer';

class VDHttpOverrides extends HttpOverrides {
  @override
  HttpClient createHttpClient(SecurityContext? context) {
    return super.createHttpClient(context)
      ..badCertificateCallback = (X509Certificate cert, String host, int port) {
        final k = '$host:$port';
        log('VDHttpOverrides_badCertificateCallback_host:port:$k');
        return k == 'api.xxxx.com:443';
      };
  }
}
void main(){
    HttpOverrides.global = VDHttpOverrides();
    runApp(new MyApp());
}

https://github.com/flutter/flutter/issues/19588

guolingege avatar Oct 20 '23 08:10 guolingege