compress.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package main
  2. import (
  3. "bufio"
  4. "compress/flate"
  5. "compress/gzip"
  6. "errors"
  7. "io"
  8. "io/ioutil"
  9. "net"
  10. "net/http"
  11. "strings"
  12. )
  13. type writeResetter interface {
  14. io.Writer
  15. Reset(io.Writer)
  16. }
  17. type compressResponseWriter struct {
  18. writeResetter
  19. http.ResponseWriter
  20. }
  21. func (w *compressResponseWriter) WriteHeader(c int) {
  22. w.ResponseWriter.Header().Del("Content-Length")
  23. w.ResponseWriter.WriteHeader(c)
  24. }
  25. func (w *compressResponseWriter) Header() http.Header {
  26. return w.ResponseWriter.Header()
  27. }
  28. func (w *compressResponseWriter) Write(b []byte) (int, error) {
  29. h := w.ResponseWriter.Header()
  30. if h.Get("Content-Type") == "" {
  31. h.Set("Content-Type", http.DetectContentType(b))
  32. }
  33. h.Del("Content-Length")
  34. return w.writeResetter.Write(b)
  35. }
  36. func (w *compressResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
  37. h, ok := w.ResponseWriter.(http.Hijacker)
  38. if !ok {
  39. return nil, nil, errors.New("compress response does not implement http.Hijacker")
  40. }
  41. conn, bufrw, err := h.Hijack()
  42. if err == nil {
  43. w.writeResetter.Reset(ioutil.Discard)
  44. }
  45. return conn, bufrw, err
  46. }
  47. func CompressHandler(h http.Handler) http.Handler {
  48. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  49. L:
  50. for _, enc := range strings.Split(r.Header.Get("Accept-Encoding"), ",") {
  51. switch strings.TrimSpace(enc) {
  52. case "gzip":
  53. w.Header().Set("Content-Encoding", "gzip")
  54. w.Header().Add("Vary", "Accept-Encoding")
  55. gw := gzip.NewWriter(w)
  56. defer gw.Close()
  57. w = &compressResponseWriter{
  58. writeResetter: gw,
  59. ResponseWriter: w,
  60. }
  61. break L
  62. case "deflate":
  63. w.Header().Set("Content-Encoding", "deflate")
  64. w.Header().Add("Vary", "Accept-Encoding")
  65. fw, _ := flate.NewWriter(w, flate.DefaultCompression)
  66. defer fw.Close()
  67. w = &compressResponseWriter{
  68. writeResetter: fw,
  69. ResponseWriter: w,
  70. }
  71. break L
  72. }
  73. }
  74. h.ServeHTTP(w, r)
  75. })
  76. }