Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataFromReader with nil extraHeaders crashes #2111

Closed
nikandfor opened this issue Oct 28, 2019 · 0 comments · Fixed by #2121
Closed

DataFromReader with nil extraHeaders crashes #2111

nikandfor opened this issue Oct 28, 2019 · 0 comments · Fixed by #2121

Comments

@nikandfor
Copy link
Contributor

Description

gin.Context.DataFromReader crashes if I don't set extra headers.

I think this little fix would be the best solution to keep API and not to compel me to write map[string]string{} each time.

diff --git a/render/reader.go b/render/reader.go
index 502d939..fd308a7 100644
--- a/render/reader.go
+++ b/render/reader.go
@@ -22,6 +22,9 @@ type Reader struct {
 func (r Reader) Render(w http.ResponseWriter) (err error) {
        r.WriteContentType(w)
        if r.ContentLength >= 0 {
+               if r.Headers == nil {
+                       r.Headers = make(map[string]string)
+               }
                r.Headers["Content-Length"] = strconv.FormatInt(r.ContentLength, 10)
        }
        r.writeHeaders(w, r.Headers)

I also have question why extra headers does not replace global headers here?
https://github.com/gin-gonic/gin/blob/master/render/reader.go#L41

// render/reader.go

// writeHeaders writes custom Header.
func (r Reader) writeHeaders(w http.ResponseWriter, headers map[string]string) {
    header := w.Header()
    for k, v := range headers {
        if header.Get(k) == "" {
            header.Set(k, v)
        }
    }
}

How to reproduce

package main

import (
    "net/http"
    "strings"

    "github.com/gin-gonic/gin"
)

func main() {
    r := gin.Default()

    r.GET("/", func(c *gin.Context) {
        // Why not set extra headers as usual (as c.JSON or c.Data)?
        c.Writer.Header().Set("X-Extra-Header", "extra value")

        c.DataFromReader(http.StatusOK, 5, gin.MIMEHTML, strings.NewReader("1234\n"), nil) // If I don't want to set extra headers it crashes
    })

    r.Run(":5000")
}

Expectations

Successful response

$ curl -v http://localhost:5000
# ...
< HTTP/1.1 200 OK
# ...

Actual result

recovered panic and 500 HTTP response

$ curl -v http://localhost:5000
# ...
< HTTP/1.1 500 Internal Server Error
# ...

Environment

  • go version: go1.13 linux/amd64
  • gin version (or commit ref): v1.4.0 and master
  • operating system: ubuntu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant