Skip to content

Commit

Permalink
net/http/cgi: allow CGI host to configure where child's stderr goes
Browse files Browse the repository at this point in the history
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 <adg@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
  • Loading branch information
bradfitz committed Mar 29, 2016
1 parent 093a9a1 commit 45d334e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/net/http/cgi/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Handler struct {
InheritEnv []string // environment variables to inherit from host, as "key"
Logger *log.Logger // optional log for errors or nil to use log.Print
Args []string // optional arguments to pass to child process
Stderr io.Writer // optional stderr for the child process; nil means os.Stderr

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

func (h *Handler) stderr() io.Writer {
if h.Stderr != nil {
return h.Stderr
}
return os.Stderr
}

// removeLeadingDuplicates remove leading duplicate in environments.
// It's possible to override environment like following.
// cgi.Handler{
Expand Down Expand Up @@ -204,7 +212,7 @@ func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
Args: append([]string{h.Path}, h.Args...),
Dir: cwd,
Env: env,
Stderr: os.Stderr, // for now
Stderr: h.stderr(),
}
if req.ContentLength != 0 {
cmd.Stdin = req.Body
Expand Down
18 changes: 18 additions & 0 deletions src/net/http/cgi/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package cgi

import (
"bufio"
"bytes"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -500,6 +501,23 @@ func TestEnvOverride(t *testing.T) {
runCgiTest(t, h, "GET /test.cgi HTTP/1.0\nHost: example.com\n\n", expectedMap)
}

func TestHandlerStderr(t *testing.T) {
check(t)
var stderr bytes.Buffer
h := &Handler{
Path: "testdata/test.cgi",
Root: "/test.cgi",
Stderr: &stderr,
}

rw := httptest.NewRecorder()
req := newRequest("GET /test.cgi?writestderr=1 HTTP/1.0\nHost: example.com\n\n")
h.ServeHTTP(rw, req)
if got, want := stderr.String(), "Hello, stderr!\n"; got != want {
t.Errorf("Stderr = %q; want %q", got, want)
}
}

func TestRemoveLeadingDuplicates(t *testing.T) {
tests := []struct {
env []string
Expand Down
4 changes: 4 additions & 0 deletions src/net/http/cgi/testdata/test.cgi
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ print "X-CGI-Pid: $$\r\n";
print "X-Test-Header: X-Test-Value\r\n";
print "\r\n";

if ($params->{"writestderr"}) {
print STDERR "Hello, stderr!\n";
}

if ($params->{"bigresponse"}) {
# 17 MB, for OS X: golang.org/issue/4958
for (1..(17 * 1024)) {
Expand Down

0 comments on commit 45d334e

Please sign in to comment.