support

package module
v0.0.0-...-83f18d3 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2015 License: Apache-2.0 Imports: 13 Imported by: 0

README

Helper library for hijacking HTTP streams.

This library contains helper methods for hijacking HTTP connections (on both the client side and the server side) so the connection can be used to stream data. This is used in the context of swarm exec.

It is highly inspired by docker exec.

Client side usage:

hijackOpts := hijack.HijackHttpOptions{
    Method:             "POST",
    Url:                url,
    DockerTermProtocol: false, // If set to true, output & error stream will be multiplexed in the format of docker StdCopy, see https://github.com/docker/docker/blob/master/pkg/stdcopy/stdcopy.go
    InputStream:        myInputStream,
    OutputStream:       myOutputStream,
    ErrorStream:        myErrorStream,
    Data:               dataStructureThatWillBePostedAsJson,
    Header:             make(http.Header),
}
hijackOpts.Header.Set("User-Agent", "My user agent")
err := hijack.HijackHttpRequest(hijackOpts)
if err != nil {
    return Mask(err)
}

Server side usage:

inStream, outStream, err := hijack.HijackServer(res)
if err != nil {
    logger.Error("Hijack server streams failed: %v", err)
    return err
}
defer hijack.CloseStreams(inStream, outStream)

// Return HTTP response header, indicating a hijacking of the stream
// Response codes inspired by docker
if _, ok := req.Header["Upgrade"]; ok {
    fmt.Fprintf(outStream, "HTTP/1.1 101 UPGRADED\r\nContent-Type: application/vnd.docker.raw-stream\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n\r\n")
} else {
    fmt.Fprintf(outStream, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
}
// Stream data from/to inStream/outStream

License

Apache 2.0, see LICENSE

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingMethod = errors.New("Method not set")
	ErrMissingUrl    = errors.New("Url not set")
)

Functions

func CloseStreams

func CloseStreams(streams ...interface{})

func HijackHttpRequest

func HijackHttpRequest(options HijackHttpOptions) error

HijackHttpRequest performs an HTTP request with given method, url and data and hijacks the request (after a successful connection) to stream data from/to the given input, output and error streams.

func HijackServer

func HijackServer(w http.ResponseWriter) (io.ReadCloser, io.Writer, error)

Types

type HijackHttpOptions

type HijackHttpOptions struct {
	Method             string
	Url                string
	Host               string // If set, this will be passed as `Host` header to the request.
	DockerTermProtocol bool
	InputStream        io.Reader
	ErrorStream        io.Writer
	OutputStream       io.Writer
	Data               interface{}
	Header             http.Header
	Log                docker.Logger
	ErrorHandler       func(res *http.Response, err error) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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