From 836e6412292c4b0f296bde42368dbcc83779a78e Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Thu, 25 Jun 2020 13:04:36 +0300 Subject: [PATCH] minor Former-commit-id: 0bc9c92c519edda9e04be8481e16fd2fdcfc74c0 --- _examples/README.md | 2 + _examples/response-writer/http2push/main.go | 40 ++++++++++++++ .../response-writer/http2push/mycert.crt | 31 +++++++++++ _examples/response-writer/http2push/mykey.key | 52 +++++++++++++++++++ .../response-writer/http2push/public/main.js | 1 + _examples/response-writer/write-rest/main.go | 4 ++ configuration.go | 6 ++- context/context.go | 2 +- context/response_writer.go | 6 ++- iris.go | 10 ++++ 10 files changed, 151 insertions(+), 3 deletions(-) create mode 100644 _examples/response-writer/http2push/main.go create mode 100644 _examples/response-writer/http2push/mycert.crt create mode 100644 _examples/response-writer/http2push/mykey.key create mode 100644 _examples/response-writer/http2push/public/main.js diff --git a/_examples/README.md b/_examples/README.md index 70710e7b..e4cf7d7b 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -132,6 +132,7 @@ * [Content Negotiation](response-writer/content-negotiation) * [Text, Markdown, YAML, HTML, JSON, JSONP, Msgpack, XML and Binary](response-writer/write-rest/main.go) * [Write Gzip](response-writer/write-gzip/main.go) + * [HTTP/2 Server Push](response-writer/http2push/main.go) * [Stream Writer](response-writer/stream-writer/main.go) * [Transactions](response-writer/transactions/main.go) * [SSE](response-writer/sse/main.go) @@ -181,6 +182,7 @@ * [Smart Contract](dependency-injection/smart-contract/main.go) * [JWT](dependency-injection/jwt/main.go) * [JWT (iris-contrib)](dependency-injection/jwt/contrib/main.go) + * [Register Dependency from Context](dependency-injection/context-register-dependency/main.go) * MVC * [Overview](mvc/overview) * [Repository and Service layers](mvc/repository) diff --git a/_examples/response-writer/http2push/main.go b/_examples/response-writer/http2push/main.go new file mode 100644 index 00000000..d7fce749 --- /dev/null +++ b/_examples/response-writer/http2push/main.go @@ -0,0 +1,40 @@ +// Server push lets the server preemptively "push" website assets +// to the client without the user having explicitly asked for them. +// When used with care, we can send what we know the user is going +// to need for the page they’re requesting. +package main + +import "github.com/kataras/iris/v12" + +func main() { + app := iris.New() + app.Get("/", pushHandler) + app.Get("/main.js", simpleAssetHandler) + + app.Run(iris.TLS("127.0.0.1:443", "mycert.crt", "mykey.key")) + // $ openssl req -new -newkey rsa:4096 -x509 -sha256 \ + // -days 365 -nodes -out mycert.crt -keyout mykey.key +} + +func pushHandler(ctx iris.Context) { + // The target must either be an absolute path (like "/path") or an absolute + // URL that contains a valid host and the same scheme as the parent request. + // If the target is a path, it will inherit the scheme and host of the + // parent request. + target := "/main.js" + err := ctx.ResponseWriter().Push(target, nil) + if err != nil { + if err == iris.ErrPushNotSupported { + ctx.StopWithText(iris.StatusHTTPVersionNotSupported, "HTTP/2 push not supported.") + } else { + ctx.StopWithError(iris.StatusInternalServerError, err) + } + return + } + + ctx.HTML(``, target) +} + +func simpleAssetHandler(ctx iris.Context) { + ctx.ServeFile("./public/main.js") +} diff --git a/_examples/response-writer/http2push/mycert.crt b/_examples/response-writer/http2push/mycert.crt new file mode 100644 index 00000000..9db93b09 --- /dev/null +++ b/_examples/response-writer/http2push/mycert.crt @@ -0,0 +1,31 @@ +-----BEGIN CERTIFICATE----- +MIIFazCCA1OgAwIBAgIUfwMd9auWixp19UnXOmyxJ9Jkv7IwDQYJKoZIhvcNAQEL +BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM +GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yMDA2MjUwOTUxNDdaFw0yMTA2 +MjUwOTUxNDdaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw +HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwggIiMA0GCSqGSIb3DQEB +AQUAA4ICDwAwggIKAoICAQDlVGyGAQ9uyfNbwZyrtYOSjLpxf5NpNToh2OzU7gy2 +OexBji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fw+4AhVh5ZDz +Bl9rGPjwEs5zwutYvVAJzs7AVcighYP1lHKoJ7DxBDQeyBsYlUNk2l6bmZgLgGUZ ++2OyWlqc/jD2GdDsIaZ4i7QqhTI/6aYZIf5zUkblKV1aMSaDulKxRv//OwW28Jax +9EEoV7VaFb3sOkB/tZGhusXeQVtdrhahT3KkZLNwmNXoXWKJ5LjeUlFWJyV6JbDe +y/PIWWCwWqyuFCSZS+Cg3RDgAzfSxkI8uVZ+IKKJS3UluDX45lxXtbRrvTQ+oDrA +6ga5c1Vz9C4kn1K5yW4d7QIvg6vPiy7gvl+//sz9oxUM3yswInDBY0HKLgT0Uq9b +YzLDh2RSaHsgHMPy2BKqR+q2N+lpg7inAWuJM1Huq6eHFqhiyQkzsfscBd1Dpm8= +-----END CERTIFICATE----- diff --git a/_examples/response-writer/http2push/mykey.key b/_examples/response-writer/http2push/mykey.key new file mode 100644 index 00000000..39f7b3af --- /dev/null +++ b/_examples/response-writer/http2push/mykey.key @@ -0,0 +1,52 @@ +-----BEGIN PRIVATE KEY----- +MIIJRAIBADANBgkqhkiG9w0BAQEFAASCCS4wggkqAgEAAoICAQDlVGyGAQ9uyfNb +wZyrtYOSjLpxf5NpNToh2OzU7gy2OexBji5lmWBQ3oYDG+FjAkbHORPzOMNpeMwj +e+IjGZBw8x6E+8WoGdSzbrEZ6pUVwKJGKEuDlx6g6HEmtv3ZwgGe20gvPjjW+oCO +888dwK/mbIHrHTq4nO3o0gAdAJwuamn9BlHU5O4RW7BQ4tLF+j/fBCACWRG1NHXA +0AT8eg544GyCdyteAH11oCDsHS8/DAPsM6t+tZrMCIt9+9dzPdVoOmQNaMMrcz8e +JohddRTK6zHe9ixZTt/soayOF7OSQQeekbr3HPYhD450zRVplLMHx7wnph/+O+Po +6bqDnUzdnkqAAwwymQapHMuHXZKNrhdfKau3rVo1GeXLIRgeWLUoxFSm4TYshrgt ++0AidLRH+dCY7MS9Ngga/sAK3vIDgSF75mFgOhY+q7nvY9Ecao6TnoN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GkJG1YD+x2WW/UW +8K6zQF8GGeQZ8Zlh7axUlIBxGpG43BGrUHpLNqPD7BXWGq6dnhufBYRFay8y34/r +sH3+yuPa92ki7/geQppZwCZRgLSKMRbIdoWaKhZZEQlpGOzCOiRmk9OGyRcoNVRU +X7UYgPqZdc1cMo/AxGWzULJNjMaYMZvIKcHkqOKZfkIcWlSictn7pMPhN1+k+NWM +yMORAQKCAQAyXl02h/c2ihx6cjKlnNeDr2ZfzkoiAvFuKaoAR+KVvb9F9X7ZgKSi +wudZyelTglIVCYXeRmG09uX3rNGCzFrweRwgn6x/8DnN5pMRJVZOXFdgR+V9uKep +K6F7DYbPyggvLOAsezB+09i9lwxM+XdA2whVpL5NFR1rGfFglnE1EQHcEvNONkcv +0h8x9cNSptJyRDLiTIKI9EhonuzwzkGpvjULQE8MLbT8PbjoLFINcE9ZWhwtyw0V +XO32KE8iLKt3KzHz9CfTRCI3M7DwD752AC6zRr8ZS/HXzs+5WTkdVVEtRC7Abd3y +W2TzuSMYNDu876twbTVQJED3mwOAQ3J7 +-----END PRIVATE KEY----- diff --git a/_examples/response-writer/http2push/public/main.js b/_examples/response-writer/http2push/public/main.js new file mode 100644 index 00000000..2aae6c4c --- /dev/null +++ b/_examples/response-writer/http2push/public/main.js @@ -0,0 +1 @@ +window.alert("javascript loaded"); \ No newline at end of file diff --git a/_examples/response-writer/write-rest/main.go b/_examples/response-writer/write-rest/main.go index 01db95b4..fd83b6aa 100644 --- a/_examples/response-writer/write-rest/main.go +++ b/_examples/response-writer/write-rest/main.go @@ -84,6 +84,10 @@ func main() { ctx.YAML(ExampleYAML{Name: "Iris", ServerAddr: "localhost:8080"}) }) + // app.Get("/protobuf", func(ctx iris.Context) { + // ctx.Protobuf(proto.Message) + // }) + app.Get("/msgpack", func(ctx iris.Context) { u := User{ Firstname: "John", diff --git a/configuration.go b/configuration.go index b6176569..0c7654c9 100644 --- a/configuration.go +++ b/configuration.go @@ -626,8 +626,12 @@ func (tc *TunnelingConfiguration) isEnabled() bool { func (tc *TunnelingConfiguration) isNgrokRunning() bool { resp, err := http.Get(tc.WebInterface) + if err != nil { + return false + } + resp.Body.Close() - return err == nil + return true } // https://ngrok.com/docs diff --git a/context/context.go b/context/context.go index 3396a5be..435cc239 100644 --- a/context/context.go +++ b/context/context.go @@ -3624,7 +3624,7 @@ type JSON struct { Indent string Prefix string ASCII bool // if true writes with unicode to ASCII content. - Secure bool // if true then it adds a "while(1);" when Go slice (to JSON Array) value. + Secure bool // if true then it prepends a "while(1);" when Go slice (to JSON Array) value. } // JSONP contains the options for the JSONP (Context's) Renderer. diff --git a/context/response_writer.go b/context/response_writer.go index dbc7d12d..6ed21f4d 100644 --- a/context/response_writer.go +++ b/context/response_writer.go @@ -349,6 +349,10 @@ func (w *responseWriter) WriteTo(to ResponseWriter) { // the body is not copied, this writer doesn't support recording } +// ErrHijackNotSupported is returned by the Hijack method to +// indicate that Hijack feature is not available. +var ErrHijackNotSupported = errors.New("hijack is not supported by this ResponseWriter") + // Hijack lets the caller take over the connection. // After a call to Hijack(), the HTTP server library // will not do anything else with the connection. @@ -366,7 +370,7 @@ func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) { return h.Hijack() } - return nil, nil, errors.New("hijack is not supported by this ResponseWriter") + return nil, nil, ErrHijackNotSupported } // Flusher indicates if `Flush` is supported by the client. diff --git a/iris.go b/iris.go index e8ffa9e9..ed473a76 100644 --- a/iris.go +++ b/iris.go @@ -605,6 +605,16 @@ var ( // ErrStopExecution if returned from a hero middleware or a request-scope dependency // stops the handler's execution, see _examples/dependency-injection/basic/middleware. ErrStopExecution = hero.ErrStopExecution + // ErrHijackNotSupported is returned by the Hijack method to + // indicate that Hijack feature is not available. + // + // A shortcut for the `context#ErrHijackNotSupported`. + ErrHijackNotSupported = context.ErrHijackNotSupported + // ErrPushNotSupported is returned by the Push method to + // indicate that HTTP/2 Push support is not available. + // + // A shortcut for the `context#ErrPushNotSupported`. + ErrPushNotSupported = context.ErrPushNotSupported ) // Constants for input argument at `router.RouteRegisterRule`.