Skip to content

Commit 45d334e

Browse files
committed
net/http/cgi: allow CGI host to configure where child's stderr goes
Patch originally from Steven Hartland. Tweaked a bit & added a test. Fixes #7197 Change-Id: I09012b4674e7c641dba31a24e9758cedb898d3ee Reviewed-on: https://go-review.googlesource.com/21196 Reviewed-by: Andrew Gerrand <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]>
1 parent 093a9a1 commit 45d334e

3 files changed

Lines changed: 31 additions & 1 deletion

File tree

‎src/net/http/cgi/host.go‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type Handler struct {
5858
InheritEnv []string // environment variables to inherit from host, as "key"
5959
Logger *log.Logger // optional log for errors or nil to use log.Print
6060
Args []string // optional arguments to pass to child process
61+
Stderr io.Writer // optional stderr for the child process; nil means os.Stderr
6162

6263
// PathLocationHandler specifies the root http Handler that
6364
// should handle internal redirects when the CGI process
@@ -70,6 +71,13 @@ type Handler struct {
7071
PathLocationHandler http.Handler
7172
}
7273

74+
func (h *Handler) stderr() io.Writer {
75+
if h.Stderr != nil {
76+
return h.Stderr
77+
}
78+
return os.Stderr
79+
}
80+
7381
// removeLeadingDuplicates remove leading duplicate in environments.
7482
// It's possible to override environment like following.
7583
// cgi.Handler{
@@ -204,7 +212,7 @@ func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
204212
Args: append([]string{h.Path}, h.Args...),
205213
Dir: cwd,
206214
Env: env,
207-
Stderr: os.Stderr, // for now
215+
Stderr: h.stderr(),
208216
}
209217
if req.ContentLength != 0 {
210218
cmd.Stdin = req.Body

‎src/net/http/cgi/host_test.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package cgi
88

99
import (
1010
"bufio"
11+
"bytes"
1112
"fmt"
1213
"io"
1314
"net"
@@ -500,6 +501,23 @@ func TestEnvOverride(t *testing.T) {
500501
runCgiTest(t, h, "GET /test.cgi HTTP/1.0\nHost: example.com\n\n", expectedMap)
501502
}
502503

504+
func TestHandlerStderr(t *testing.T) {
505+
check(t)
506+
var stderr bytes.Buffer
507+
h := &Handler{
508+
Path: "testdata/test.cgi",
509+
Root: "/test.cgi",
510+
Stderr: &stderr,
511+
}
512+
513+
rw := httptest.NewRecorder()
514+
req := newRequest("GET /test.cgi?writestderr=1 HTTP/1.0\nHost: example.com\n\n")
515+
h.ServeHTTP(rw, req)
516+
if got, want := stderr.String(), "Hello, stderr!\n"; got != want {
517+
t.Errorf("Stderr = %q; want %q", got, want)
518+
}
519+
}
520+
503521
func TestRemoveLeadingDuplicates(t *testing.T) {
504522
tests := []struct {
505523
env []string

‎src/net/http/cgi/testdata/test.cgi‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ print "X-CGI-Pid: $$\r\n";
2323
print "X-Test-Header: X-Test-Value\r\n";
2424
print "\r\n";
2525

26+
if ($params->{"writestderr"}) {
27+
print STDERR "Hello, stderr!\n";
28+
}
29+
2630
if ($params->{"bigresponse"}) {
2731
# 17 MB, for OS X: golang.org/issue/4958
2832
for (1..(17 * 1024)) {

0 commit comments

Comments
 (0)