responsewriter

package
v6.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 24, 2015 License: MIT Imports: 9 Imported by: 1

Documentation

Overview

Package responsewriter provides some ResponseWriter wrappers that help with development of middleware and also support context sharing via embedding of a stack.Contexter if it is available.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

type Buffer struct {

	// ResponseWriter is the underlying response writer that is wrapped by Buffer
	http.ResponseWriter

	// if the underlying ResponseWriter is a Contexter, that Contexter is saved here
	stack.Contexter

	// Buffer is the underlying io.Writer that buffers the response body
	Buffer bytes.Buffer

	// Code is the cached status code
	Code int
	// contains filtered or unexported fields
}

Buffer is a ResponseWriter wrapper that may be used as buffer.

A middleware may pass it to the next handlers ServeHTTP method as a drop in replacement for the response writer. After the ServeHTTP method is run the middleware may examine what has been written to the Buffer and decide what to write to the "original" ResponseWriter (that may well be another buffer passed from another middleware).

The downside is the body being written two times and the complete caching of the body in the memory which will be inacceptable for large bodies. Therefor Peek is an alternative response writer wrapper that only caching headers and status code but allowing to intercept calls of the Write method.

func NewBuffer

func NewBuffer(w http.ResponseWriter) (bf *Buffer)

NewBuffer creates a new Buffer by wrapping the given response writer.

func (*Buffer) Body

func (bf *Buffer) Body() []byte

Body returns the bytes of the underlying buffer (that is meant to be the body of the response)

func (*Buffer) BodyString

func (bf *Buffer) BodyString() string

BodyString returns the string of the underlying buffer (that is meant to be the body of the response)

func (*Buffer) FlushAll

func (bf *Buffer) FlushAll()

FlushAll flushes headers, status code and body to the underlying ResponseWriter, if something changed

func (*Buffer) FlushCode

func (bf *Buffer) FlushCode()

FlushCode flushes the status code to the underlying responsewriter if it was set.

func (*Buffer) FlushHeaders

func (bf *Buffer) FlushHeaders()

FlushHeaders adds the headers to the underlying ResponseWriter, removing them from Buffer.

func (*Buffer) HasChanged

func (bf *Buffer) HasChanged() bool

HasChanged returns true if Header, WriteHeader or Write has been called

func (*Buffer) Header

func (bf *Buffer) Header() http.Header

Header returns the cached http.Header and tracks this call as change

func (*Buffer) IsOk

func (bf *Buffer) IsOk() bool

IsOk returns true if the cached status code is not set or in the 2xx range.

func (*Buffer) Reset

func (bf *Buffer) Reset()

Reset set the Buffer to the defaults

func (*Buffer) Write

func (bf *Buffer) Write(b []byte) (int, error)

Write writes to the underlying buffer and tracks this call as change

func (*Buffer) WriteHeader

func (bf *Buffer) WriteHeader(i int)

WriteHeader writes the cached status code and tracks this call as change

type Deflate

type Deflate struct {
	io.WriteCloser
	http.ResponseWriter
	stack.Contexter
}

func NewDeflate

func NewDeflate(rw http.ResponseWriter, level int) *Deflate

if level < 0, default compression is used if level = 0, no compression is used if level >= 9, max compression is used

func (*Deflate) Write

func (w *Deflate) Write(b []byte) (int, error)

type ErrBodyFlushedBeforeCode

type ErrBodyFlushedBeforeCode struct{}

ErrBodyFlushedBeforeCode is the error returned if a body flushed to an underlying response writer before the status code has been flushed. It should help to sort out errors in middleware that uses responsewriter wrappers from this package.

func (ErrBodyFlushedBeforeCode) Error

func (e ErrBodyFlushedBeforeCode) Error() string

Error returns the error message

type ErrCodeFlushedBeforeHeaders

type ErrCodeFlushedBeforeHeaders struct{}

ErrCodeFlushedBeforeHeaders is the error returned if a status code flushed to an underlying response writer before the headers have been flushed. It should help to sort out errors in middleware that uses responsewriter wrappers from this package.

func (ErrCodeFlushedBeforeHeaders) Error

Error returns the error message

type EscapeHTML

type EscapeHTML struct {
	http.ResponseWriter

	// if the underlying ResponseWriter is a Contexter, that Contexter is saved here
	stack.Contexter
}

EscapeHTML wraps an http.ResponseWriter in order to override its Write method so that it escape html special chars while writing

func NewEscapeHTML

func NewEscapeHTML(w http.ResponseWriter) *EscapeHTML

func (*EscapeHTML) Write

func (e *EscapeHTML) Write(b []byte) (num int, err error)

Write writes to the inner *http.ResponseWriter escaping html special chars on the fly Since there is nothing useful to do with the number of bytes written returned from the inner responsewriter, the returned int is always 0. Since there is nothing useful to do in case of a failed write to the response writer, writing errors are silently dropped. the method is modelled after EscapeText from encoding/xml

type GZip

type GZip struct {
	io.WriteCloser
	http.ResponseWriter
	stack.Contexter
}

func NewGZIP

func NewGZIP(rw http.ResponseWriter) *GZip

func (*GZip) Write

func (w *GZip) Write(b []byte) (int, error)

type JSONDecoder

type JSONDecoder struct {
	// ResponseWriter is the underlying response writer that is wrapped by Buffer
	http.ResponseWriter

	// if the underlying ResponseWriter is a Contexter, that Contexter is saved here
	stack.Contexter

	// Code is the cached status code
	Code int
	// contains filtered or unexported fields
}

JSONDecoder is a ResponseWriter wrapper that decodes everything writen to it to a json object.

func NewJSONDecoder

func NewJSONDecoder(w http.ResponseWriter) (dec *JSONDecoder)

NewJSONDecoder creates a new JSONDecoder by wrapping the given response writer.

func (*JSONDecoder) Decode

func (d *JSONDecoder) Decode(target interface{}) error

Decode decodes the buffer into the given target

func (*JSONDecoder) FlushCode

func (d *JSONDecoder) FlushCode()

FlushCode flushes the status code to the underlying responsewriter if it was set.

func (*JSONDecoder) FlushHeaders

func (d *JSONDecoder) FlushHeaders()

FlushHeaders adds the headers to the underlying ResponseWriter, removing them from Buffer.

func (*JSONDecoder) HasChanged

func (d *JSONDecoder) HasChanged() bool

HasChanged returns true if Header, WriteHeader or Write has been called

func (*JSONDecoder) Header

func (d *JSONDecoder) Header() http.Header

Header returns the cached http.Header and tracks this call as change

func (*JSONDecoder) Write

func (d *JSONDecoder) Write(b []byte) (int, error)

Write writes to the underlying buffer and tracks this call as change

func (*JSONDecoder) WriteHeader

func (d *JSONDecoder) WriteHeader(i int)

WriteHeader writes the cached status code and tracks this call as change

type PanicCodes

type PanicCodes struct {
	// Codes that should trigger a panic
	Codes []int
	*Buffer
}

PanicCodes is a debugging tool to get a stack trace, if some http.StatusCode is set to one of its codes

func NewPanicCodes

func NewPanicCodes(w http.ResponseWriter, codes ...int) *PanicCodes

func (*PanicCodes) WriteHeader

func (p *PanicCodes) WriteHeader(code int)

WriteHeader triggers a panic, if the code is one of the Codes otherwise it passes to the underlying Buffer

type Peek

type Peek struct {
	// the cached status code
	Code int

	// the underlying response writer
	http.ResponseWriter

	// if the underlying ResponseWriter is a Contexter, that Contexter is saved here
	stack.Contexter
	// contains filtered or unexported fields
}

Peek is a ResponseWriter wrapper that intercepts the writing of the body, allowing to check headers and status code that has been set to prevent the body writing and to write a modified body.

Peek is more efficient than Buffer, since it does not write to a buffer first before determining if the body will be flushed to the underlying response writer.

func NewPeek

func NewPeek(rw http.ResponseWriter, proceed func(*Peek) bool) *Peek

NewPeek creates a new Peek for the given response writer using the given proceed function.

The proceed function is called when the Write method is run for the first time. It receives the Peek and may check the cached headers and the cached status code.

If the cached headers and the cached status code should be flushed to the underlying response writer, the proceed function must do so (e.g. by calling FlushMissing). This also allows to write other headers status codes or not write them at all.

If the proceed function returns true, the body will be written to the underlying response write. That also holds for all following calls of Write when proceed is not run anymore.

To write some other body or no body at all, proceed must return false, Then after the Write method has been run the Peek might be checked again and the underlying ResponseWriter that is exposed by Peek might be used to write a custom body.

However if the http.Handler that receives the Peek does not write to the body, proceed will not be called at all.

To ensure that any cached headers and status code will be flushed, the FlushMissing method can be called after the serving http.Handler is run.

If proceed is nil, Write behaves as if proceed would have returned true.

func (*Peek) FlushCode

func (p *Peek) FlushCode()

FlushCode writes the status code to the underlying responsewriter if it was set

func (*Peek) FlushHeaders

func (p *Peek) FlushHeaders()

FlushHeaders adds the headers to the underlying ResponseWriter, removing them from Peek

func (*Peek) FlushMissing

func (p *Peek) FlushMissing()

FlushMissing ensures that the Headers and Code are written to the underlying ResponseWriter if they are not written yet (and nothing has been written to the body)

func (*Peek) HasChanged

func (p *Peek) HasChanged() bool

HasChanged returns true if Header or WriteHeader method have been called or if Write has been called and did write to the underlying response writer.

func (*Peek) Header

func (p *Peek) Header() http.Header

Header returns the cached http.Header, tracking the call as change

func (*Peek) IsOk

func (p *Peek) IsOk() bool

IsOk returns true if the returned status code is not set or in the 2xx range

func (*Peek) Reset

func (p *Peek) Reset()

Reset set the Peek to the defaults, so it will act as if it was freshly initialized.

func (*Peek) Write

func (p *Peek) Write(b []byte) (int, error)

Write writes to the underlying response writer, if the proceed function returns true. Otherwise it returns 0, io.EOF. If the data is written, the call is tracked as change.

The proceed function is only called the first time, Write has been called. If proceed is nil, it behaves as if proceed would have returned true.

See NewPeek for more informations about the usage of the proceed function.

func (*Peek) WriteHeader

func (p *Peek) WriteHeader(i int)

WriteHeader writes the cached status code, tracking the call as change

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL