Skip to content
Myr

reader.go

This file

Path

render/reader.go

48 lines · 1.2 KiB · Go

Last changed by

chore: update go.mod and remove space from copyright (#3158)thinkeroubefore adoption4b68a5f

History of this file

Take it

The walk stopped after 200 commits: lines older than a550c56 point at it.

chore: update go.mod and remove space from copyright (#3158)thinkeroubefore adoption4b68a5f

line 1why, one step back
1// Copyright 2018 Gin Core Team. All rights reserved.

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 2–48why, one step back
2// Use of this source code is governed by a MIT style
3// license that can be found in the LICENSE file.45package render67import (8 "io"9 "net/http"10 "strconv"11)1213// Reader contains the IO reader and its length, and custom ContentType and other headers.14type Reader struct {15 ContentType string16 ContentLength int6417 Reader io.Reader18 Headers map[string]string19}2021// Render (Reader) writes data with custom ContentType and headers.22func (r Reader) Render(w http.ResponseWriter) (err error) {23 r.WriteContentType(w)24 if r.ContentLength >= 0 {25 if r.Headers == nil {26 r.Headers = map[string]string{}27 }28 r.Headers["Content-Length"] = strconv.FormatInt(r.ContentLength, 10)29 }30 r.writeHeaders(w, r.Headers)31 _, err = io.Copy(w, r.Reader)32 return33}3435// WriteContentType (Reader) writes custom ContentType.36func (r Reader) WriteContentType(w http.ResponseWriter) {37 writeContentType(w, []string{r.ContentType})38}3940// writeHeaders writes custom Header.41func (r Reader) writeHeaders(w http.ResponseWriter, headers map[string]string) {42 header := w.Header()43 for k, v := range headers {44 if header.Get(k) == "" {45 header.Set(k, v)46 }47 }48}