zoukankan      html  css  js  c++  java
  • compress.go

    // 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)
        })
    }

  • 相关阅读:
    从清月高中物理动学课件制作工具说【FarseerPhysics引擎之WheelJoint】及【PropetryGrid之动态下拉列表】
    一种从纹理图片提取多边形的方法
    蒸汽世界满手尘土生命、水、光照锁定修改器
    【五子棋AI循序渐进】——整合完成
    洛谷-07敦刻尔克大撤退-[再也不坑]【二战2】二战系列2:狼烟四起
    洛谷-火柴棒等式-NOIP2008提高组复赛
    洛谷-笨小猴-NOIP2008提高组复赛
    NOIP2013-普及组复赛-第一题-计数问题
    NOIP2010-普及组复赛-第四题-三国游戏
    NOIP2012-普及组复赛-第二题-寻宝
  • 原文地址:https://www.cnblogs.com/zhangboyu/p/7457496.html
Copyright © 2011-2022 走看看