afterResponse method

  1. @override
Future<HttpClientResponse?> afterResponse(
  1. HttpClientRequest request,
  2. HttpClientResponse response
)
override

@params kraken: The Kraken widget that is requesting the resource. request: HttpClientResponse that containing the detail of the request. @return newRequest: A HttpClientResponse containing the response information or null if the kraken should load the resource itself.

Implementation

@override
Future<HttpClientResponse?> afterResponse(HttpClientRequest request, HttpClientResponse response) async {
  String requestId = _getRequestId(request);
  sendEventToFrontend(NetworkResponseReceivedEvent(
    requestId: requestId,
    loaderId: devtoolsService.controller!.view.contextId.toString(),
    url: request.uri.toString(),
    headers: _getHttpHeaders(request.headers),
    status: response.statusCode,
    statusText: response.reasonPhrase,
    mimeType: response.headers.value(HttpHeaders.contentTypeHeader) ?? 'text/plain',
    remoteIPAddress: response.connectionInfo!.remoteAddress.address,
    remotePort: response.connectionInfo!.remotePort,
    // HttpClientStreamResponse is the internal implementation for disk cache.
    fromDiskCache: response is HttpClientStreamResponse,
    encodedDataLength: response.contentLength,
    protocol: request.uri.scheme,
    type: _getRequestType(request),
    timestamp: (DateTime.now().millisecondsSinceEpoch - _initialTimestamp) ~/ 1000,
  ));
  sendEventToFrontend(NetworkLoadingFinishedEvent(
    requestId: requestId,
    contentLength: response.contentLength,
    timestamp: (DateTime.now().millisecondsSinceEpoch - _initialTimestamp) ~/ 1000,
  ));
  Uint8List data = await consolidateHttpClientResponseBytes(response);
  _responseBuffers[requestId] = data;

  HttpClientStreamResponse proxyResponse = HttpClientStreamResponse(
      Stream.value(data),
      statusCode: response.statusCode,
      reasonPhrase: response.reasonPhrase,
      responseHeaders: _getHttpHeaders(response.headers));

  HttpClientInterceptor? customHttpClientInterceptor = _customHttpClientInterceptor;
  if (customHttpClientInterceptor != null) {
    return customHttpClientInterceptor.afterResponse(request, proxyResponse);
  } else {
    return Future.value(proxyResponse);
  }
}