Skip to content

Commit e74e43c

Browse files
committed
Compress all HTTP requests with GZip
1 parent 3039da7 commit e74e43c

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

core/src/main/java/xyz/gianlu/librespot/core/Session.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import com.spotify.explicit.ExplicitContentPubsub.UserAttributesUpdate;
1111
import okhttp3.Authenticator;
1212
import okhttp3.*;
13+
import okio.BufferedSink;
14+
import okio.GzipSink;
15+
import okio.Okio;
1316
import org.apache.log4j.Logger;
1417
import org.jetbrains.annotations.Contract;
1518
import org.jetbrains.annotations.NotNull;
@@ -142,6 +145,36 @@ public Request authenticate(Route route, @NotNull Response response) {
142145
}
143146
}
144147

148+
builder.addInterceptor(chain -> {
149+
Request original = chain.request();
150+
RequestBody body;
151+
if ((body = original.body()) == null || original.header("Content-Encoding") != null)
152+
return chain.proceed(original);
153+
154+
Request compressedRequest = original.newBuilder()
155+
.header("Content-Encoding", "gzip")
156+
.method(original.method(), new RequestBody() {
157+
@Override
158+
public MediaType contentType() {
159+
return body.contentType();
160+
}
161+
162+
@Override
163+
public long contentLength() {
164+
return -1;
165+
}
166+
167+
@Override
168+
public void writeTo(@NotNull BufferedSink sink) throws IOException {
169+
try (BufferedSink gzipSink = Okio.buffer(new GzipSink(sink))) {
170+
body.writeTo(gzipSink);
171+
}
172+
}
173+
}).build();
174+
175+
return chain.proceed(compressedRequest);
176+
});
177+
145178
return builder.build();
146179
}
147180

0 commit comments

Comments
 (0)