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

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"9	"errors"10	"io"11	"net"12	"net/http"13)1415const (16	noWritten     = -117	defaultStatus = http.StatusOK18)1920var errHijackAlreadyWritten = errors.New("gin: response body already written")2122// 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) {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 {115		return nil, nil, errHijackAlreadyWritten116	}117	hijacker, ok := w.ResponseWriter.(http.Hijacker)118	if !ok {119		return nil, nil, http.ErrNotSupported120	}121	if w.size < 0 {122		w.size = 0123	}124	return hijacker.Hijack()125}126127// CloseNotify implements the http.CloseNotifier interface.128func (w *responseWriter) CloseNotify() <-chan bool {129	if cn, ok := w.ResponseWriter.(http.CloseNotifier); ok {130		return cn.CloseNotify()131	}132	return nil133}134135// Flush implements the http.Flusher interface.136func (w *responseWriter) Flush() {137	w.WriteHeaderNow()138	if f, ok := w.ResponseWriter.(http.Flusher); ok {139		f.Flush()140	}141}142143func (w *responseWriter) Pusher() (pusher http.Pusher) {144	if pusher, ok := w.ResponseWriter.(http.Pusher); ok {145		return pusher146	}147	return nil148}