From 434b07675e09387b6e2309e6f774cb5219fdf3ce Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Thu, 14 Dec 2017 14:15:29 +0200 Subject: [PATCH] add example for the simple context#WriteGzip Former-commit-id: 3b4e0834370ebde5e4796b3d75cdbd5c2c25093f --- _examples/README.md | 1 + .../http_responsewriter/write-gzip/main.go | 21 +++++++++++++++ context/context.go | 26 +++++-------------- 3 files changed, 29 insertions(+), 19 deletions(-) create mode 100644 _examples/http_responsewriter/write-gzip/main.go diff --git a/_examples/README.md b/_examples/README.md index 83272b5f..44bb0f2c 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -292,6 +292,7 @@ You can serve [quicktemplate](https://github.com/valyala/quicktemplate) and [her - [Write `valyala/quicktemplate` templates](http_responsewriter/quicktemplate) - [Write `shiyanhui/hero` templates](http_responsewriter/hero) - [Text, Markdown, HTML, JSON, JSONP, XML, Binary](http_responsewriter/write-rest/main.go) +- [Write Gzip](http_responsewriter/write-gzip/main.go) - [Stream Writer](http_responsewriter/stream-writer/main.go) - [Transactions](http_responsewriter/transactions/main.go) diff --git a/_examples/http_responsewriter/write-gzip/main.go b/_examples/http_responsewriter/write-gzip/main.go new file mode 100644 index 00000000..2e16650e --- /dev/null +++ b/_examples/http_responsewriter/write-gzip/main.go @@ -0,0 +1,21 @@ +package main + +import "github.com/kataras/iris" + +func main() { + app := iris.New() + app.Get("/", func(ctx iris.Context) { + ctx.WriteGzip([]byte("Hello World!")) + ctx.Header("X-Custom", + "Headers can be set here after WriteGzip as well, because the data are kept before sent to the client when using the context's GzipResponseWriter and ResponseRecorder.") + }) + + app.Get("/2", func(ctx iris.Context) { + // same as the `WriteGzip`. + // However GzipResponseWriter gives you more options, like + // reset data, disable and more, look its methods. + ctx.GzipResponseWriter().WriteString("Hello World!") + }) + + app.Run(iris.Addr(":8080")) +} diff --git a/context/context.go b/context/context.go index e3a38ad8..3b50f6a3 100644 --- a/context/context.go +++ b/context/context.go @@ -599,13 +599,11 @@ type Context interface { ClientSupportsGzip() bool // WriteGzip accepts bytes, which are compressed to gzip format and sent to the client. // returns the number of bytes written and an error ( if the client doesn' supports gzip compression) - // - // This function writes temporary gzip contents, the ResponseWriter is untouched. + // You may re-use this function in the same handler + // to write more data many times without any troubles. WriteGzip(b []byte) (int, error) // TryWriteGzip accepts bytes, which are compressed to gzip format and sent to the client. // If client does not supprots gzip then the contents are written as they are, uncompressed. - // - // This function writes temporary gzip contents, the ResponseWriter is untouched. TryWriteGzip(b []byte) (int, error) // GzipResponseWriter converts the current response writer into a response writer // which when its .Write called it compress the data to gzip and writes them to the client. @@ -1900,28 +1898,18 @@ var ( // WriteGzip accepts bytes, which are compressed to gzip format and sent to the client. // returns the number of bytes written and an error ( if the client doesn' supports gzip compression) // -// This function writes temporary gzip contents, the ResponseWriter is untouched. +// You may re-use this function in the same handler +// to write more data many times without any troubles. func (ctx *context) WriteGzip(b []byte) (int, error) { - if ctx.ClientSupportsGzip() { - ctx.writer.Header().Add(varyHeaderKey, acceptEncodingHeaderKey) - - gzipWriter := acquireGzipWriter(ctx.writer) - defer releaseGzipWriter(gzipWriter) - n, err := gzipWriter.Write(b) - - if err == nil { - ctx.Header(contentEncodingHeaderKey, "gzip") - } // else write the contents as it is? no let's create a new func for this - return n, err + if !ctx.ClientSupportsGzip() { + return 0, errClientDoesNotSupportGzip } - return 0, errClientDoesNotSupportGzip + return ctx.GzipResponseWriter().Write(b) } // TryWriteGzip accepts bytes, which are compressed to gzip format and sent to the client. // If client does not supprots gzip then the contents are written as they are, uncompressed. -// -// This function writes temporary gzip contents, the ResponseWriter is untouched. func (ctx *context) TryWriteGzip(b []byte) (int, error) { n, err := ctx.WriteGzip(b) if err != nil {