Skip to content

Commit 899cd04

Browse files
committed
net/http: add Request.RequestURI field
The new url.URL's parsing can be too canonicalizing for certain applications. By keeping the original request URI around, we give applications a gross escape hatch while keeping the URL package clean and simple for normal uses. (From a discussion with Gary Burd, Gustavo Niemeyer, and Russ Cox.) Fixes #2782 R=golang-dev, rsc, dsymonds CC=golang-dev https://golang.org/cl/5580044
1 parent 408f0b1 commit 899cd04

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

src/pkg/net/http/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ func send(req *Request, t RoundTripper) (resp *Response, err error) {
116116
return nil, errors.New("http: nil Request.URL")
117117
}
118118

119+
if req.RequestURI != "" {
120+
return nil, errors.New("http: Request.RequestURI can't be set in client requests.")
121+
}
122+
119123
// Most the callers of send (Get, Post, et al) don't need
120124
// Headers, leaving it uninitialized. We guarantee to the
121125
// Transport that this has been initialized, though.

src/pkg/net/http/client_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,3 +428,15 @@ func TestClientInsecureTransport(t *testing.T) {
428428
}
429429
}
430430
}
431+
432+
func TestClientErrorWithRequestURI(t *testing.T) {
433+
req, _ := NewRequest("GET", "http://localhost:1234/", nil)
434+
req.RequestURI = "/this/field/is/illegal/and/should/error/"
435+
_, err := DefaultClient.Do(req)
436+
if err == nil {
437+
t.Fatalf("expected an error")
438+
}
439+
if !strings.Contains(err.Error(), "RequestURI") {
440+
t.Errorf("wanted error mentioning RequestURI; got error: %v", err)
441+
}
442+
}

src/pkg/net/http/readrequest_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ var reqTests = []reqTest{
6464
Close: false,
6565
ContentLength: 7,
6666
Host: "www.techcrunch.com",
67+
RequestURI: "http://www.techcrunch.com/",
6768
},
6869

6970
"abcdef\n",
@@ -89,6 +90,7 @@ var reqTests = []reqTest{
8990
Close: false,
9091
ContentLength: 0,
9192
Host: "foo.com",
93+
RequestURI: "/",
9294
},
9395

9496
noBody,
@@ -114,6 +116,7 @@ var reqTests = []reqTest{
114116
Close: false,
115117
ContentLength: 0,
116118
Host: "test",
119+
RequestURI: "//user@host/is/actually/a/path/",
117120
},
118121

119122
noBody,
@@ -163,6 +166,7 @@ var reqTests = []reqTest{
163166
Header: Header{},
164167
ContentLength: -1,
165168
Host: "foo.com",
169+
RequestURI: "/",
166170
},
167171

168172
"foobar",
@@ -188,6 +192,7 @@ var reqTests = []reqTest{
188192
Close: false,
189193
ContentLength: 0,
190194
Host: "www.google.com:443",
195+
RequestURI: "www.google.com:443",
191196
},
192197

193198
noBody,
@@ -211,6 +216,7 @@ var reqTests = []reqTest{
211216
Close: false,
212217
ContentLength: 0,
213218
Host: "127.0.0.1:6060",
219+
RequestURI: "127.0.0.1:6060",
214220
},
215221

216222
noBody,
@@ -234,6 +240,7 @@ var reqTests = []reqTest{
234240
Close: false,
235241
ContentLength: 0,
236242
Host: "",
243+
RequestURI: "/_goRPC_",
237244
},
238245

239246
noBody,

src/pkg/net/http/request.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ type Request struct {
153153
// This field is ignored by the HTTP client.
154154
RemoteAddr string
155155

156+
// RequestURI is the unmodified Request-URI of the
157+
// Request-Line (RFC 2616, Section 5.1) as sent by the client
158+
// to a server. Usually the URL field should be used instead.
159+
// It is an error to set this field in an HTTP client request.
160+
RequestURI string
161+
156162
// TLS allows HTTP servers and other software to record
157163
// information about the TLS connection on which the request
158164
// was received. This field is not filled in by ReadRequest.
@@ -459,8 +465,8 @@ func ReadRequest(b *bufio.Reader) (req *Request, err error) {
459465
if f = strings.SplitN(s, " ", 3); len(f) < 3 {
460466
return nil, &badStringError{"malformed HTTP request", s}
461467
}
462-
var rawurl string
463-
req.Method, rawurl, req.Proto = f[0], f[1], f[2]
468+
req.Method, req.RequestURI, req.Proto = f[0], f[1], f[2]
469+
rawurl := req.RequestURI
464470
var ok bool
465471
if req.ProtoMajor, req.ProtoMinor, ok = ParseHTTPVersion(req.Proto); !ok {
466472
return nil, &badStringError{"malformed HTTP version", req.Proto}

0 commit comments

Comments
 (0)