httprevproxy

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2026 License: Apache-2.0, BSD-3-Clause Imports: 11 Imported by: 0

README

httprevproxy

httprevproxy is a TinyGo-compatible reverse proxy whose public API mirrors the reverse-proxy portion of net/http/httputil. The distinct package name keeps imports unambiguous. Applications that want a later one-line migration can import it with the httputil alias, then change only the import path:

import httputil "github.com/shibukawa/tinygodriver/httprevproxy"

target, _ := url.Parse("http://127.0.0.1:8080/base")
proxy := &httputil.ReverseProxy{
	Rewrite: func(r *httputil.ProxyRequest) {
		r.SetURL(target)
		r.SetXForwarded()
	},
}
http.Handle("/", proxy)

Supported behavior includes Rewrite, the deprecated Director, NewSingleHostReverseProxy, hop-by-hop header removal, forwarding helpers, response modification, error handling, trailers, bounded-buffer streaming, and periodic or immediate flushing.

Director does not work under TinyGo

Use Rewrite, as the example above does. TinyGo's HTTP client takes the dial address from Request.Host where standard net/http takes it from Request.URL.Host (see rule:tinygo-dials-request-host), so preserving the inbound Host — which is exactly what Director and NewSingleHostReverseProxy are for — means dialing it, and the proxy ends up calling itself.

Rewrite with SetURL is unaffected: SetURL leaves Out.Host empty, and this package fills it in from the target URL on the TinyGo path, which is the same Host header standard net/http would have written. A Rewrite that sets Out.Host itself is left alone. Only a Host that disagrees with the target URL is refused, and it is refused through ErrorHandler rather than looping.

The stdlib net/http/httputil.ReverseProxy that TinyGo 0.42 ships has the same problem and no such compensation.

Protocol upgrades (including WebSockets), 1xx forwarding, and HTTP/2-specific proxy features are intentionally unsupported. A protocol-upgrade request or response is passed to ErrorHandler and produces 502 with the default handler.

Documentation

Overview

Package httprevproxy provides a TinyGo-compatible subset of net/http/httputil's reverse proxy API.

The public API intentionally follows net/http/httputil so applications can migrate without redesigning proxy configuration when TinyGo supports the standard implementation. Protocol upgrades and 1xx forwarding are not supported.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BufferPool

type BufferPool interface {
	Get() []byte
	Put([]byte)
}

A BufferPool gets and returns temporary byte slices used when copying a response body.

type ProxyRequest

type ProxyRequest struct {
	// In is the request received by the proxy. Rewrite must not modify In.
	In *http.Request
	// Out is the request sent by the proxy. Rewrite may modify or replace Out.
	Out *http.Request
}

A ProxyRequest contains a request to be rewritten by a ReverseProxy.

func (*ProxyRequest) SetURL

func (r *ProxyRequest) SetURL(target *url.URL)

SetURL routes the outbound request to target, joining target's base path to the incoming path. It also makes the outbound Host header use target.Host.

func (*ProxyRequest) SetXForwarded

func (r *ProxyRequest) SetXForwarded()

SetXForwarded sets X-Forwarded-For, X-Forwarded-Host, and X-Forwarded-Proto from the inbound request. An existing X-Forwarded-For value on Out is preserved and appended to.

type ReverseProxy

type ReverseProxy struct {
	Rewrite        func(*ProxyRequest)
	Transport      http.RoundTripper
	FlushInterval  time.Duration
	ErrorLog       *log.Logger
	BufferPool     BufferPool
	ModifyResponse func(*http.Response) error
	ErrorHandler   func(http.ResponseWriter, *http.Request, error)

	// Director is deprecated. Use Rewrite instead. Exactly one of Director or
	// Rewrite must be set.
	Director func(*http.Request)
}

ReverseProxy is an HTTP Handler that proxies requests to another server. Its public fields match net/http/httputil.ReverseProxy. This implementation does not support protocol upgrades or forwarding 1xx responses.

func NewSingleHostReverseProxy

func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy

NewSingleHostReverseProxy returns a proxy that routes requests to target. Like net/http/httputil, it uses the deprecated Director field for backwards compatibility and preserves the inbound Host header.

That last part is why this constructor does not work under TinyGo, whose transport dials Request.Host rather than Request.URL.Host: preserving the inbound Host means dialing it, which sends the proxy back to its own address. It reports an error there rather than looping. Use Rewrite with ProxyRequest.SetURL instead, which every other example in this package does.

func (*ReverseProxy) ServeHTTP

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

ServeHTTP implements http.Handler.

Jump to

Keyboard shortcuts

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