normalizeProxyHtml static method

String normalizeProxyHtml(
  1. String body, {
  2. Map<String, String>? headers,
})

Normalizes proxy response payloads into usable HTML:

  • unwraps known JSON envelopes (e.g. allorigins { contents: ... })
  • decodes HTML entities when payload appears escaped
  • removes wrapping quotes around full-document strings

Implementation

static String normalizeProxyHtml(
  String body, {
  Map<String, String>? headers,
}) {
  if (body.trim().isEmpty) {
    return body;
  }

  var html = _extractHtmlFromPossibleJsonEnvelope(body, headers: headers);
  html = _stripWrappingQuotes(html.trim());

  if (_shouldDecodeEntities(html)) {
    // Decode up to twice for doubly escaped payloads such as &amp;lt;html...&amp;gt;
    var decoded = _decodeHtmlEntitiesOnce(html);
    if (_shouldDecodeEntities(decoded)) {
      decoded = _decodeHtmlEntitiesOnce(decoded);
    }
    if (looksLikeHtml(decoded)) {
      html = decoded;
    }
  }

  // Some proxies can return URL-encoded HTML payloads.
  if (!looksLikeHtml(html) && html.contains('%3C')) {
    try {
      final decodedUrlEncoded = Uri.decodeFull(html);
      if (looksLikeHtml(decodedUrlEncoded)) {
        html = decodedUrlEncoded;
      }
    } catch (_) {
      // Ignore malformed URI payloads.
    }
  }

  return html;
}