Skip to content

Commit daea278

Browse files
committed
add stderr
1 parent 05a6fbd commit daea278

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

client.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (this *Client) KeepAlive() {
5858
this.keepAlive = true
5959
}
6060

61-
func (this *Client) Call(req *Request) (*http.Response, error) {
61+
func (this *Client) Call(req *Request) (resp *http.Response, stderr []byte, err error) {
6262
this.isFree = false
6363

6464
this.locker.Lock()
@@ -67,7 +67,7 @@ func (this *Client) Call(req *Request) (*http.Response, error) {
6767
err := this.Connect()
6868
if err != nil {
6969
this.locker.Unlock()
70-
return nil, err
70+
return nil, nil, err
7171
}
7272
}
7373

@@ -84,13 +84,13 @@ func (this *Client) Call(req *Request) (*http.Response, error) {
8484
}()
8585

8686
if this.conn == nil {
87-
return nil, errors.New("no connection to server")
87+
return nil, nil, errors.New("no connection to server")
8888
}
8989

9090
if req.timeout > 0 {
9191
this.beforeTime(req.timeout)
9292
}
93-
resp, err := req.CallOn(this.conn)
93+
resp, stderr, err = req.CallOn(this.conn)
9494
this.endTime()
9595

9696
// 如果失去连接,则重新连接
@@ -105,7 +105,7 @@ func (this *Client) Call(req *Request) (*http.Response, error) {
105105
if req.timeout > 0 {
106106
this.beforeTime(req.timeout)
107107
}
108-
resp, err = req.CallOn(this.conn)
108+
resp, stderr, err = req.CallOn(this.conn)
109109
this.endTime()
110110
} else {
111111
log.Println("[gofcgi]again:" + err.Error())
@@ -114,7 +114,7 @@ func (this *Client) Call(req *Request) (*http.Response, error) {
114114
}
115115
}
116116

117-
return resp, err
117+
return resp, stderr, err
118118
}
119119

120120
func (this *Client) Close() {

client_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,15 @@ func TestClientGet(t *testing.T) {
3838
"REQUEST_METHOD": "GET",
3939
})
4040

41-
resp, err := client.Call(req)
41+
resp, stderr, err := client.Call(req)
4242
if err != nil {
4343
t.Fatal("do error:", err.Error())
4444
}
4545

46+
if len(stderr) > 0 {
47+
t.Fatal("stderr:", string(stderr))
48+
}
49+
4650
t.Log("resp, status:", resp.StatusCode)
4751
t.Log("resp, status message:", resp.Status)
4852
t.Log("resp headers:", resp.Header)
@@ -85,7 +89,7 @@ func TestClientGetAlive(t *testing.T) {
8589
"REQUEST_METHOD": "GET",
8690
})
8791

88-
resp, err := client.Call(req)
92+
resp, _, err := client.Call(req)
8993
if err != nil {
9094
t.Fatal("do error:", err.Error())
9195
}
@@ -138,7 +142,7 @@ func TestClientPost(t *testing.T) {
138142
//req.SetParam("CONTENT_LENGTH", fmt.Sprintf("%d", r.Len()))
139143
req.SetBody(r, uint32(r.Len()))
140144

141-
resp, err := client.Call(req)
145+
resp, _, err := client.Call(req)
142146
if err != nil {
143147
t.Fatal("do error:", err.Error())
144148
}
@@ -196,7 +200,7 @@ func TestClientPerformance(t *testing.T) {
196200
"REQUEST_METHOD": "GET",
197201
})
198202

199-
resp, err := client.Call(req)
203+
resp, _, err := client.Call(req)
200204
if err != nil {
201205
locker.Lock()
202206
countFail++

request.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,20 @@ func (this *Request) SetTimeout(timeout time.Duration) {
6363
this.timeout = timeout
6464
}
6565

66-
func (this *Request) CallOn(conn net.Conn) (*http.Response, error) {
67-
err := this.writeBeginRequest(conn)
66+
func (this *Request) CallOn(conn net.Conn) (resp *http.Response, stderr []byte, err error) {
67+
err = this.writeBeginRequest(conn)
6868
if err != nil {
69-
return nil, err
69+
return nil, nil, err
7070
}
7171

7272
err = this.writeParams(conn)
7373
if err != nil {
74-
return nil, err
74+
return nil, nil, err
7575
}
7676

7777
err = this.writeStdin(conn)
7878
if err != nil {
79-
return nil, err
79+
return nil, nil, err
8080
}
8181

8282
return this.readStdout(conn)
@@ -187,15 +187,14 @@ func (this *Request) writeRecord(conn net.Conn, recordType byte, contentData []b
187187
return nil
188188
}
189189

190-
func (this *Request) readStdout(conn net.Conn) (*http.Response, error) {
190+
func (this *Request) readStdout(conn net.Conn) (resp *http.Response, stderr []byte, err error) {
191191
stdout := []byte{}
192-
stderr := []byte{}
193192

194193
for {
195194
respHeader := Header{}
196195
err := binary.Read(conn, binary.BigEndian, &respHeader)
197196
if err != nil {
198-
return nil, ErrClientDisconnect
197+
return nil, nil, ErrClientDisconnect
199198
}
200199

201200
// 检查ID是否一致
@@ -207,7 +206,7 @@ func (this *Request) readStdout(conn net.Conn) (*http.Response, error) {
207206
err = binary.Read(conn, binary.BigEndian, &b)
208207
if err != nil {
209208
log.Println("err:", err.Error())
210-
return nil, ErrClientDisconnect
209+
return nil, nil, ErrClientDisconnect
211210
}
212211

213212
if respHeader.Type == FCGI_STDOUT {
@@ -243,7 +242,7 @@ func (this *Request) readStdout(conn net.Conn) (*http.Response, error) {
243242
resp, err := http.ReadResponse(bufio.NewReader(bytes.NewReader(statusStdout)), nil)
244243

245244
if err != nil {
246-
return nil, err
245+
return nil, stderr, err
247246
}
248247

249248
if !foundStatus {
@@ -263,14 +262,14 @@ func (this *Request) readStdout(conn net.Conn) (*http.Response, error) {
263262
}
264263
}
265264

266-
return resp, nil
265+
return resp, stderr, nil
267266
}
268267

269268
if len(stderr) > 0 {
270-
return nil, errors.New("fcgi:" + string(stderr))
269+
return nil, stderr, errors.New("fcgi:" + string(stderr))
271270
}
272271

273-
return nil, errors.New("no response from server")
272+
return nil, stderr, errors.New("no response from server")
274273
}
275274

276275
func (this *Request) nextId() uint16 {

0 commit comments

Comments
 (0)