Example #1
1
// Gzip accepts bytes, which are compressed to gzip format and sent to the client
func (ctx *Context) Gzip(b []byte, status int) {
	ctx.RequestCtx.Response.Header.Add(varyHeader, acceptEncodingHeader)

	if ctx.clientAllowsGzip() {
		_, err := fasthttp.WriteGzip(ctx.RequestCtx.Response.BodyWriter(), b)
		if err == nil {
			ctx.SetHeader(contentEncodingHeader, "gzip")
		}
	}
}
Example #2
0
// RenderGzip a Text response using gzip compression.
func (t Text) RenderGzip(ctx *fasthttp.RequestCtx, v interface{}) error {
	c := string(ctx.Request.Header.Peek(ContentType))
	if c != "" {
		t.Head.ContentType = c
	}
	ctx.Response.Header.Add("Content-Encoding", "gzip")
	t.Head.Write(ctx)
	fasthttp.WriteGzip(ctx.Response.BodyWriter(), []byte(v.(string)))

	return nil
}
Example #3
0
// RenderGzip a data response using gzip compression.
func (d Data) RenderGzip(ctx *fasthttp.RequestCtx, v interface{}) error {
	c := string(ctx.Request.Header.Peek(ContentType))
	if c != "" {
		d.Head.ContentType = c
	}

	d.Head.Write(ctx)
	_, err := fasthttp.WriteGzip(ctx.Response.BodyWriter(), v.([]byte))
	if err == nil {
		ctx.Response.Header.Add("Content-Encoding", "gzip")
	}
	return err
}
Example #4
0
// renderSerialized renders contents with a serializer with status OK which you can change using RenderWithStatus or ctx.SetStatusCode(iris.StatusCode)
func (ctx *Context) renderSerialized(contentType string, obj interface{}, options ...map[string]interface{}) error {
	s := ctx.framework.serializers
	finalResult, err := s.Serialize(contentType, obj, options...)
	if err != nil {
		return err
	}
	gzipEnabled := ctx.framework.Config.Gzip
	charset := ctx.framework.Config.Charset
	if len(options) > 0 {
		gzipEnabled = getGzipOption(gzipEnabled, options[0]) // located to the template.go below the RenderOptions
		charset = getCharsetOption(charset, options[0])
	}
	ctype := contentType

	if ctype == contentMarkdown { // remember the text/markdown is just a custom internal iris content type, which in reallity renders html
		ctype = contentHTML
	}

	if ctype != contentBinary { // set the charset only on non-binary data
		ctype += "; charset=" + charset
	}
	ctx.SetContentType(ctype)

	if gzipEnabled && ctx.clientAllowsGzip() {
		_, err := fasthttp.WriteGzip(ctx.RequestCtx.Response.BodyWriter(), finalResult)
		if err != nil {
			return err
		}
		ctx.RequestCtx.Response.Header.Add(varyHeader, acceptEncodingHeader)
		ctx.SetHeader(contentEncodingHeader, "gzip")
	} else {
		ctx.Response.SetBody(finalResult)
	}

	ctx.SetStatusCode(StatusOK)

	return nil
}