question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Transparent support for Brotli content-encoding.

See original GitHub issue

Brotli content-encoding is supported by the major browsers, and it looks like there is pretty good support on the web server side too.

It could be supported transparently like gzip is, so that when OkHttp receives a request with the header Content-Encoding: br OkHttp uses Brotli decompression transparently.

Brotli content-encoding has reduced our payload size by about 20% for the average response.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
yschimkecommented, Dec 24, 2017

@swankjesse anything obviously wrong with this impl?

n.b. Consider this particular trivial code as public domain if you want to make a java library on github for it.

https://github.com/yschimke/oksocial/pull/295/files

package com.baulsupp.oksocial.brotli

import okhttp3.Interceptor
import okhttp3.Response
import okhttp3.ResponseBody
import okio.Okio
import org.brotli.dec.BrotliInputStream

/**
 * Transparent Brotli response support.
 *
 * Adds Accept-Encoding: br to existing encodings and checks (and strips) for Content-Encoding: br in responses
 */
object BrotliInterceptor : Interceptor {
  override fun intercept(chain: Interceptor.Chain): Response {
    val request = chain.request().newBuilder().addHeader("Accept-Encoding", "br").build()

    val response = chain.proceed(request)

    if (response.header("Content-Encoding") == "br") {
      val body = response.body()!!
      val decompressedSource = Okio.buffer(Okio.source(BrotliInputStream(body.source().inputStream())))
      return response.newBuilder()
              .removeHeader("Content-Encoding")
              .body(ResponseBody.create(body.contentType(), -1, decompressedSource))
              .build()
    }

    return response
  }
}
0reactions
swankjessecommented, Sep 11, 2019

We shipped this in 4.1!

Read more comments on GitHub >

github_iconTop Results From Across the Web

BrotliInterceptor - OkHttp - Square Open Source
Transparent Brotli response support. Adds Accept-Encoding: br to request and checks (and strips) for Content-Encoding: br in responses.
Read more >
Brotli Accept-Encoding/Content-Encoding - CanIUse
1 Supported in Chrome and Opera behind the 'Brotli Content-Encoding' flag · 2 Enabled since 27 May 2016 · 3 Support starting with...
Read more >
452335 - Support Brotli as a content-encoding method on ...
This launch-owp bug is about making it available as an HTT transfer-encoding method (e.g. Accept-Encoding: Brotli). Advantages of Brotli over gzip: - ...
Read more >
Boosting Site Speed Using Brotli Compression
It's designed for text compression and provides a 20%-30% reduction in size compared to gzip. Its encoding speed is generally slower than gzip...
Read more >
Brotli compression may not be supported over HTTP ...
br files with HTTP Response Header "Content-Encoding: br". Brotli compression may not be supported over HTTP connections. Migrate your server to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found