Skip to content

DataFromReader with nil extraHeaders crashes #2111

Closed
@nikandfor

Description

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

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions