-
-
Notifications
You must be signed in to change notification settings - Fork 980
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 #128 from pressly/http2
Http2 support for middleware, fixes #123
- Loading branch information
Showing
11 changed files
with
271 additions
and
35 deletions.
There are no files selected for viewing
File renamed without changes.
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,15 @@ | ||
// +build go1.8 | ||
|
||
package middleware | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
) | ||
|
||
func (w *maybeCompressResponseWriter) Push(target string, opts *http.PushOptions) error { | ||
if ps, ok := w.w.(http.Pusher); ok { | ||
return ps.Push(target, opts) | ||
} | ||
return errors.New("chi/middleware: http.Pusher is unavailable on the writer") | ||
} |
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,77 @@ | ||
// +build go1.8 | ||
|
||
package middleware | ||
|
||
import ( | ||
"crypto/tls" | ||
"io" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"golang.org/x/net/http2" | ||
) | ||
|
||
// NOTE: we must import `golang.org/x/net/http2` in order to explicitly enable | ||
// http2 transports for certain tests. The runtime pkg does not have this dependency | ||
// though as the transport configuration happens under the hood on go 1.7+. | ||
|
||
func TestWrapWriterHTTP2(t *testing.T) { | ||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
_, cn := w.(http.CloseNotifier) | ||
if !cn { | ||
t.Fatal("request should have been a http.CloseNotifier") | ||
} | ||
_, fl := w.(http.Flusher) | ||
if !fl { | ||
t.Fatal("request should have been a http.Flusher") | ||
} | ||
_, hj := w.(http.Hijacker) | ||
if hj { | ||
t.Fatal("request should not have been a http.Hijacker") | ||
} | ||
_, rf := w.(io.ReaderFrom) | ||
if rf { | ||
t.Fatal("request should not have been a io.ReaderFrom") | ||
} | ||
_, ps := w.(http.Pusher) | ||
if !ps { | ||
t.Fatal("request should have been a http.Pusher") | ||
} | ||
|
||
w.Write([]byte("OK")) | ||
}) | ||
|
||
wmw := func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
next.ServeHTTP(NewWrapResponseWriter(w, r.ProtoMajor), r) | ||
}) | ||
} | ||
|
||
server := http.Server{ | ||
Addr: ":7072", | ||
Handler: wmw(handler), | ||
} | ||
// By serving over TLS, we get HTTP2 requests | ||
go server.ListenAndServeTLS(testdataDir+"/cert.pem", testdataDir+"/key.pem") | ||
defer server.Close() | ||
// We need the server to start before making the request | ||
time.Sleep(100 * time.Millisecond) | ||
|
||
client := &http.Client{ | ||
Transport: &http2.Transport{ | ||
TLSClientConfig: &tls.Config{ | ||
// The certificates we are using are self signed | ||
InsecureSkipVerify: true, | ||
}, | ||
}, | ||
} | ||
|
||
resp, err := client.Get("https://localhost:7072") | ||
if err != nil { | ||
t.Fatalf("could not get server: %v", err) | ||
} | ||
if resp.StatusCode != 200 { | ||
t.Fatalf("non 200 response: %v", 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
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,34 @@ | ||
// +build go1.7,!go1.8 | ||
|
||
package middleware | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
) | ||
|
||
// NewWrapResponseWriter wraps an http.ResponseWriter, returning a proxy that allows you to | ||
// hook into various parts of the response process. | ||
func NewWrapResponseWriter(w http.ResponseWriter, protoMajor int) WrapResponseWriter { | ||
_, cn := w.(http.CloseNotifier) | ||
_, fl := w.(http.Flusher) | ||
|
||
bw := basicWriter{ResponseWriter: w} | ||
|
||
if protoMajor == 2 { | ||
if cn && fl { | ||
return &http2FancyWriter{bw} | ||
} | ||
} else { | ||
_, hj := w.(http.Hijacker) | ||
_, rf := w.(io.ReaderFrom) | ||
if cn && fl && hj && rf { | ||
return &httpFancyWriter{bw} | ||
} | ||
} | ||
if fl { | ||
return &flushWriter{bw} | ||
} | ||
|
||
return &bw | ||
} |
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,41 @@ | ||
// +build go1.8 | ||
|
||
package middleware | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
) | ||
|
||
// NewWrapResponseWriter wraps an http.ResponseWriter, returning a proxy that allows you to | ||
// hook into various parts of the response process. | ||
func NewWrapResponseWriter(w http.ResponseWriter, protoMajor int) WrapResponseWriter { | ||
_, cn := w.(http.CloseNotifier) | ||
_, fl := w.(http.Flusher) | ||
|
||
bw := basicWriter{ResponseWriter: w} | ||
|
||
if protoMajor == 2 { | ||
_, ps := w.(http.Pusher) | ||
if cn && fl && ps { | ||
return &http2FancyWriter{bw} | ||
} | ||
} else { | ||
_, hj := w.(http.Hijacker) | ||
_, rf := w.(io.ReaderFrom) | ||
if cn && fl && hj && rf { | ||
return &httpFancyWriter{bw} | ||
} | ||
} | ||
if fl { | ||
return &flushWriter{bw} | ||
} | ||
|
||
return &bw | ||
} | ||
|
||
func (f *http2FancyWriter) Push(target string, opts *http.PushOptions) error { | ||
return f.basicWriter.ResponseWriter.(http.Pusher).Push(target, opts) | ||
} | ||
|
||
var _ http.Pusher = &http2FancyWriter{} |
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,19 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIC/zCCAeegAwIBAgIRANioW0Re7DtpT4qZpJU1iK8wDQYJKoZIhvcNAQELBQAw | ||
EjEQMA4GA1UEChMHQWNtZSBDbzAeFw0xNjEyMzExNDU0MzBaFw0xNzEyMzExNDU0 | ||
MzBaMBIxEDAOBgNVBAoTB0FjbWUgQ28wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAw | ||
ggEKAoIBAQDpFfOsaXDYlL+ektfsqGYrSAsoTbe7zqjpow9nqUU4PmLRu2YMaaW8 | ||
fAoneUnJxsJw7ql38+VMpphZUOmOWvsO7uV/lfnTIQfTwllHDdgAR5A11d84Zy/y | ||
TiNIFJduuaPtEhQs1dxPhU7TG8sEfFRhBoUDPv473akeGPNkVU756RVBYM6rUc3b | ||
YygD0PXGsQ2obrImbYUyyHH5YClCvGl1No57n3ugLqSSfwbgR3/Gw7kkGKy0PMOu | ||
TuHuJnTEmofJPkqEyFRVMlIAtfqFqJUfDHTOuQGWIUPnjDg+fqTI9EPJ+pElBqDQ | ||
IqW93BY5XePMdrTQc1h6xkduDfuLeA7TAgMBAAGjUDBOMA4GA1UdDwEB/wQEAwIF | ||
oDATBgNVHSUEDDAKBggrBgEFBQcDATAMBgNVHRMBAf8EAjAAMBkGA1UdEQQSMBCC | ||
DmxvY2FsaG9zdDo3MDcyMA0GCSqGSIb3DQEBCwUAA4IBAQDnsWmZdf7209A/XHUe | ||
xoONCbU8jaYFVoA+CN9J+3CASzrzTQ4fh9RJdm2FZuv4sWnb5c5hDN7H/M/nLcb0 | ||
+uu7ACBGhd7yACYCQm/z3Pm3CY2BRIo0vCCRioGx+6J3CPGWFm0vHwNBge0iBOKC | ||
Wn+/YOlTDth/M3auHYlr7hdFmf57U4V/5iTr4wiKxwM9yMPcVRQF/1XpPd7A0VqM | ||
nFSEfDpFjrA7MvT3DrRqQGqF/ZXxDbro2nyki3YG8FwgKlFNVN9w55zNiriQ+WNA | ||
uz86lKg1FTc+m/R/0CD//7+7mme28N813EPVdV83TgxWNrfvAIRazkHE7YxETry0 | ||
BJDg | ||
-----END CERTIFICATE----- |
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,27 @@ | ||
-----BEGIN RSA PRIVATE KEY----- | ||
MIIEpAIBAAKCAQEA6RXzrGlw2JS/npLX7KhmK0gLKE23u86o6aMPZ6lFOD5i0btm | ||
DGmlvHwKJ3lJycbCcO6pd/PlTKaYWVDpjlr7Du7lf5X50yEH08JZRw3YAEeQNdXf | ||
OGcv8k4jSBSXbrmj7RIULNXcT4VO0xvLBHxUYQaFAz7+O92pHhjzZFVO+ekVQWDO | ||
q1HN22MoA9D1xrENqG6yJm2FMshx+WApQrxpdTaOe597oC6kkn8G4Ed/xsO5JBis | ||
tDzDrk7h7iZ0xJqHyT5KhMhUVTJSALX6haiVHwx0zrkBliFD54w4Pn6kyPRDyfqR | ||
JQag0CKlvdwWOV3jzHa00HNYesZHbg37i3gO0wIDAQABAoIBAFvqYDE5U1rVLctm | ||
tOeKcN/YhS3bl/zjvhCEUOrcAYPwdh+m+tMiRk1RzN9MISEE1GCcfQ/kiiPz/lga | ||
ZD/S+PYmlzH8/ouXlvKWzYYLm4ZgsinIsUIYzvuKfLdMB3uOkWpHmtUjcMGbHD57 | ||
009tiAjK/WEOUkthWfOYe0KxsXczBn3PTAWZuiIkuA3RVWa7pCCFHUENk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