]> Cypherpunks repositories - gostls13.git/commitdiff
cgi: set Request.TLS and Request.RemoteAddr for children
authorBrad Fitzpatrick <bradfitz@golang.org>
Thu, 28 Apr 2011 22:02:32 +0000 (15:02 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 28 Apr 2011 22:02:32 +0000 (15:02 -0700)
R=agl, eds, rsc1, rsc
CC=golang-dev
https://golang.org/cl/4432079

src/pkg/http/cgi/child.go
src/pkg/http/cgi/child_test.go

index 85b5ae6b276d7acd6ed9e1e9a13e381dd859174a..760d1179b83ee613ec89946cc6f266908c1e9823 100644 (file)
@@ -9,10 +9,12 @@ package cgi
 
 import (
        "bufio"
+       "crypto/tls"
        "fmt"
        "http"
        "io"
        "io/ioutil"
+       "net"
        "os"
        "strconv"
        "strings"
@@ -21,6 +23,7 @@ import (
 // Request returns the HTTP request as represented in the current
 // environment. This assumes the current program is being run
 // by a web server in a CGI environment.
+// The returned Request's Body is populated, if applicable.
 func Request() (*http.Request, os.Error) {
        r, err := RequestFromMap(envMap(os.Environ()))
        if err != nil {
@@ -50,6 +53,7 @@ var skipHeader = map[string]bool{
 }
 
 // RequestFromMap creates an http.Request from CGI variables.
+// The returned Request's Body field is not populated.
 func RequestFromMap(params map[string]string) (*http.Request, os.Error) {
        r := new(http.Request)
        r.Method = params["REQUEST_METHOD"]
@@ -110,6 +114,18 @@ func RequestFromMap(params map[string]string) (*http.Request, os.Error) {
                }
                r.URL = url
        }
+
+       // There's apparently a de-facto standard for this.
+       // http://docstore.mik.ua/orelly/linux/cgi/ch03_02.htm#ch03-35636
+       if s := params["HTTPS"]; s == "on" || s == "ON" || s == "1" {
+               r.TLS = &tls.ConnectionState{HandshakeComplete: true}
+       }
+
+       // Request.RemoteAddr has its port set by Go's standard http
+       // server, so we do here too. We don't have one, though, so we
+       // use a dummy one.
+       r.RemoteAddr = net.JoinHostPort(params["REMOTE_ADDR"], "0")
+
        return r, nil
 }
 
@@ -148,10 +164,6 @@ func (r *response) Flush() {
        r.bufw.Flush()
 }
 
-func (r *response) RemoteAddr() string {
-       return os.Getenv("REMOTE_ADDR")
-}
-
 func (r *response) Header() http.Header {
        return r.header
 }
index 6a885e10f0c34a124625855e8eae339c773f1d1f..87d3f79a0cb843da31fb46fb1909c49c83b89a7a 100644 (file)
@@ -20,6 +20,8 @@ func TestRequest(t *testing.T) {
                "HTTP_FOO_BAR":    "baz",
                "REQUEST_URI":     "/path?a=b",
                "CONTENT_LENGTH":  "123",
+               "HTTPS":           "1",
+               "REMOTE_ADDR":     "5.6.7.8",
        }
        req, err := RequestFromMap(env)
        if err != nil {
@@ -59,6 +61,12 @@ func TestRequest(t *testing.T) {
        if req.Trailer == nil {
                t.Errorf("unexpected nil Trailer")
        }
+       if req.TLS == nil {
+               t.Errorf("expected non-nil TLS")
+       }
+       if e, g := "5.6.7.8:0", req.RemoteAddr; e != g {
+               t.Errorf("RemoteAddr: got %q; want %q", g, e)
+       }
 }
 
 func TestRequestWithoutHost(t *testing.T) {