intercept

package module
v0.0.0-...-8781ab9 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2016 License: MIT Imports: 9 Imported by: 0

README

intercept Build Status GitHub release GoDoc Coverage Status Go Report Card

Middleware to easily intercept and/or modify HTTP requests/responses before sending them to the client/server.

Installation

go get -u gopkg.in/vinxi/intercept.v0

API

See the godoc reference for detailed API documentation.

Examples

Response interceptor and modifier
package main

import (
  "fmt"
  "gopkg.in/vinxi/intercept.v0"
  "gopkg.in/vinxi/vinxi.v0"
  "strings"
)

func main() {
  fmt.Printf("Server listening on port: %d\n", 3100)
  vs := vinxi.NewServer(vinxi.ServerOptions{Host: "localhost", Port: 3100})

  // Intercept request and modify URI path
  vs.Use(intercept.Request(func(req *intercept.RequestModifier) {
    req.Request.RequestURI = "/html"
  }))

  // Intercept and replace response body
  vs.Use(intercept.Response(func(res *intercept.ResponseModifier) {
    data, _ := res.ReadString()
    str := strings.Replace(data, "Herman Melville - Moby-Dick", "A Long History", 1)
    res.String(str)
  }))

  vs.Forward("http://httpbin.org")

  err := vs.Listen()
  if err != nil {
    fmt.Errorf("Error: %s\n", err)
  }
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Response

func Response(fn ResModifierFunc) func(http.Handler) http.Handler

Response intercepts an HTTP response and passes it to the given response modifier function.

Types

type Filter

type Filter func(*http.Request) bool

Filter defines whether a RequestModifier should be applied or not.

type ReqModifierFunc

type ReqModifierFunc func(*RequestModifier)

ReqModifierFunc represent the function interface for request modifiers.

type RequestInterceptor

type RequestInterceptor struct {
	Modifier ReqModifierFunc
	Filters  []Filter
}

RequestInterceptor interceps a given http.Request using a custom request modifier function.

func Request

Request intercepts an HTTP request and passes it to the given request modifier function.

func (*RequestInterceptor) Filter

func (s *RequestInterceptor) Filter(f ...Filter)

Filter intercepts an HTTP requests if and only if the given filter returns true.

func (*RequestInterceptor) HandleHTTP

func (s *RequestInterceptor) HandleHTTP(w http.ResponseWriter, r *http.Request, h http.Handler)

HandleHTTP handles the middleware call chain, intercepting the request data if possible. This methods implements the middleware layer compatible interface.

type RequestModifier

type RequestModifier struct {
	// Header exposes the request http.Header type.
	Header http.Header

	// Request exposes the current http.Request to be modified.
	Request *http.Request
}

RequestModifier implements a convenient abstraction to modify an http.Request, including methods to read, decode/encode and define JSON/XML/String/Binary bodies and modify HTTP headers.

func NewRequestModifier

func NewRequestModifier(req *http.Request) *RequestModifier

NewRequestModifier creates a new request modifier that modifies the given http.Request.

func (*RequestModifier) Bytes

func (s *RequestModifier) Bytes(body []byte)

Bytes sets the given bytes as http.Request body.

func (*RequestModifier) DecodeJSON

func (s *RequestModifier) DecodeJSON(userStruct interface{}) error

DecodeJSON reads and parses the current http.Request body and tries to decode it as JSON.

func (*RequestModifier) DecodeXML

func (s *RequestModifier) DecodeXML(userStruct interface{}, charsetReader XMLCharDecoder) error

DecodeXML reads and parses the current http.Request body and tries to decode it as XML.

func (*RequestModifier) JSON

func (s *RequestModifier) JSON(data interface{}) error

JSON sets the given JSON serializable struct as http.Request body defining the proper content length header.

func (*RequestModifier) ReadBytes

func (s *RequestModifier) ReadBytes() ([]byte, error)

ReadBytes reads the whole body of the current http.Request and returns it as bytes.

func (*RequestModifier) ReadString

func (s *RequestModifier) ReadString() (string, error)

ReadString reads the whole body of the current http.Request and returns it as string.

func (*RequestModifier) Reader

func (s *RequestModifier) Reader(body io.Reader) error

Reader sets the given io.Reader stream as http.Request body defining the proper content length header.

func (*RequestModifier) String

func (s *RequestModifier) String(body string)

String sets the given string as http.Request body.

func (*RequestModifier) XML

func (s *RequestModifier) XML(data interface{}) error

XML sets the given XML serializable struct as http.Request body defining the proper content length header.

type ResModifierFunc

type ResModifierFunc func(*ResponseModifier)

ResModifierFunc defines the function interface for http.Response modifiers.

type ResponseModifier

type ResponseModifier struct {
	Header   http.Header
	Request  *http.Request
	Response *http.Response
}

ResponseModifier implements a convenient abstraction to modify an http.Response, including methods to read, decode/encode and define JSON/XML/String/Binary bodies and modify HTTP headers.

func NewResponseModifier

func NewResponseModifier(req *http.Request, res *http.Response) *ResponseModifier

NewResponseModifier creates a new response modifier that modifies the given http.Response.

func (*ResponseModifier) Bytes

func (s *ResponseModifier) Bytes(body []byte)

Bytes sets the given bytes as http.Response body.

func (*ResponseModifier) DecodeJSON

func (s *ResponseModifier) DecodeJSON(userStruct interface{}) error

DecodeJSON reads and parses the current http.Response body and tries to decode it as JSON.

func (*ResponseModifier) DecodeXML

func (s *ResponseModifier) DecodeXML(userStruct interface{}, charsetReader XMLCharDecoder) error

DecodeXML reads and parses the current http.Response body and tries to decode it as XML.

func (*ResponseModifier) JSON

func (s *ResponseModifier) JSON(data interface{}) error

JSON sets the given JSON serializable struct as http.Response body defining the proper content length header.

func (*ResponseModifier) ReadBytes

func (s *ResponseModifier) ReadBytes() ([]byte, error)

ReadBytes reads the whole body of the current http.Response and returns it as bytes.

func (*ResponseModifier) ReadString

func (s *ResponseModifier) ReadString() (string, error)

ReadString reads the whole body of the current http.Response and returns it as string.

func (*ResponseModifier) Reader

func (s *ResponseModifier) Reader(body io.Reader) error

Reader sets the given io.Reader stream as http.Response body defining the proper content length header.

func (*ResponseModifier) Status

func (s *ResponseModifier) Status(status int)

Status sets a new status code in the http.Response to be modified.

func (*ResponseModifier) String

func (s *ResponseModifier) String(body string)

String sets the given string as http.Response body.

func (*ResponseModifier) XML

func (s *ResponseModifier) XML(data interface{}) error

XML sets the given XML serializable struct as http.Response body defining the proper content length header.

type WriterInterceptor

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

WriterInterceptor implements an http.ResponseWriter compatible interface that will intercept and buffer any method call until the body writes is completed, and then will call the http.Response modifier function to intercept and modify it accordingly before writting the final response fields.

func NewWriterInterceptor

func NewWriterInterceptor(w http.ResponseWriter, req *http.Request, fn ResModifierFunc) *WriterInterceptor

NewWriterInterceptor creates a new http.ResponseWriter capable interface that will intercept the current response.

func (*WriterInterceptor) Close

func (w *WriterInterceptor) Close()

Close closes the body readers and flags the interceptor as closed status.

func (*WriterInterceptor) DoWrite

func (w *WriterInterceptor) DoWrite() (int, error)

DoWrite writes the final HTTP response header and body in the real http.ResponseWriter.

func (*WriterInterceptor) Header

func (w *WriterInterceptor) Header() http.Header

Header returns the current response http.Header.

func (*WriterInterceptor) Write

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

Write intercepts and stores chunks of bytes as part of the response body.

func (*WriterInterceptor) WriteHeader

func (w *WriterInterceptor) WriteHeader(status int)

WriteHeader intercepts the desired response status code.

type XMLCharDecoder

type XMLCharDecoder func(charset string, input io.Reader) (io.Reader, error)

XMLCharDecoder is a helper type that takes a stream of bytes (not encoded in UTF-8) and returns a reader that encodes the bytes into UTF-8. This is done because Go's XML library only supports XML encoded in UTF-8.

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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