-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1598 from containous/fix-stats-hijack
Fix stats hijack
- Loading branch information
Showing
6 changed files
with
115 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package middlewares | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/codegangsta/negroni" | ||
"github.com/containous/traefik/log" | ||
) | ||
|
||
// RecoverHandler recovers from a panic in http handlers | ||
func RecoverHandler(next http.Handler) http.Handler { | ||
fn := func(w http.ResponseWriter, r *http.Request) { | ||
defer recoverFunc(w) | ||
next.ServeHTTP(w, r) | ||
} | ||
return http.HandlerFunc(fn) | ||
} | ||
|
||
// NegroniRecoverHandler recovers from a panic in negroni handlers | ||
func NegroniRecoverHandler() negroni.Handler { | ||
fn := func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { | ||
defer recoverFunc(w) | ||
next.ServeHTTP(w, r) | ||
} | ||
return negroni.HandlerFunc(fn) | ||
} | ||
|
||
func recoverFunc(w http.ResponseWriter) { | ||
if err := recover(); err != nil { | ||
log.Errorf("Recovered from panic in http handler: %+v", err) | ||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package middlewares | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/codegangsta/negroni" | ||
) | ||
|
||
func TestRecoverHandler(t *testing.T) { | ||
fn := func(w http.ResponseWriter, r *http.Request) { | ||
panic("I love panicing!") | ||
} | ||
recoverHandler := RecoverHandler(http.HandlerFunc(fn)) | ||
server := httptest.NewServer(recoverHandler) | ||
defer server.Close() | ||
|
||
resp, err := http.Get(server.URL) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if resp.StatusCode != http.StatusInternalServerError { | ||
t.Fatalf("Received non-%d response: %d\n", http.StatusInternalServerError, resp.StatusCode) | ||
} | ||
} | ||
|
||
func TestNegroniRecoverHandler(t *testing.T) { | ||
n := negroni.New() | ||
n.Use(NegroniRecoverHandler()) | ||
panicHandler := func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { | ||
panic("I love panicing!") | ||
} | ||
n.UseFunc(negroni.HandlerFunc(panicHandler)) | ||
server := httptest.NewServer(n) | ||
defer server.Close() | ||
|
||
resp, err := http.Get(server.URL) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if resp.StatusCode != http.StatusInternalServerError { | ||
t.Fatalf("Received non-%d response: %d\n", http.StatusInternalServerError, resp.StatusCode) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package middlewares | ||
|
||
import "net/http" | ||
|
||
// Stateful interface groups all http interfaces that must be | ||
// implemented by a stateful middleware (ie: recorders) | ||
type Stateful interface { | ||
http.ResponseWriter | ||
http.Hijacker | ||
http.Flusher | ||
http.CloseNotifier | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters