httprevproxy

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 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.

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.

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