]> Cypherpunks repositories - gostls13.git/commitdiff
http: move RemoteAddr & UsingTLS from ResponseWriter to Request
authorBrad Fitzpatrick <bradfitz@golang.org>
Thu, 10 Mar 2011 16:17:22 +0000 (08:17 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 10 Mar 2011 16:17:22 +0000 (08:17 -0800)
ResponseWriter.RemoteAddr() string -> Request.RemoteAddr string
ResponseWriter.UsingTLS() bool -> Request.TLS *tls.ConnectionState

R=rsc, bradfitzwork
CC=gburd, golang-dev
https://golang.org/cl/4248075

src/cmd/godoc/main.go
src/pkg/http/cgi/host.go
src/pkg/http/cgi/host_test.go
src/pkg/http/httptest/recorder.go
src/pkg/http/request.go
src/pkg/http/serve_test.go
src/pkg/http/server.go
src/pkg/rpc/server.go
src/pkg/websocket/server.go

index f32a5b91459052f395cf6b187ff420076b7a19c0..c6dd6ded0e8d10ee5c7360dd98ba78f70e2f1a8c 100644 (file)
@@ -152,7 +152,7 @@ func usage() {
 
 func loggingHandler(h http.Handler) http.Handler {
        return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
-               log.Printf("%s\t%s", w.RemoteAddr(), req.URL)
+               log.Printf("%s\t%s", req.RemoteAddr, req.URL)
                h.ServeHTTP(w, req)
        })
 }
index d6c8ab22a11f9e91fafc8d2feebdcf86dde7e1d7..227238737495544232bb115f63bb25f7287eeb1b 100644 (file)
@@ -74,11 +74,15 @@ func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
                "PATH_INFO=" + pathInfo,
                "SCRIPT_NAME=" + root,
                "SCRIPT_FILENAME=" + h.Path,
-               "REMOTE_ADDR=" + rw.RemoteAddr(),
-               "REMOTE_HOST=" + rw.RemoteAddr(),
+               "REMOTE_ADDR=" + req.RemoteAddr,
+               "REMOTE_HOST=" + req.RemoteAddr,
                "SERVER_PORT=" + port,
        }
 
