From d0cd8b9fd9d233da0dca0aa8f13eb0881963cf7b Mon Sep 17 00:00:00 2001 From: "Manu Mtz.-Almeida" Date: Tue, 26 Jan 2016 14:55:45 +0100 Subject: [PATCH 1/2] Adds Status() method to Context --- context.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/context.go b/context.go index b784c14..3621098 100644 --- a/context.go +++ b/context.go @@ -112,7 +112,7 @@ func (c *Context) Abort() { // AbortWithStatus calls `Abort()` and writes the headers with the specified status code. // For example, a failed attempt to authentificate a request could use: context.AbortWithStatus(401). func (c *Context) AbortWithStatus(code int) { - c.Writer.WriteHeader(code) + c.Status(code) c.Abort() } @@ -310,6 +310,10 @@ func (c *Context) requestHeader(key string) string { /******** RESPONSE RENDERING ********/ /************************************/ +func (c *Context) Status(code int) { + c.writermem.WriteHeader(code) +} + // Header is a intelligent shortcut for c.Writer.Header().Set(key, value) // It writes a header in the response. // If value == "", this method removes the header `c.Writer.Header().Del(key)` @@ -322,7 +326,7 @@ func (c *Context) Header(key, value string) { } func (c *Context) Render(code int, r render.Render) { - c.writermem.WriteHeader(code) + c.Status(code) if err := r.Render(c.Writer); err != nil { c.renderError(err) } @@ -352,7 +356,7 @@ func (c *Context) IndentedJSON(code int, obj interface{}) { // JSON serializes the given struct as JSON into the response body. // It also sets the Content-Type as "application/json". func (c *Context) JSON(code int, obj interface{}) { - c.writermem.WriteHeader(code) + c.Status(code) if err := render.WriteJSON(c.Writer, obj); err != nil { c.renderError(err) } @@ -366,7 +370,7 @@ func (c *Context) XML(code int, obj interface{}) { // String writes the given string into the response body. func (c *Context) String(code int, format string, values ...interface{}) { - c.writermem.WriteHeader(code) + c.Status(code) render.WriteString(c.Writer, format, values) } From 7b4e9a81a5d9abad33c8459d75918530884a1546 Mon Sep 17 00:00:00 2001 From: "Manu Mtz.-Almeida" Date: Tue, 26 Jan 2016 15:55:30 +0100 Subject: [PATCH 2/2] Cosmetic changes --- CHANGELOG.md | 2 +- context.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b5b6ad..82f1bea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ###Gin 1.0rc2 (...) -- [PERFORMANCE] Fast path for writting Content-Type. +- [PERFORMANCE] Fast path for writing Content-Type. - [PERFORMANCE] Much faster 404 routing - [PERFORMANCE] Allocation optimizations - [PERFORMANCE] Faster root tree lookup diff --git a/context.go b/context.go index 3621098..c2aed74 100644 --- a/context.go +++ b/context.go @@ -412,9 +412,9 @@ func (c *Context) Stream(step func(w io.Writer) bool) { case <-clientGone: return default: - keepopen := step(w) + keepOpen := step(w) w.Flush() - if !keepopen { + if !keepOpen { return } }