private boolean canCompressFor()

in runtime/impl/src/main/java/com/google/apphosting/runtime/HttpCompression.java [224:302]


  private boolean canCompressFor(
      boolean ignoreGfe,
      @Nullable String userAgent,
      @Nullable String coding,
      @Nullable String type) {
    if ((userAgent == null) || userAgent.isEmpty()) {
      return false;
    }
    if ((coding == null) || coding.isEmpty()) {
      return false;
    }
    if ((type == null) || type.isEmpty()) {
      return false;
    }
    // 1st: if they don't ask for gzip don't give it to them
    if (!coding.startsWith("gzip")
        && // starts with "gzip"
        !coding.contains(" gzip")
        && // gzip is a word
        !coding.contains(",gzip")) {
      return false;
    } else if (ignoreGfe) {
      if (// GFE asked for gzip.
         coding.contains("gzip(gfe)")
         && // Client did not ask for gzip.
         !coding.replace("gzip(gfe)", "").contains("gzip")) {
        return false;
      }
    }
    // extract the actual type from the content type header
    try {
      MediaType mediaType = MediaType.parse(type);
      if (mediaType.type() != null && mediaType.subtype() != null) {
        type = mediaType.type() + "/" + mediaType.subtype();
      } else {
        type = "nodefaulttype";
      }
    } catch (IllegalArgumentException e) {
      type = "nodefaulttype";
    }

    // check for clients which handle compression properly
    if ((!userAgent.contains("Mozilla/") || userAgent.contains("Mozilla/4.0"))
        && !userAgent.contains(" MSIE ")
        && !userAgent.contains("Opera")
        && !isGoodGzipUserAgent(userAgent)) {
      // Check for override...
      int gzipPosition = userAgent.indexOf("gzip"); // how clients can insist
      if (gzipPosition != -1) {
        // but maybe ignore the override if it came from gfe.
        if (ignoreGfe && gzipPosition == userAgent.indexOf("gzip(gfe)")) {
          return false;
        }
      } else {
        return false;
      }
    }

    // Don't compress css/javascript for anything but browsers we
    // trust - currently IE, Opera, Mozilla, and safari.  This list
    // should be kept in sync with C++, net/httpserverconnection.cc
    if (COMPRESSABLE_CSS_JS.contains(type)
        && !userAgent.contains(" MSIE ")
        && !userAgent.contains("Opera")
        && !isGoodGzipUserAgent(userAgent)
        && !userAgent.contains("gzip")) {
      return false;
    }

    // otherwise, compress all text/ content types and
    // several application types that we allow to be compressed.
    return type.startsWith("text/")
        || COMPRESSABLE_CSS_JS.contains(type)
        || (type.startsWith("application/")
            && (type.endsWith("+xml") || type.endsWith("/xml") || type.endsWith("/csv")))
        ||
        // cloud printer raster format heavily compressible
        type.equals("image/pwg-raster");
  }