Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EnableGZIP: don't defer close to avoid the gzip writer to Write(nil) which results in writing a 200 header #6

Merged
merged 7 commits into from
Sep 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: go
go:
- 1.6.2
- 1.7.1
- 1.7

script:
- go test -v ./...
2 changes: 1 addition & 1 deletion http_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ func EnableGZIP(fn httprouter.Handle) httprouter.Handle {
}
w.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w)
defer gz.Close()
gzr := gzipResponseWriter{Writer: gz, ResponseWriter: w}
fn(gzr, r, p)
gz.Close()
}
}
30 changes: 16 additions & 14 deletions params.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package parameters

import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"io"
Expand All @@ -22,12 +23,15 @@ import (
"strings"
"time"

"github.com/gorilla/context"
"github.com/gorilla/mux"
"github.com/julienschmidt/httprouter"
"github.com/ugorji/go/codec"
)

const (
paramsKey = "params"
)

type Params struct {
isBinary bool
Values map[string]interface{}
Expand Down Expand Up @@ -464,17 +468,16 @@ func (p *Params) GetJSON(key string) map[string]interface{} {
}

func MakeParsedReq(fn http.HandlerFunc) http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {
ParseParams(req)
fn(rw, req)
context.Clear(req)
return func(rw http.ResponseWriter, r *http.Request) {
r = r.WithContext(context.WithValue(r.Context(), paramsKey, ParseParams(r)))
fn(rw, r)
}
}

func MakeHTTPRouterParsedReq(fn httprouter.Handle) httprouter.Handle {
return func(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
ParseParams(req)
params := GetParams(req)
return func(rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
r = r.WithContext(context.WithValue(r.Context(), paramsKey, ParseParams(r)))
params := GetParams(r)
for _, param := range p {
const ID = "id"
if strings.Contains(param.Key, ID) {
Expand All @@ -488,14 +491,13 @@ func MakeHTTPRouterParsedReq(fn httprouter.Handle) httprouter.Handle {
params.Values[param.Key] = param.Value
}
}
fn(rw, req, p)
context.Clear(req)
fn(rw, r, p)
}
}

func GetParams(req *http.Request) *Params {
params := context.Get(req, "params").(Params)
return &params
params := req.Context().Value(paramsKey).(*Params)
return params
}

type CustomTypeHandler func(field *reflect.Value, value interface{})
Expand Down Expand Up @@ -618,7 +620,7 @@ func contains(haystack []string, needle string) bool {
return false
}

func ParseParams(req *http.Request) {
func ParseParams(req *http.Request) *Params {
var p Params
ct := req.Header.Get("Content-Type")
ct = strings.Split(ct, ";")[0]
Expand Down Expand Up @@ -715,5 +717,5 @@ func ParseParams(req *http.Request) {
}
}

context.Set(req, "params", p)
return &p
}
26 changes: 13 additions & 13 deletions params_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parameters

import (
"context"
"net/http"
"strings"
"testing"
Expand All @@ -17,7 +18,7 @@ func TestParseJSONBody(t *testing.T) {
}
r.Header.Set("Content-Type", "application/json")

ParseParams(r)
r = r.WithContext(context.WithValue(r.Context(), paramsKey, ParseParams(r)))

params := GetParams(r)

Expand All @@ -38,7 +39,7 @@ func TestParseJSONBodyContentType(t *testing.T) {
}
r.Header.Set("Content-Type", "application/json; charset=utf8")

ParseParams(r)
r = r.WithContext(context.WithValue(r.Context(), paramsKey, ParseParams(r)))

params := GetParams(r)

Expand All @@ -59,7 +60,7 @@ func TestParseNestedJSONBody(t *testing.T) {
}
r.Header.Set("Content-Type", "application/json")

ParseParams(r)
r = r.WithContext(context.WithValue(r.Context(), paramsKey, ParseParams(r)))

params := GetParams(r)

Expand Down Expand Up @@ -118,7 +119,7 @@ func TestParseGET(t *testing.T) {
t.Fatal("Could not build request", err)
}

ParseParams(r)
r = r.WithContext(context.WithValue(r.Context(), paramsKey, ParseParams(r)))

params := GetParams(r)

Expand All @@ -139,7 +140,7 @@ func TestParsePOST(t *testing.T) {
}
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")

ParseParams(r)
r = r.WithContext(context.WithValue(r.Context(), paramsKey, ParseParams(r)))

params := GetParams(r)

Expand All @@ -160,7 +161,7 @@ func TestParsePUT(t *testing.T) {
}
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")

ParseParams(r)
r = r.WithContext(context.WithValue(r.Context(), paramsKey, ParseParams(r)))

params := GetParams(r)

Expand All @@ -181,7 +182,7 @@ func TestParsePostUrlJSON(t *testing.T) {
}
r.Header.Set("Content-Type", "application/json")

ParseParams(r)
r = r.WithContext(context.WithValue(r.Context(), paramsKey, ParseParams(r)))

params := GetParams(r)

Expand Down Expand Up @@ -212,7 +213,7 @@ func TestParseJSONBodyMux(t *testing.T) {
m := mux.NewRouter()
m.KeepContext = true
m.HandleFunc("/test/{id:[0-9]+}", func(w http.ResponseWriter, r *http.Request) {
ParseParams(r)
r = r.WithContext(context.WithValue(r.Context(), paramsKey, ParseParams(r)))

params := GetParams(r)

Expand All @@ -238,7 +239,6 @@ func TestParseJSONBodyMux(t *testing.T) {
t.Error("Mux did not match")
}
m.ServeHTTP(nil, r)

}

func TestImbue(t *testing.T) {
Expand All @@ -249,7 +249,7 @@ func TestImbue(t *testing.T) {
}
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")

ParseParams(r)
r = r.WithContext(context.WithValue(r.Context(), paramsKey, ParseParams(r)))

params := GetParams(r)

Expand Down Expand Up @@ -288,7 +288,7 @@ func TestImbueTime(t *testing.T) {
}
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")

ParseParams(r)
r = r.WithContext(context.WithValue(r.Context(), paramsKey, ParseParams(r)))

params := GetParams(r)

Expand Down Expand Up @@ -322,7 +322,7 @@ func TestHasAll(t *testing.T) {
}
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")

ParseParams(r)
r = r.WithContext(context.WithValue(r.Context(), paramsKey, ParseParams(r)))

params := GetParams(r)
//Test All
Expand Down Expand Up @@ -358,7 +358,7 @@ func TestParseEmpty(t *testing.T) {
}
r.Header.Set("Content-Type", "application/json")

ParseParams(r)
r = r.WithContext(context.WithValue(r.Context(), paramsKey, ParseParams(r)))

params := GetParams(r)

Expand Down