+       if req.TLS != nil {
+               env = append(env, "HTTPS=on")
+       }
+
        if len(req.Cookie) > 0 {
                b := new(bytes.Buffer)
                for idx, c := range req.Cookie {
index 2db08d5429830cee9731dbcbfc80c7f3e016ab05..9980356736c5d2d4e25211ba06cb6233bac3e44d 100644 (file)
@@ -37,6 +37,7 @@ func newRequest(httpreq string) *http.Request {
        if err != nil {
                panic("cgi: bogus http request in test: " + httpreq)
        }
+       req.RemoteAddr = "1.2.3.4"
        return req
 }
 
index 22827a31db1b42f8f5cbdacc07dc77fc4cf9623c..8d70c2834a130dc5d97fd155a0bb62fb7fc49fe3 100644 (file)
@@ -14,12 +14,10 @@ import (
 // ResponseRecorder is an implementation of http.ResponseWriter that
 // records its mutations for later inspection in tests.
 type ResponseRecorder struct {
-       Code           int           // the HTTP response code from WriteHeader
-       HeaderMap      http.Header   // the HTTP response headers
-       Body           *bytes.Buffer // if non-nil, the bytes.Buffer to append written data to
-       Flushed        bool
-       FakeRemoteAddr string // the fake RemoteAddr to return, or "" for DefaultRemoteAddr
-       FakeUsingTLS   bool   // whether to return true from the UsingTLS method
+       Code      int           // the HTTP response code from WriteHeader
+       HeaderMap http.Header   // the HTTP response headers
+       Body      *bytes.Buffer // if non-nil, the bytes.Buffer to append written data to
+       Flushed   bool
 }
 
 // NewRecorder returns an initialized ResponseRecorder.
@@ -34,20 +32,6 @@ func NewRecorder() *ResponseRecorder {
 // an explicit DefaultRemoteAddr isn't set on ResponseRecorder.
 const DefaultRemoteAddr = "1.2.3.4"
 
-// RemoteAddr returns the value of rw.FakeRemoteAddr, if set, else
-// returns DefaultRemoteAddr.
-func (rw *ResponseRecorder) RemoteAddr() string {
-       if rw.FakeRemoteAddr != "" {
-               return rw.FakeRemoteAddr
-       }
-       return DefaultRemoteAddr
-}
-
-// UsingTLS returns the fake value in rw.FakeUsingTLS
-func (rw *ResponseRecorder) UsingTLS() bool {
-       return rw.FakeUsingTLS
-}
-
 // Header returns the response headers.
 func (rw *ResponseRecorder) Header() http.Header {
        return rw.HeaderMap
index d8456bab3217353fb2935a861a6ff1100ef3d926..d82894fab08829716e6411d5dba7ee46be574eb5 100644 (file)
@@ -11,6 +11,7 @@ package http
 
 import (
        "bufio"
+       "crypto/tls"
        "container/vector"
        "fmt"
        "io"
@@ -137,6 +138,22 @@ type Request struct {
        // response has multiple trailer lines with the same key, they will be
        // concatenated, delimited by commas.
        Trailer Header
+
+       // RemoteAddr allows HTTP servers and other software to record
+       // the network address that sent the request, usually for
+       // logging. This field is not filled in by ReadRequest and
+       // has no defined format. The HTTP server in this package
+       // sets RemoteAddr to an "IP:port" address before invoking a
+       // handler.
+       RemoteAddr string
+
+       // TLS allows HTTP servers and other software to record
+       // information about the TLS connection on which the request
+       // was received. This field is not filled in by ReadRequest.
+       // The HTTP server in this package sets the field for
+       // TLS-enabled connections before invoking a handler;
+       // otherwise it leaves the field nil.
+       TLS *tls.ConnectionState
 }
 
 // ProtoAtLeast returns whether the HTTP protocol used
index a6d3cab09d46a581bbb7c302a94bbec7ccbf7db6..482acfd314176a57de4c3aa0f3234f1554db2a8b 100644 (file)
@@ -229,6 +229,7 @@ func TestMuxRedirectLeadingSlashes(t *testing.T) {
 }
 
 func TestServerTimeouts(t *testing.T) {
+       // TODO(bradfitz): convert this to use httptest.Server
        l, err := net.ListenTCP("tcp", &net.TCPAddr{Port: 0})
        if err != nil {
                t.Fatalf("listen error: %v", err)
@@ -406,3 +407,23 @@ func TestServeHTTP10Close(t *testing.T) {
 
        success <- true
 }
+
+func TestSetsRemoteAddr(t *testing.T) {
+       ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
+               fmt.Fprintf(w, "%s", r.RemoteAddr)
+       }))
+       defer ts.Close()
+
+       res, _, err := Get(ts.URL)
+       if err != nil {
+               t.Fatalf("Get error: %v", err)
+       }
+       body, err := ioutil.ReadAll(res.Body)
+       if err != nil {
+               t.Fatalf("ReadAll error: %v", err)
+       }
+       ip := string(body)
+       if !strings.HasPrefix(ip, "127.0.0.1:") && !strings.HasPrefix(ip, "[::1]:") {
+               t.Fatalf("Expected local addr; got %q", ip)
+       }
+}
index 5f36af5484cee5abfdebf089f257c15d02403e11..6a7c74efb039498823728a3ea931744a12b211d4 100644 (file)
@@ -48,12 +48,6 @@ type Handler interface {
 // A ResponseWriter interface is used by an HTTP handler to
 // construct an HTTP response.
 type ResponseWriter interface {
-       // RemoteAddr returns the address of the client that sent the current request
-       RemoteAddr() string
-
-       // UsingTLS returns true if the client is connected using TLS
-       UsingTLS() bool
-
        // Header returns the header map that will be sent by WriteHeader.
        // Changing the header after a call to WriteHeader (or Write) has
        // no effect.
@@ -97,12 +91,12 @@ type Hijacker interface {
 
 // A conn represents the server side of an HTTP connection.
 type conn struct {
-       remoteAddr string            // network address of remote side
-       handler    Handler           // request handler
-       rwc        net.Conn          // i/o connection
-       buf        *bufio.ReadWriter // buffered rwc
-       hijacked   bool              // connection has been hijacked by handler
-       usingTLS   bool              // a flag indicating connection over TLS
+       remoteAddr string               // network address of remote side
+       handler    Handler              // request handler
+       rwc        net.Conn             // i/o connection
+       buf        *bufio.ReadWriter    // buffered rwc
+       hijacked   bool                 // connection has been hijacked by handler
+       tlsState   *tls.ConnectionState // or nil when not using TLS        
 }
 
 // A response represents the server side of an HTTP response.
@@ -130,10 +124,15 @@ func newConn(rwc net.Conn, handler Handler) (c *conn, err os.Error) {
        c.remoteAddr = rwc.RemoteAddr().String()
        c.handler = handler
        c.rwc = rwc
-       _, c.usingTLS = rwc.(*tls.Conn)
        br := bufio.NewReader(rwc)
        bw := bufio.NewWriter(rwc)
        c.buf = bufio.NewReadWriter(br, bw)
+
+       if tlsConn, ok := rwc.(*tls.Conn); ok {
+               c.tlsState = new(tls.ConnectionState)
+               *c.tlsState = tlsConn.ConnectionState()
+       }
+
        return c, nil
 }
 
@@ -173,6 +172,9 @@ func (c *conn) readRequest() (w *response, err os.Error) {
                return nil, err
        }
 
+       req.RemoteAddr = c.remoteAddr
+       req.TLS = c.tlsState
+
        w = new(response)
        w.conn = c
        w.req = req
@@ -187,12 +189,6 @@ func (c *conn) readRequest() (w *response, err os.Error) {
        return w, nil
 }
 
-func (w *response) UsingTLS() bool {
-       return w.conn.usingTLS
-}
-
-func (w *response) RemoteAddr() string { return w.conn.remoteAddr }
-
 func (w *response) Header() Header {
        return w.header
 }
index 6dd962d81f6162df022a00a1c9ed31df97367e7c..59ebaf4a80821d308d884aeaf635bfe4687c3717 100644 (file)
@@ -514,7 +514,7 @@ func (server *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
        }
        conn, _, err := w.(http.Hijacker).Hijack()
        if err != nil {
-               log.Print("rpc hijacking ", w.RemoteAddr(), ": ", err.String())
+               log.Print("rpc hijacking ", req.RemoteAddr, ": ", err.String())
                return
        }
        io.WriteString(conn, "HTTP/1.0 "+connected+"\n\n")
index 37149f044d45325275fff2ce6695809d0aa4fbcb..1119b2d34ebdf906e14f0a96e962a891352d09c5 100644 (file)
@@ -98,7 +98,7 @@ func (f Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
        }
 
        var location string
-       if w.UsingTLS() {
+       if req.TLS != nil {
                location = "wss://" + req.Host + req.URL.RawPath
        } else {
                location = "ws://" + req.Host + req.URL.RawPath
@@ -192,7 +192,7 @@ func (f Draft75Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
        defer rwc.Close()
 
        var location string
-       if w.UsingTLS() {
+       if req.TLS != nil {
                location = "wss://" + req.Host + req.URL.RawPath
        } else {
                location = "ws://" + req.Host + req.URL.RawPath