Browse Source

Move writer proxy to a separate file

Carl Jackson 12 years ago
parent
commit
66431cbd3b
2 changed files with 61 additions and 56 deletions
  1. +0
    -56
      web/middleware/logger.go
  2. +61
    -0
      web/middleware/writer_proxy.go

+ 0
- 56
web/middleware/logger.go View File

@ -1,10 +1,8 @@
package middleware package middleware
import ( import (
"bufio"
"bytes" "bytes"
"log" "log"
"net"
"net/http" "net/http"
"time" "time"
@ -93,57 +91,3 @@ func wrapWriter(w http.ResponseWriter) writerProxy {
return &bw return &bw
} }
} }
type writerProxy interface {
http.ResponseWriter
maybeWriteHeader()
status() int
}
type basicWriter struct {
http.ResponseWriter
wroteHeader bool
code int
}
func (b *basicWriter) WriteHeader(code int) {
b.code = code
b.wroteHeader = true
b.ResponseWriter.WriteHeader(code)
}
func (b *basicWriter) Write(buf []byte) (int, error) {
b.maybeWriteHeader()
return b.ResponseWriter.Write(buf)
}
func (b *basicWriter) maybeWriteHeader() {
if !b.wroteHeader {
b.WriteHeader(http.StatusOK)
}
}
func (b *basicWriter) status() int {
return b.code
}
func (b *basicWriter) Unwrap() http.ResponseWriter {
return b.ResponseWriter
}
type fancyWriter struct {
basicWriter
}
func (f *fancyWriter) CloseNotify() <-chan bool {
cn := f.basicWriter.ResponseWriter.(http.CloseNotifier)
return cn.CloseNotify()
}
func (f *fancyWriter) Flush() {
fl := f.basicWriter.ResponseWriter.(http.Flusher)
fl.Flush()
}
func (f *fancyWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hj := f.basicWriter.ResponseWriter.(http.Hijacker)
return hj.Hijack()
}
var _ http.CloseNotifier = &fancyWriter{}
var _ http.Flusher = &fancyWriter{}
var _ http.Hijacker = &fancyWriter{}

+ 61
- 0
web/middleware/writer_proxy.go View File

@ -0,0 +1,61 @@
package middleware
import (
"bufio"
"net"
"net/http"
)
type writerProxy interface {
http.ResponseWriter
maybeWriteHeader()
status() int
}
type basicWriter struct {
http.ResponseWriter
wroteHeader bool
code int
}
func (b *basicWriter) WriteHeader(code int) {
b.code = code
b.wroteHeader = true
b.ResponseWriter.WriteHeader(code)
}
func (b *basicWriter) Write(buf []byte) (int, error) {
b.maybeWriteHeader()
return b.ResponseWriter.Write(buf)
}
func (b *basicWriter) maybeWriteHeader() {
if !b.wroteHeader {
b.WriteHeader(http.StatusOK)
}
}
func (b *basicWriter) status() int {
return b.code
}
func (b *basicWriter) Unwrap() http.ResponseWriter {
return b.ResponseWriter
}
type fancyWriter struct {
basicWriter
}
func (f *fancyWriter) CloseNotify() <-chan bool {
cn := f.basicWriter.ResponseWriter.(http.CloseNotifier)
return cn.CloseNotify()
}
func (f *fancyWriter) Flush() {
fl := f.basicWriter.ResponseWriter.(http.Flusher)
fl.Flush()
}
func (f *fancyWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hj := f.basicWriter.ResponseWriter.(http.Hijacker)
return hj.Hijack()
}
var _ http.CloseNotifier = &fancyWriter{}
var _ http.Flusher = &fancyWriter{}
var _ http.Hijacker = &fancyWriter{}

Loading…
Cancel
Save