Skip to content
Myr

response_writer.go

This file

Path

response_writer.go

148 lines · 3.3 KiB · Go

Last changed by

fix(response): panic on Hijack/CloseNotify when wrapper unsupported (#4645)Sai Asish Ybefore adoptiond75fcd4

History of this file

Take it

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

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 1–8why, one step back
1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package gin67import (8 "bufio"

chore(response): prevention of Hijack() runtime panics (#4295)諏訪原慶斗before adoption32065bb

line 9why, one step back
9 "errors"

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 10–19why, one step back
10 "io"
11 "net"12 "net/http"13)1415const (16 noWritten = -117 defaultStatus = http.StatusOK18)19

fix(response): refine hijack behavior for response lifecycle (#4373)Bo-Yi Wubefore adoption234a6d4

line 20why, one step back
20var errHijackAlreadyWritten = errors.New("gin: response body already written")

chore(response): prevention of Hijack() runtime panics (#4295)諏訪原慶斗before adoption32065bb

line 21why, one step back
21

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 22–111why, one step back
22// ResponseWriter ...
23type ResponseWriter interface {24 http.ResponseWriter25 http.Hijacker26 http.Flusher27 http.CloseNotifier2829 // Status returns the HTTP response status code of the current request.30 Status() int3132 // Size returns the number of bytes already written into the response http body.33 // See Written()34 Size() int3536 // WriteString writes the string into the response body.37 WriteString(string) (int, error)3839 // Written returns true if the response body was already written.40 Written() bool4142 // WriteHeaderNow forces to write the http header (status code + headers).43 WriteHeaderNow()4445 // Pusher get the http.Pusher for server push46 Pusher() http.Pusher47}4849type responseWriter struct {50 http.ResponseWriter51 size int52 status int53}5455var _ ResponseWriter = (*responseWriter)(nil)5657func (w *responseWriter) Unwrap() http.ResponseWriter {58 return w.ResponseWriter59}6061func (w *responseWriter) reset(writer http.ResponseWriter) {62 w.ResponseWriter = writer63 w.size = noWritten64 w.status = defaultStatus65}6667func (w *responseWriter) WriteHeader(code int) {68 if code > 0 && w.status != code {69 if w.Written() {70 debugPrint("[WARNING] Headers were already written. Wanted to override status code %d with %d", w.status, code)71 return72 }73 w.status = code74 }75}7677func (w *responseWriter) WriteHeaderNow() {78 if !w.Written() {79 w.size = 080 w.ResponseWriter.WriteHeader(w.status)81 }82}8384func (w *responseWriter) Write(data []byte) (n int, err error) {85 w.WriteHeaderNow()86 n, err = w.ResponseWriter.Write(data)87 w.size += n88 return89}9091func (w *responseWriter) WriteString(s string) (n int, err error) {92 w.WriteHeaderNow()93 n, err = io.WriteString(w.ResponseWriter, s)94 w.size += n95 return96}9798func (w *responseWriter) Status() int {99 return w.status100}101102func (w *responseWriter) Size() int {103 return w.size104}105106func (w *responseWriter) Written() bool {107 return w.size != noWritten108}109110// Hijack implements the http.Hijacker interface.111func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {

fix(response): refine hijack behavior for response lifecycle (#4373)Bo-Yi Wubefore adoption234a6d4

lines 112–114why, one step back
112 // Allow hijacking before any data is written (size == -1) or after headers are written (size == 0),
113 // but not after body data is written (size > 0). For compatibility with websocket libraries (e.g., github.com/coder/websocket)114 if w.size > 0 {

chore(response): prevention of Hijack() runtime panics (#4295)諏訪原慶斗before adoption32065bb

lines 115–116why, one step back
115 return nil, nil, errHijackAlreadyWritten
116 }

fix(response): panic on Hijack/CloseNotify when wrapper unsupported (#4645)Sai Asish Ybefore adoptiond75fcd4

lines 117–120why, one step back
117 hijacker, ok := w.ResponseWriter.(http.Hijacker)
118 if !ok {119 return nil, nil, http.ErrNotSupported120 }

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 121–123why, one step back
121 if w.size < 0 {
122 w.size = 0123 }

fix(response): panic on Hijack/CloseNotify when wrapper unsupported (#4645)Sai Asish Ybefore adoptiond75fcd4

line 124why, one step back
124 return hijacker.Hijack()

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 125–128why, one step back
125}
126127// CloseNotify implements the http.CloseNotifier interface.128func (w *responseWriter) CloseNotify() <-chan bool {

fix(response): panic on Hijack/CloseNotify when wrapper unsupported (#4645)Sai Asish Ybefore adoptiond75fcd4

lines 129–132why, one step back
129 if cn, ok := w.ResponseWriter.(http.CloseNotifier); ok {
130 return cn.CloseNotify()131 }132 return nil

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 133–137why, one step back
133}
134135// Flush implements the http.Flusher interface.136func (w *responseWriter) Flush() {137 w.WriteHeaderNow()

chore(response): prevent Flush() panic when `http.Flusher` (#4479)Twacqwqbefore adoption26c3a62

lines 138–140why, one step back
138 if f, ok := w.ResponseWriter.(http.Flusher); ok {
139 f.Flush()140 }

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 141–148why, one step back
141}
142143func (w *responseWriter) Pusher() (pusher http.Pusher) {144 if pusher, ok := w.ResponseWriter.(http.Pusher); ok {145 return pusher146 }147 return nil148}