httpunix

package module
v0.0.0-...-2ba4b9c Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2019 License: MIT Imports: 6 Imported by: 86

Documentation

Overview

Package httpunix provides a HTTP transport (net/http.RoundTripper) that uses Unix domain sockets instead of HTTP.

This is useful for non-browser connections within the same host, as it allows using the file system for credentials of both client and server, and guaranteeing unique names.

The URLs look like this:

http+unix://LOCATION/PATH_ETC

where LOCATION is translated to a file system path with Transport.RegisterLocation, and PATH_ETC follow normal http: scheme conventions.

Example (ClientIntegrated)
package main

import (
	"fmt"
	"log"
	"net/http"
	"net/http/httputil"
	"time"

	"github.com/tv42/httpunix"
)

func main() {
	// This example shows handling all net/http requests for the
	// http+unix URL scheme.
	u := &httpunix.Transport{
		DialTimeout:           100 * time.Millisecond,
		RequestTimeout:        1 * time.Second,
		ResponseHeaderTimeout: 1 * time.Second,
	}
	u.RegisterLocation("myservice", "/path/to/socket")

	// If you want to use http: with the same client:
	t := &http.Transport{}
	t.RegisterProtocol(httpunix.Scheme, u)
	var client = http.Client{
		Transport: t,
	}

	resp, err := client.Get("http+unix://myservice/urlpath/as/seen/by/server")
	if err != nil {
		log.Fatal(err)
	}
	buf, err := httputil.DumpResponse(resp, true)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s", buf)
	resp.Body.Close()
}
Output:

Example (ClientStandalone)
package main

import (
	"fmt"
	"log"
	"net/http"
	"net/http/httputil"
	"time"

	"github.com/tv42/httpunix"
)

func main() {
	// This example shows using a customized http.Client.
	u := &httpunix.Transport{
		DialTimeout:           100 * time.Millisecond,
		RequestTimeout:        1 * time.Second,
		ResponseHeaderTimeout: 1 * time.Second,
	}
	u.RegisterLocation("myservice", "/path/to/socket")

	var client = http.Client{
		Transport: u,
	}

	resp, err := client.Get("http+unix://myservice/urlpath/as/seen/by/server")
	if err != nil {
		log.Fatal(err)
	}
	buf, err := httputil.DumpResponse(resp, true)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s", buf)
	resp.Body.Close()
}
Output:

Example (Server)
package main

import (
	"log"
	"net"
	"net/http"
)

func main() {
	l, err := net.Listen("unix", "/path/to/socket")
	if err != nil {
		log.Fatal(err)
	}
	defer l.Close()

	if err := http.Serve(l, nil); err != nil {
		log.Fatal(err)
	}
}
Output:

Index

Examples

Constants

View Source
const Scheme = "http+unix"

Scheme is the URL scheme used for HTTP over UNIX domain sockets.

Variables

This section is empty.

Functions

This section is empty.

Types

type Transport

type Transport struct {
	// DialTimeout is deprecated. Use context instead.
	DialTimeout time.Duration
	// RequestTimeout is deprecated and has no effect.
	RequestTimeout time.Duration
	// ResponseHeaderTimeout is deprecated. Use context instead.
	ResponseHeaderTimeout time.Duration
	// contains filtered or unexported fields
}

Transport is a http.RoundTripper that connects to Unix domain sockets.

func (*Transport) RegisterLocation

func (t *Transport) RegisterLocation(loc string, path string)

RegisterLocation registers an URL location and maps it to the given file system path.

Calling RegisterLocation twice for the same location is a programmer error, and causes a panic.

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip executes a single HTTP transaction. See net/http.RoundTripper.

Jump to

Keyboard shortcuts

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