github.com/TeaOSLab/EdgeNode@v1.3.8/internal/nodes/http_writer_empty.go (about)

     1  // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  
     3  package nodes
     4  
     5  import (
     6  	"bufio"
     7  	"net"
     8  	"net/http"
     9  )
    10  
    11  // EmptyResponseWriter 空的响应Writer
    12  type EmptyResponseWriter struct {
    13  	header       http.Header
    14  	parentWriter http.ResponseWriter
    15  
    16  	statusCode int
    17  }
    18  
    19  func NewEmptyResponseWriter(parentWriter http.ResponseWriter) *EmptyResponseWriter {
    20  	return &EmptyResponseWriter{
    21  		header:       http.Header{},
    22  		parentWriter: parentWriter,
    23  	}
    24  }
    25  
    26  func (this *EmptyResponseWriter) Header() http.Header {
    27  	return this.header
    28  }
    29  
    30  func (this *EmptyResponseWriter) Write(data []byte) (int, error) {
    31  	if this.statusCode > 300 && this.parentWriter != nil {
    32  		return this.parentWriter.Write(data)
    33  	}
    34  	return 0, nil
    35  }
    36  
    37  func (this *EmptyResponseWriter) WriteHeader(statusCode int) {
    38  	this.statusCode = statusCode
    39  
    40  	if this.statusCode > 300 && this.parentWriter != nil {
    41  		var parentHeader = this.parentWriter.Header()
    42  		for k, v := range this.header {
    43  			parentHeader[k] = v
    44  		}
    45  		this.parentWriter.WriteHeader(this.statusCode)
    46  	}
    47  }
    48  
    49  func (this *EmptyResponseWriter) StatusCode() int {
    50  	return this.statusCode
    51  }
    52  
    53  // Hijack Hijack
    54  func (this *EmptyResponseWriter) Hijack() (conn net.Conn, buf *bufio.ReadWriter, err error) {
    55  	if this.parentWriter == nil {
    56  		return
    57  	}
    58  	hijack, ok := this.parentWriter.(http.Hijacker)
    59  	if ok {
    60  		return hijack.Hijack()
    61  	}
    62  	return
    63  }