Standard library / net/http/httputil

httputil.NewProxyClientConn()

Wrap a connection in the legacy client API that writes absolute-form proxy request targets. Legacy-only: use net/http Client or Server for new code. Expected stdout: 'http://demo.invalid/a\n'.

Formal Syntax & Structure

go syntaxgo
func NewProxyClientConn(c net.Conn, r *bufio.Reader) *ClientConn

Parameters & Attributes

NameTypeDescription
cnet.ConnConnection transferred to the legacy wrapper.
r*bufio.ReaderBuffered reader or underlying stream as specified by its type; nil selects the default for legacy connections.

Return Value

*ClientConn

Deprecated client wrapper using proxy-form request targets.

Working Example

Example Snippetgo
package main
import (  "bufio"  "fmt"  "net"  "net/http"  "net/http/httputil"  "time")
func main() {  c, s := net.Pipe()  defer c.Close()  defer s.Close()  check(c.SetDeadline(time.Now().Add(3 * time.Second)))  check(s.SetDeadline(time.Now().Add(3 * time.Second)))  cc := httputil.NewProxyClientConn(c, nil)  req := must(http.NewRequest("GET", "http://demo.invalid/a", nil))  line := make(chan string, 1)  go func() { r := must(http.ReadRequest(bufio.NewReader(s))); line <- r.RequestURI }()  check(cc.Write(req))  fmt.Println(<-line)  check(cc.Close())}
func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference