Skip to content

Commit

Permalink
fix missing Content-Type header in response.
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara committed Oct 8, 2015
1 parent 4c7df00 commit 158879e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
25 changes: 1 addition & 24 deletions dashboard.go
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bytes"
"compress/gzip"
"encoding/json"
"flag"
"fmt"
Expand Down Expand Up @@ -88,29 +87,6 @@ type Node struct {
Address string
}

type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}

func (w gzipResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}

func makeGzipHandler(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
fn(w, r)
return
}
w.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w)
defer gz.Close()
gzr := gzipResponseWriter{Writer: gz, ResponseWriter: w}
fn(gzr, r)
}
}

func main() {
var (
port int
Expand Down Expand Up @@ -170,6 +146,7 @@ func indexPage(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Println(err)
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprint(w, string(data))
}

Expand Down
39 changes: 39 additions & 0 deletions http.go
@@ -0,0 +1,39 @@
package main

import (
"compress/gzip"
"io"
"net/http"
"strings"
)

type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}

func (w gzipResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}

func (w gzipResponseWriter) Header() http.Header {
return w.ResponseWriter.Header()
}

func (w gzipResponseWriter) WriteHeader(code int) {
w.ResponseWriter.WriteHeader(code)
}

func makeGzipHandler(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
fn(w, r)
return
}
w.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w)
defer gz.Close()
gzr := gzipResponseWriter{Writer: gz, ResponseWriter: w}
fn(gzr, r)
}
}

0 comments on commit 158879e

Please sign in to comment.