Right, that was lovely, Roger. It does exactly what I asked -- only, it turns out that what I asked for wasn't exactly what I wanted. (At least a learnt a couple of things.)
Turns out my understanding of HTTP is stuck at 1.0, and with 1.1 the same TCP connection can be kept open between any two hosts for the whole session. So I can't detect individual "HTTP requests" on the connection, which is necessary. I'm therefore now using the http package to do essentially the same thing. I've worked out most of it:
package main
import (
"http"
"net"
)
type handler struct { }
func (h *handler) ServeHTTP(conn *http.Conn, req *http.Request) {
client_tcp_conn, _ := net.Dial("tcp", "", "
127.0.0.1:9999") // Open new TCP connection to the server
client_http_conn := http.NewClientConn(client_tcp_conn, nil) // Start a new HTTP connection on it
client_http_conn.Write(req) // Pass on the request
resp, _ := client_http_conn.Read() // Read back the reply
resp.Write(conn) // Write the reply to the original connection
client_http_conn.Close() // Close the connection to the server
}
func main() {
local, _ := net.Listen("tcp", "
0.0.0.0:9998")
h := new(handler)
http.Serve(local, h)
}
The one, probably trivial, problem with this is that the HTTP response is written to the connection as a response *body* -- i.e., my browser displays the raw HTTP response rather than parsing it :)
The problem presumably is that I'm doing "resp.Write(conn)" on a Conn from the http package, not tcp. I just can't find how to get at that underlying TCP connection.