博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
compress.go
阅读量:5116 次
发布时间:2019-06-13

本文共 2616 字,大约阅读时间需要 8 分钟。

// Copyright 2013 The Gorilla Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
 
// copied from https://github.com/gorilla/handlers/blob/master/compress.go
 
package http_api
 
import (
    "compress/flate"
    "compress/gzip"
    "io"
    "net/http"
    "strings"
)
 
type compressResponseWriter struct {
    io.Writer
    http.ResponseWriter
    http.Hijacker
}
 
func (w *compressResponseWriter) Header() http.Header {
    return w.ResponseWriter.Header()
}
 
func (w *compressResponseWriter) WriteHeader(c int) {
    w.ResponseWriter.Header().Del("Content-Length")
    w.ResponseWriter.WriteHeader(c)
}
 
func (w *compressResponseWriter) Write(b []byte) (int, error) {
    h := w.ResponseWriter.Header()
    if h.Get("Content-Type") == "" {
        h.Set("Content-Type", http.DetectContentType(b))
    }
    h.Del("Content-Length")
    return w.Writer.Write(b)
}
 
// CompressHandler gzip compresses HTTP responses for clients that support it
// via the 'Accept-Encoding' header.
func CompressHandler(h http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    L:
        for _, enc := range strings.Split(r.Header.Get("Accept-Encoding"), ",") {
            switch strings.TrimSpace(enc) {
            case "gzip":
                w.Header().Set("Content-Encoding", "gzip")
                w.Header().Add("Vary", "Accept-Encoding")
 
                gw := gzip.NewWriter(w)
                defer gw.Close()
 
                h, hok := w.(http.Hijacker)
                if !hok {
/* w is not Hijacker... oh well... */
                    h = nil
                }
 
                w = &compressResponseWriter{
                    Writer:         gw,
                    ResponseWriter: w,
                    Hijacker:       h,
                }
 
                break L
            case "deflate":
                w.Header().Set("Content-Encoding", "deflate")
                w.Header().Add("Vary", "Accept-Encoding")
 
                fw, _ := flate.NewWriter(w, flate.DefaultCompression)
                defer fw.Close()
 
                h, hok := w.(http.Hijacker)
                if !hok {
/* w is not Hijacker... oh well... */
                    h = nil
                }
 
                w = &compressResponseWriter{
                    Writer:         fw,
                    ResponseWriter: w,
                    Hijacker:       h,
                }
 
                break L
            }
        }
 
        h.ServeHTTP(w, r)
    })
}
 

转载于:https://www.cnblogs.com/zhangboyu/p/7457496.html

你可能感兴趣的文章
Android设计模式系列--原型模式
查看>>
免费的论文查重网站
查看>>
C语言程序第一次作业
查看>>
leetcode-Sort List
查看>>
中文词频统计
查看>>
了解node.js
查看>>
想做移动开发,先看看别人怎么做
查看>>
Eclipse相关集锦
查看>>
虚拟化架构中小型机构通用虚拟化架构
查看>>
继承条款effecitve c++ 条款41-45
查看>>
HTML+CSS学习笔记(九)
查看>>
Java泛型的基本使用
查看>>
1076 Wifi密码 (15 分)
查看>>
rsync
查看>>
noip模拟赛 党
查看>>
bzoj2038 [2009国家集训队]小Z的袜子(hose)
查看>>
Java反射机制及其Class类浅析
查看>>
Postman-----如何导入和导出
查看>>
移动设备显示尺寸大全 CSS3媒体查询
查看>>
图片等比例缩放及图片上下剧中
查看>>