美文网首页新收藏
OkHttp使用gzip

OkHttp使用gzip

作者: 还是那个没头脑 | 来源:发表于2021-09-24 16:16 被阅读0次

    OkHttp 在最终构建请求信息以及处理返回信息时,内部使用了一个叫做 BridgeInterceptor 的拦截器,该类的代码如下:

    public final class BridgeInterceptor implements Interceptor {
        private final CookieJar cookieJar;
    
        public BridgeInterceptor(CookieJar cookieJar) {
            this.cookieJar = cookieJar;
        }
    
        @Override 
        public Response intercept(Chain chain) throws IOException {
            Request userRequest = chain.request();
            Request.Builder requestBuilder = userRequest.newBuilder();
    
            RequestBody body = userRequest.body();
            if (body != null) {
                MediaType contentType = body.contentType();
                //自动增添加请求头 Content-Type
                if (contentType != null) {
                    requestBuilder.header("Content-Type", contentType.toString());
                }
    
                long contentLength = body.contentLength();
                //如果传输长度不为-1,则表示完整传输
                if (contentLength != -1) {
                    //设置头信息 Content-Length
                    requestBuilder.header("Content-Length", Long.toString(contentLength));
                    requestBuilder.removeHeader("Transfer-Encoding");
                } else {
                    //如果传输长度为-1,则表示分块传输,自动设置头信息         
                    requestBuilder.header("Transfer-Encoding", "chunked");
                    requestBuilder.removeHeader("Content-Length");
                }
            }
    
            if (userRequest.header("Host") == null) {
                requestBuilder.header("Host", hostHeader(userRequest.url(), false));
            }
    
            //如果没有设置头信息 Connection,则自动设置为 Keep-Alive
            if (userRequest.header("Connection") == null) {
                requestBuilder.header("Connection", "Keep-Alive");
            }
    
            // If we add an "Accept-Encoding: gzip" header field we're responsible for also decompressing
            // the transfer stream.
            boolean transparentGzip = false;
            if (userRequest.header("Accept-Encoding") == null && userRequest.header("Range") == null) {
                //如果我们没有在请求头信息里增加Accept-Encoding,在这里会自动设置头信息 Accept-Encoding = gzip
                transparentGzip = true;
                requestBuilder.header("Accept-Encoding", "gzip");
            }
    
            List<Cookie> cookies = cookieJar.loadForRequest(userRequest.url());
            if (!cookies.isEmpty()) {
                requestBuilder.header("Cookie", cookieHeader(cookies));
            }
    
            if (userRequest.header("User-Agent") == null) {
                requestBuilder.header("User-Agent", Version.userAgent());
            }
    
            Response networkResponse = chain.proceed(requestBuilder.build());
    
            HttpHeaders.receiveHeaders(cookieJar, userRequest.url(), networkResponse.headers());
    
            Response.Builder responseBuilder = networkResponse.newBuilder()
                    .request(userRequest);
    
            //如果返回的头信息里Content-Encoding = gzip,并且我们没有手动在请求头信息里设置 Accept-Encoding = gzip,则会进行 gzip 解压数据流
            if (transparentGzip
                    && "gzip".equalsIgnoreCase(networkResponse.header("Content-Encoding"))
                    && HttpHeaders.hasBody(networkResponse)) {
                GzipSource responseBody = new GzipSource(networkResponse.body().source());
                Headers strippedHeaders = networkResponse.headers().newBuilder()
                        .removeAll("Content-Encoding")
                        .removeAll("Content-Length")
                        .build();
                responseBuilder.headers(strippedHeaders);
                String contentType = networkResponse.header("Content-Type");
                responseBuilder.body(new RealResponseBody(contentType, -1L, Okio.buffer(responseBody)));
            }
    
            return responseBuilder.build();
        }
    }
    

    混淆后

    public final class a implements v {
    
        /* renamed from: a  reason: collision with root package name */
        private final m f33903a;
    
        public a(m mVar) {
            this.f33903a = mVar;
        }
    
        @Override // okhttp3.v
        public b0 a(v.a aVar) throws IOException {
            z J = aVar.J();
            z.a f2 = J.f();
            a0 a2 = J.a();
            if (a2 != null) {
                w c2 = a2.c();
                if (c2 != null) {
                    f2.b("Content-Type", c2.toString());
                }
                long b2 = a2.b();
                if (b2 != -1) {
                    f2.b("Content-Length", Long.toString(b2));
                    f2.a("Transfer-Encoding");
                } else {
                    f2.b("Transfer-Encoding", "chunked");
                    f2.a("Content-Length");
                }
            }
            boolean z = false;
            if (J.a("Host") == null) {
                f2.b("Host", b.a(J.h(), false));
            }
            if (J.a("Connection") == null) {
                f2.b("Connection", c.w);
            }
            if (J.a("Accept-Encoding") == null && J.a("Range") == null) {
                z = true;
                f2.b("Accept-Encoding", "gzip");
            }
            List<l> a3 = this.f33903a.a(J.h());
            if (!a3.isEmpty()) {
                f2.b("Cookie", a(a3));
            }
            if (J.a("User-Agent") == null) {
                f2.b("User-Agent", okhttp3.internal.c.a());
            }
            b0 a4 = aVar.a(f2.a());
            e.a(this.f33903a, J.h(), a4.v());
            b0.a a5 = a4.z().a(J);
            if (z && "gzip".equalsIgnoreCase(a4.c("Content-Encoding")) && e.b(a4)) {
                GzipSource gzipSource = new GzipSource(a4.c().t());
                a5.a(a4.v().c().d("Content-Encoding").d("Content-Length").a());
                a5.a(new h(a4.c("Content-Type"), -1, Okio.buffer(gzipSource)));
            }
            return a5.a();
        }
    }
    

    相关文章

      网友评论

        本文标题:OkHttp使用gzip

        本文链接:https://www.haomeiwen.com/subject/fluzgltx.html