Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
89873e60
Commit
89873e60
authored
Jun 23, 2011
by
Brad Fitzpatrick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
http: buffer Request.Write
Fixes #1996 R=golang-dev, r CC=golang-dev
https://golang.org/cl/4639068
parent
65b036c3
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
8 deletions
+55
-8
src/pkg/http/client_test.go
src/pkg/http/client_test.go
+46
-0
src/pkg/http/request.go
src/pkg/http/request.go
+9
-8
No files found.
src/pkg/http/client_test.go
View file @
89873e60
...
...
@@ -12,6 +12,7 @@ import (
"http/httptest"
"io"
"io/ioutil"
"net"
"os"
"strconv"
"strings"
...
...
@@ -243,3 +244,48 @@ func TestStreamingGet(t *testing.T) {
t
.
Fatalf
(
"at end expected EOF, got %v"
,
err
)
}
}
type
writeCountingConn
struct
{
net
.
Conn
count
*
int
}
func
(
c
*
writeCountingConn
)
Write
(
p
[]
byte
)
(
int
,
os
.
Error
)
{
*
c
.
count
++
return
c
.
Conn
.
Write
(
p
)
}
// TestClientWrites verifies that client requests are buffered and we
// don't send a TCP packet per line of the http request + body.
func
TestClientWrites
(
t
*
testing
.
T
)
{
ts
:=
httptest
.
NewServer
(
HandlerFunc
(
func
(
w
ResponseWriter
,
r
*
Request
)
{
}))
defer
ts
.
Close
()
writes
:=
0
dialer
:=
func
(
netz
string
,
addr
string
)
(
net
.
Conn
,
os
.
Error
)
{
c
,
err
:=
net
.
Dial
(
netz
,
addr
)
if
err
==
nil
{
c
=
&
writeCountingConn
{
c
,
&
writes
}
}
return
c
,
err
}
c
:=
&
Client
{
Transport
:
&
Transport
{
Dial
:
dialer
}}
_
,
err
:=
c
.
Get
(
ts
.
URL
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
writes
!=
1
{
t
.
Errorf
(
"Get request did %d Write calls, want 1"
,
writes
)
}
writes
=
0
_
,
err
=
c
.
PostForm
(
ts
.
URL
,
Values
{
"foo"
:
{
"bar"
}})
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
writes
!=
1
{
t
.
Errorf
(
"Post request did %d Write calls, want 1"
,
writes
)
}
}
src/pkg/http/request.go
View file @
89873e60
...
...
@@ -304,10 +304,11 @@ func (req *Request) write(w io.Writer, usingProxy bool) os.Error {
}
}
fmt
.
Fprintf
(
w
,
"%s %s HTTP/1.1
\r\n
"
,
valueOrDefault
(
req
.
Method
,
"GET"
),
uri
)
bw
:=
bufio
.
NewWriter
(
w
)
fmt
.
Fprintf
(
bw
,
"%s %s HTTP/1.1
\r\n
"
,
valueOrDefault
(
req
.
Method
,
"GET"
),
uri
)
// Header lines
fmt
.
Fprintf
(
w
,
"Host: %s
\r\n
"
,
host
)
fmt
.
Fprintf
(
b
w
,
"Host: %s
\r\n
"
,
host
)
// Use the defaultUserAgent unless the Header contains one, which
// may be blank to not send the header.
...
...
@@ -318,7 +319,7 @@ func (req *Request) write(w io.Writer, usingProxy bool) os.Error {
}
}
if
userAgent
!=
""
{
fmt
.
Fprintf
(
w
,
"User-Agent: %s
\r\n
"
,
userAgent
)
fmt
.
Fprintf
(
b
w
,
"User-Agent: %s
\r\n
"
,
userAgent
)
}
// Process Body,ContentLength,Close,Trailer
...
...
@@ -326,25 +327,25 @@ func (req *Request) write(w io.Writer, usingProxy bool) os.Error {
if
err
!=
nil
{
return
err
}
err
=
tw
.
WriteHeader
(
w
)
err
=
tw
.
WriteHeader
(
b
w
)
if
err
!=
nil
{
return
err
}
// TODO: split long values? (If so, should share code with Conn.Write)
err
=
req
.
Header
.
WriteSubset
(
w
,
reqWriteExcludeHeader
)
err
=
req
.
Header
.
WriteSubset
(
b
w
,
reqWriteExcludeHeader
)
if
err
!=
nil
{
return
err
}
io
.
WriteString
(
w
,
"
\r\n
"
)
io
.
WriteString
(
b
w
,
"
\r\n
"
)
// Write body and trailer
err
=
tw
.
WriteBody
(
w
)
err
=
tw
.
WriteBody
(
b
w
)
if
err
!=
nil
{
return
err
}
bw
.
Flush
()
return
nil
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment