httprec

package module
v0.0.0-...-904792f Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2025 License: MIT Imports: 4 Imported by: 5

README

go-httprec

Package httprec provides tools for recording and modifying HTTP responses that are useful for creating middleware, for the Go programming language.

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-httprec

GoDoc

Import

To import package httprec use import code like the following:

import "github.com/reiver/go-httprec"

Installation

To install package httprec do the following:

GOPROXY=direct go get github.com/reiver/go-httprec

Author

Package httprec was written by Charles Iliya Krempeaux

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Always

type Always struct{}
var AlwaysRecord Always

func (Always) MayRecord

func (receiver Always) MayRecord(subResponseWriter http.ResponseWriter, request *http.Request) bool

type Never

type Never struct{}
var NeverRecord Never

func (Never) MayRecord

func (receiver Never) MayRecord(subResponseWriter http.ResponseWriter, request *http.Request) bool

type ResponseWriter

type ResponseWriter struct {
	// contains filtered or unexported fields
}

ResponseWriter is an http.ResponseWriter that can recording the HTTP-response that is written.

To use one, called NewResponseWriter:

func ServeHTTP(rw http.ResponseWriter, req *http.Request) {

	// ...

	recordingResponseWriter := httprec.NewResponseWriter(rw, req, controller)

	// ...

}

func NewResponseWriter

func NewResponseWriter(subResponseWriter http.ResponseWriter, request *http.Request, controller ResponseWriterController) *ResponseWriter

NewResponseWriter returns a new ResponseWriter.

You should only create a ResponseWriter by using NewResponseWriter.

func (*ResponseWriter) Bytes

func (receiver *ResponseWriter) Bytes() []byte

Bytes returns the recording body of the HTTP-response.

func (*ResponseWriter) HasRecording

func (receiver *ResponseWriter) HasRecording() bool

HasRecording returns true is something was recorded, and false otherwirse.

func (*ResponseWriter) Header

func (receiver *ResponseWriter) Header() http.Header

Header is the same as Header in http.ResponseWriter

func (*ResponseWriter) StatusCode

func (receiver *ResponseWriter) StatusCode() int

StatusCode returns the HTTP-response statatus-code.

func (*ResponseWriter) Write

func (receiver *ResponseWriter) Write(bytes []byte) (int, error)

Write is the same as Write in http.ResponseWriter

func (*ResponseWriter) WriteHeader

func (receiver *ResponseWriter) WriteHeader(statusCode int)

WriteHeader is the same as WriteHeader in http.ResponseWriter

type ResponseWriterController

type ResponseWriterController interface {
	MayRecord(subResponseWriter http.ResponseWriter, request *http.Request) bool
}

ResponseWriterController decides whether a ResponseWriter records or not.

When [ResponseWriterController.MayRecord] is called, it also has the opportunity to, for example, modify the (internal) sub-http.ResponseWriter and the http.Request.

[ResponseWriterController.MayRecord] will only be called once. Its result will be stored by ResponseWriter.

For example:

import "strings"

type MyResponseWriterController struct {}

func (receiver MyResponseWriterController) MayRecord(subResponseWriter http.ResponseWriter, request *http.Request) bool {
	// Only record if response is Markdown.

(So that our middle-ware can transform it.)

	return strings.Contains(subResponseWriter.Header().Get("Content-Type"), "text/markdown")
}

type ResponseWriterControllerFunc

type ResponseWriterControllerFunc func(subResponseWriter http.ResponseWriter, request *http.Request) bool

func (ResponseWriterControllerFunc) MayRecord

func (fn ResponseWriterControllerFunc) MayRecord(subResponseWriter http.ResponseWriter, request *http.Request) bool

Jump to

Keyboard shortcuts

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