日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

一. HTTP gzip壓縮,概述

  • request
    • header中聲明Accept-Encoding : gzip,告知服務器客戶端接受gzip的數據
  • response
    • body,同時加入以下header:Content-Encoding: gzip:表明body是gzip過的數據
    • Content-Length:117:表示body gzip壓縮后的數據大小,便于客戶端使用
    • 或Transfer-Encoding: chunked:分塊傳輸編碼

二. 如何使用gzip進行壓縮

Tomcat開啟壓縮(gzip)

tomcat server.xml

<Connector
		compression="on" # 表示開啟壓縮
		noCompressionUserAgents="gozilla, traviata"
		compressionMinSize="2048" # 表示會對大于2KB的文件進行壓縮
		compressableMimeType="text/html,text/xml,text/css,text/JAVAscript,image/gif,image/jpg" # 是指將進行壓縮的文件類型
/>
  • 弊端
    對HTTP傳輸內容進行壓縮是改良前端響應性能的可用方法之一,大型網站都在用。但是也有缺點,就是壓縮過程占用cpu的資源,客戶端瀏覽器解析也占據了一部分時間。但是隨著硬件性能不斷的提高,這些問題正在不斷的弱化。

程序壓縮/解壓

GZIPInputStream(解壓) / GZIPOutputStream(壓縮)

  • netflix.zuul相關示例
# org.springframework.cloud.netflix.zuul.filters.post.SendResponseFilter#writeResponse()
is = context.getResponseDataStream();
InputStream inputStream = is;
if (is != null) {
		if (context.sendZuulResponse()) {
		// if origin response is gzipped, and client has not requested gzip,
		// decompress stream
		// before sending to client
		// else, stream gzip directly to client
				if (context.getResponseGZipped() && !isGzipRequested) {
						// If origin tell it's GZipped but the content is ZERO bytes,
						// don't try to uncompress
						final Long len = context.getOriginContentLength();
						if (len == null || len > 0) {
								try {
												inputStream = new GZIPInputStream(is);
								}catch (java.util.zip.ZipException ex) {
										log.debug("gzip expected but not "+ "received assuming unencoded response "+ RequestContext.getCurrentContext()
												.getRequest().getRequestURL()
												.toString());
										inputStream = is;
								}
					}else {
								// Already done : inputStream = is;
				}
		}else if (context.getResponseGZipped() && isGzipRequested) {
				servletResponse.setHeader(ZuulHeaders.CONTENT_ENCODING, "gzip");
		}
		writeResponse(inputStream, outStream);
		}
}

# com.netflix.zuul.http.HttpServletRequestWrApper.UnitTest#handlesGzipRequestBody
@Test
public void handlesGzipRequestBody() throws IOException {
		// creates string, gzips into byte array which will be mocked as InputStream of request
		final String body = "hello";
		final byte[] bodyBytes = body.getBytes();
		// in this case the compressed stream is actually larger - need to allocate enough space
		final ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream(0);
		final GZIPOutputStream gzipOutStream = new GZIPOutputStream(byteOutStream);
		gzipOutStream.write(bodyBytes);
		gzipOutStream.finish();
		gzipOutStream.flush();
		body(byteOutStream.toByteArray());

		final HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper(request);
		assertEquals(body, IOUtils.toString(new GZIPInputStream(wrapper.getInputStream())));
}

示例: 網關主動對response進行壓縮響應(可減少帶寬) GZIPOutputStream

  • 簡單實現示例.實際情況需考慮更新情況,如是否已經被壓縮等
