Skip to content

Commit

Permalink
add example for the simple context#WriteGzip
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Dec 14, 2017
1 parent 0be7759 commit c98d012
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
1 change: 1 addition & 0 deletions _examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
21 changes: 21 additions & 0 deletions _examples/http_responsewriter/write-gzip/main.go
Original file line number Diff line number Diff line change
@@ -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"))
}
26 changes: 7 additions & 19 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit c98d012

Please sign in to comment.