1
0
mirror of https://github.com/golang/go synced 2024-11-20 04:24:51 -07:00

cgi: set Request.TLS and Request.RemoteAddr for children

R=agl, eds, rsc1, rsc
CC=golang-dev
https://golang.org/cl/4432079
This commit is contained in:
Brad Fitzpatrick 2011-04-28 15:02:32 -07:00
parent 98945a2bad
commit 548c9c8624
2 changed files with 24 additions and 4 deletions

View 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
}

View 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) {