InputStream inputStream = okResponse.body().byteStream();
try {
// 網關主動對response進行壓縮響應(可減少帶寬)
		HttpServletRequest request = RequestContext.getCurrentContext().getRequest();
		boolean isGatewayGZIP = Boolean.parseBoolean(request.getHeader("x-gateway-gzip"));
		if (!isGatewayGZIP) {
				isGatewayGZIP = Boolean.parseBoolean(request.getParameter("x-gateway-gzip"));
		}

if (isGatewayGZIP) {
		final ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream(0);
		final GZIPOutputStream gzipOutStream = new GZIPOutputStream(byteOutStream);
		gzipOutStream.write(okResponse.body().bytes());
		gzipOutStream.finish();
		gzipOutStream.flush();
		inputStream = new ServletInputStreamWrapper(byteOutStream.toByteArray());
		httpHeaders.add(ZuulHeaders.CONTENT_ENCODING, "gzip");
}
} catch (Exception e) {
		logger.error("GatewayGZIP error:", e);
}

三.okhttp 壓縮相關處理

okHttp 解壓gzip,條件: Content-Encoding = gizp

  • okio.GzipSource
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)));
}

okhttp gzip壓縮/解壓 (示例)

//zip壓縮
GzipSink gzipSink = new GzipSink(Okio.sink(file));
BufferedSink bufferedSink = Okio.buffer(gzipSink);
bufferedSink.writeUtf8("this is zip file");
bufferedSink.flush();
bufferedSink.close();

//讀取zip
GzipSource gzipSource = new GzipSource(Okio.source(file));
BufferedSource bufferedSource = Okio.buffer(gzipSource);
String s = bufferedSource.readUtf8();

okhttp框架-如何對請求(request)數據進行GZIP壓縮-GzipRequestInterceptor

OkHttpClient okHttpClient = new OkHttpClient.Builder() 
.addInterceptor(new GzipRequestInterceptor())//開啟Gzip壓縮
...
.build();
GzipRequestInterceptor
https://github.com/square/okhttp\issues/350#issuecomment-123105641
class GzipRequestInterceptor implements Interceptor {
@Override 
public Response intercept(Chain chain) throws IOException {
	Request originalRequest = chain.request();
	if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) {
			return chain.proceed(originalRequest);
}

Request compressedRequest = originalRequest.newBuilder()
	.header("Content-Encoding", "gzip")
	.method(originalRequest.method(), forceContentLength(gzip(originalRequest.body())))
	.build();
	return chain.proceed(compressedRequest);
}

/** https://github.com/square/okhttp\issues/350 */
private RequestBody forceContentLength(final RequestBody requestBody) throws IOException {
      final Buffer buffer = new Buffer();
      requestBody.writeTo(buffer);
      return new RequestBody() {
        @Override
        public MediaType contentType() {
              return requestBody.contentType();
        }

        @Override
        public long contentLength() {
              return buffer.size();
        }

      @Override
      public void writeTo(BufferedSink sink) throws IOException {
            sink.write(buffer.snapshot());
      }
};
}

private RequestBody gzip(final RequestBody body) {
return new RequestBody() {
      @Override 
        public MediaType contentType() {
            return body.contentType();
      }

      @Override 
       public long contentLength() {
            return -1; // We don't know the compressed length in advance!
      }

      @Override 
       public void writeTo(BufferedSink sink) throws IOException {
            BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
            body.writeTo(gzipSink);
            gzipSink.close();
      }
};
}
}

okhttp框架-如何對請求數據進行GZIP壓縮

https://cloud.tencent.com/info/61307ab74137a46628c2ea2ca42a6eb4.html

Okhttp3請求網絡開啟Gzip壓縮 - CSDN博客

https://blog.csdn.net/aiynmimi/article/details/77453809

四. Nginx的Gzip可以對服務器端響應內容進行壓縮從而減少一定的客戶端響應時間

gzip on;
gzip_min_length 1k;
gzip_buffers 4 32k;
gzip_types text/plain application/x-JavaScript application/javascript text/xml text/css;
gzip_vary on;

API網關那些兒 | I'm Yunlong

http://ylzheng.com/2017/03/14/the-things-about-api-gateway

source: //liuxiang.github.io/2018/08/13/HTTP%20gzip壓縮

分享到:
標簽:壓縮 gzip
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定