aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.8.1/libgo/go/net/http/cgi
diff options
context:
space:
mode:
Diffstat (limited to 'gcc-4.8.1/libgo/go/net/http/cgi')
-rw-r--r--gcc-4.8.1/libgo/go/net/http/cgi/child.go201
-rw-r--r--gcc-4.8.1/libgo/go/net/http/cgi/child_test.go109
-rw-r--r--gcc-4.8.1/libgo/go/net/http/cgi/host.go350
-rw-r--r--gcc-4.8.1/libgo/go/net/http/cgi/host_test.go467
-rw-r--r--gcc-4.8.1/libgo/go/net/http/cgi/matryoshka_test.go93
-rw-r--r--gcc-4.8.1/libgo/go/net/http/cgi/testdata/test.cgi90
6 files changed, 0 insertions, 1310 deletions
diff --git a/gcc-4.8.1/libgo/go/net/http/cgi/child.go b/gcc-4.8.1/libgo/go/net/http/cgi/child.go
deleted file mode 100644
index 100b8b777..000000000
--- a/gcc-4.8.1/libgo/go/net/http/cgi/child.go
+++ /dev/null
@@ -1,201 +0,0 @@
-// Copyright 2011 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-// This file implements CGI from the perspective of a child
-// process.
-
-package cgi
-
-import (
- "bufio"
- "crypto/tls"
- "errors"
- "fmt"
- "io"
- "io/ioutil"
- "net"
- "net/http"
- "net/url"
- "os"
- "strconv"
- "strings"
-)
-
-// Request returns the HTTP request as represented in the current
-// environment. This assumes the current program is being run
-// by a web server in a CGI environment.
-// The returned Request's Body is populated, if applicable.
-func Request() (*http.Request, error) {
- r, err := RequestFromMap(envMap(os.Environ()))
- if err != nil {
- return nil, err
- }
- if r.ContentLength > 0 {
- r.Body = ioutil.NopCloser(io.LimitReader(os.Stdin, r.ContentLength))
- }
- return r, nil
-}
-
-func envMap(env []string) map[string]string {
- m := make(map[string]string)
- for _, kv := range env {
- if idx := strings.Index(kv, "="); idx != -1 {
- m[kv[:idx]] = kv[idx+1:]
- }
- }
- return m
-}
-
-// RequestFromMap creates an http.Request from CGI variables.
-// The returned Request's Body field is not populated.
-func RequestFromMap(params map[string]string) (*http.Request, error) {
- r := new(http.Request)
- r.Method = params["REQUEST_METHOD"]
- if r.Method == "" {
- return nil, errors.New("cgi: no REQUEST_METHOD in environment")
- }
-
- r.Proto = params["SERVER_PROTOCOL"]
- var ok bool
- r.ProtoMajor, r.ProtoMinor, ok = http.ParseHTTPVersion(r.Proto)
- if !ok {
- return nil, errors.New("cgi: invalid SERVER_PROTOCOL version")
- }
-
- r.Close = true
- r.Trailer = http.Header{}
- r.Header = http.Header{}
-
- r.Host = params["HTTP_HOST"]
-
- if lenstr := params["CONTENT_LENGTH"]; lenstr != "" {
- clen, err := strconv.ParseInt(lenstr, 10, 64)
- if err != nil {
- return nil, errors.New("cgi: bad CONTENT_LENGTH in environment: " + lenstr)
- }
- r.ContentLength = clen
- }
-
- if ct := params["CONTENT_TYPE"]; ct != "" {
- r.Header.Set("Content-Type", ct)
- }
-
- // Copy "HTTP_FOO_BAR" variables to "Foo-Bar" Headers
- for k, v := range params {
- if !strings.HasPrefix(k, "HTTP_") || k == "HTTP_HOST" {
- continue
- }
- r.Header.Add(strings.Replace(k[5:], "_", "-", -1), v)
- }
-
- // TODO: cookies. parsing them isn't exported, though.
-
- uriStr := params["REQUEST_URI"]
- if uriStr == "" {
- // Fallback to SCRIPT_NAME, PATH_INFO and QUERY_STRING.
- uriStr = params["SCRIPT_NAME"] + params["PATH_INFO"]
- s := params["QUERY_STRING"]
- if s != "" {
- uriStr += "?" + s
- }
- }
- if r.Host != "" {
- // Hostname is provided, so we can reasonably construct a URL,
- // even if we have to assume 'http' for the scheme.
- rawurl := "http://" + r.Host + uriStr
- url, err := url.Parse(rawurl)
- if err != nil {
- return nil, errors.New("cgi: failed to parse host and REQUEST_URI into a URL: " + rawurl)
- }
- r.URL = url
- }
- // Fallback logic if we don't have a Host header or the URL
- // failed to parse
- if r.URL == nil {
- url, err := url.Parse(uriStr)
- if err != nil {
- return nil, errors.New("cgi: failed to parse REQUEST_URI into a URL: " + uriStr)
- }
- r.URL = url
- }
-
- // There's apparently a de-facto standard for this.
- // http://docstore.mik.ua/orelly/linux/cgi/ch03_02.htm#ch03-35636
- if s := params["HTTPS"]; s == "on" || s == "ON" || s == "1" {
- r.TLS = &tls.ConnectionState{HandshakeComplete: true}
- }
-
- // Request.RemoteAddr has its port set by Go's standard http
- // server, so we do here too. We don't have one, though, so we
- // use a dummy one.
- r.RemoteAddr = net.JoinHostPort(params["REMOTE_ADDR"], "0")
-
- return r, nil
-}
-
-// Serve executes the provided Handler on the currently active CGI
-// request, if any. If there's no current CGI environment
-// an error is returned. The provided handler may be nil to use
-// http.DefaultServeMux.
-func Serve(handler http.Handler) error {
- req, err := Request()
- if err != nil {
- return err
- }
- if handler == nil {
- handler = http.DefaultServeMux
- }
- rw := &response{
- req: req,
- header: make(http.Header),
- bufw: bufio.NewWriter(os.Stdout),
- }
- handler.ServeHTTP(rw, req)
- rw.Write(nil) // make sure a response is sent
- if err = rw.bufw.Flush(); err != nil {
- return err
- }
- return nil
-}
-
-type response struct {
- req *http.Request
- header http.Header
- bufw *bufio.Writer
- headerSent bool
-}
-
-func (r *response) Flush() {
- r.bufw.Flush()
-}
-
-func (r *response) Header() http.Header {
- return r.header
-}
-
-func (r *response) Write(p []byte) (n int, err error) {
- if !r.headerSent {
- r.WriteHeader(http.StatusOK)
- }
- return r.bufw.Write(p)
-}
-
-func (r *response) WriteHeader(code int) {
- if r.headerSent {
- // Note: explicitly using Stderr, as Stdout is our HTTP output.
- fmt.Fprintf(os.Stderr, "CGI attempted to write header twice on request for %s", r.req.URL)
- return
- }
- r.headerSent = true
- fmt.Fprintf(r.bufw, "Status: %d %s\r\n", code, http.StatusText(code))
-
- // Set a default Content-Type
- if _, hasType := r.header["Content-Type"]; !hasType {
- r.header.Add("Content-Type", "text/html; charset=utf-8")
- }
-
- r.header.Write(r.bufw)
- r.bufw.WriteString("\r\n")
- r.bufw.Flush()
-}
diff --git a/gcc-4.8.1/libgo/go/net/http/cgi/child_test.go b/gcc-4.8.1/libgo/go/net/http/cgi/child_test.go
deleted file mode 100644
index 74e068014..000000000
--- a/gcc-4.8.1/libgo/go/net/http/cgi/child_test.go
+++ /dev/null
@@ -1,109 +0,0 @@
-// Copyright 2011 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-// Tests for CGI (the child process perspective)
-
-package cgi
-
-import (
- "testing"
-)
-
-func TestRequest(t *testing.T) {
- env := map[string]string{
- "SERVER_PROTOCOL": "HTTP/1.1",
- "REQUEST_METHOD": "GET",
- "HTTP_HOST": "example.com",
- "HTTP_REFERER": "elsewhere",
- "HTTP_USER_AGENT": "goclient",
- "HTTP_FOO_BAR": "baz",
- "REQUEST_URI": "/path?a=b",
- "CONTENT_LENGTH": "123",
- "CONTENT_TYPE": "text/xml",
- "HTTPS": "1",
- "REMOTE_ADDR": "5.6.7.8",
- }
- req, err := RequestFromMap(env)
- if err != nil {
- t.Fatalf("RequestFromMap: %v", err)
- }
- if g, e := req.UserAgent(), "goclient"; e != g {
- t.Errorf("expected UserAgent %q; got %q", e, g)
- }
- if g, e := req.Method, "GET"; e != g {
- t.Errorf("expected Method %q; got %q", e, g)
- }
- if g, e := req.Header.Get("Content-Type"), "text/xml"; e != g {
- t.Errorf("expected Content-Type %q; got %q", e, g)
- }
- if g, e := req.ContentLength, int64(123); e != g {
- t.Errorf("expected ContentLength %d; got %d", e, g)
- }
- if g, e := req.Referer(), "elsewhere"; e != g {
- t.Errorf("expected Referer %q; got %q", e, g)
- }
- if req.Header == nil {
- t.Fatalf("unexpected nil Header")
- }
- if g, e := req.Header.Get("Foo-Bar"), "baz"; e != g {
- t.Errorf("expected Foo-Bar %q; got %q", e, g)
- }
- if g, e := req.URL.String(), "http://example.com/path?a=b"; e != g {
- t.Errorf("expected URL %q; got %q", e, g)
- }
- if g, e := req.FormValue("a"), "b"; e != g {
- t.Errorf("expected FormValue(a) %q; got %q", e, g)
- }
- if req.Trailer == nil {
- t.Errorf("unexpected nil Trailer")
- }
- if req.TLS == nil {
- t.Errorf("expected non-nil TLS")
- }
- if e, g := "5.6.7.8:0", req.RemoteAddr; e != g {
- t.Errorf("RemoteAddr: got %q; want %q", g, e)
- }
-}
-
-func TestRequestWithoutHost(t *testing.T) {
- env := map[string]string{
- "SERVER_PROTOCOL": "HTTP/1.1",
- "HTTP_HOST": "",
- "REQUEST_METHOD": "GET",
- "REQUEST_URI": "/path?a=b",
- "CONTENT_LENGTH": "123",
- }
- req, err := RequestFromMap(env)
- if err != nil {
- t.Fatalf("RequestFromMap: %v", err)
- }
- if req.URL == nil {
- t.Fatalf("unexpected nil URL")
- }
- if g, e := req.URL.String(), "/path?a=b"; e != g {
- t.Errorf("URL = %q; want %q", g, e)
- }
-}
-
-func TestRequestWithoutRequestURI(t *testing.T) {
- env := map[string]string{
- "SERVER_PROTOCOL": "HTTP/1.1",
- "HTTP_HOST": "example.com",
- "REQUEST_METHOD": "GET",
- "SCRIPT_NAME": "/dir/scriptname",
- "PATH_INFO": "/p1/p2",
- "QUERY_STRING": "a=1&b=2",
- "CONTENT_LENGTH": "123",
- }
- req, err := RequestFromMap(env)
- if err != nil {
- t.Fatalf("RequestFromMap: %v", err)
- }
- if req.URL == nil {
- t.Fatalf("unexpected nil URL")
- }
- if g, e := req.URL.String(), "http://example.com/dir/scriptname/p1/p2?a=1&b=2"; e != g {
- t.Errorf("URL = %q; want %q", g, e)
- }
-}
diff --git a/gcc-4.8.1/libgo/go/net/http/cgi/host.go b/gcc-4.8.1/libgo/go/net/http/cgi/host.go
deleted file mode 100644
index d27cc4dc9..000000000
--- a/gcc-4.8.1/libgo/go/net/http/cgi/host.go
+++ /dev/null
@@ -1,350 +0,0 @@
-// Copyright 2011 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-// This file implements the host side of CGI (being the webserver
-// parent process).
-
-// Package cgi implements CGI (Common Gateway Interface) as specified
-// in RFC 3875.
-//
-// Note that using CGI means starting a new process to handle each
-// request, which is typically less efficient than using a
-// long-running server. This package is intended primarily for
-// compatibility with existing systems.
-package cgi
-
-import (
- "bufio"
- "fmt"
- "io"
- "log"
- "net/http"
- "os"
- "os/exec"
- "path/filepath"
- "regexp"
- "runtime"
- "strconv"
- "strings"
-)
-
-var trailingPort = regexp.MustCompile(`:([0-9]+)$`)
-
-var osDefaultInheritEnv = map[string][]string{
- "darwin": {"DYLD_LIBRARY_PATH"},
- "freebsd": {"LD_LIBRARY_PATH"},
- "hpux": {"LD_LIBRARY_PATH", "SHLIB_PATH"},
- "irix": {"LD_LIBRARY_PATH", "LD_LIBRARYN32_PATH", "LD_LIBRARY64_PATH"},
- "linux": {"LD_LIBRARY_PATH"},
- "openbsd": {"LD_LIBRARY_PATH"},
- "solaris": {"LD_LIBRARY_PATH", "LD_LIBRARY_PATH_32", "LD_LIBRARY_PATH_64"},
- "windows": {"SystemRoot", "COMSPEC", "PATHEXT", "WINDIR"},
-}
-
-// Handler runs an executable in a subprocess with a CGI environment.
-type Handler struct {
- Path string // path to the CGI executable
- Root string // root URI prefix of handler or empty for "/"
-
- // Dir specifies the CGI executable's working directory.
- // If Dir is empty, the base directory of Path is used.
- // If Path has no base directory, the current working
- // directory is used.
- Dir string
-
- Env []string // extra environment variables to set, if any, as "key=value"
- 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
-
- // PathLocationHandler specifies the root http Handler that
- // should handle internal redirects when the CGI process
- // returns a Location header value starting with a "/", as
- // specified in RFC 3875 ยง 6.3.2. This will likely be
- // http.DefaultServeMux.
- //
- // If nil, a CGI response with a local URI path is instead sent
- // back to the client and not redirected internally.
- PathLocationHandler http.Handler
-}
-
-// removeLeadingDuplicates remove leading duplicate in environments.
-// It's possible to override environment like following.
-// cgi.Handler{
-// ...
-// Env: []string{"SCRIPT_FILENAME=foo.php"},
-// }
-func removeLeadingDuplicates(env []string) (ret []string) {
- n := len(env)
- for i := 0; i < n; i++ {
- e := env[i]
- s := strings.SplitN(e, "=", 2)[0]
- found := false
- for j := i + 1; j < n; j++ {
- if s == strings.SplitN(env[j], "=", 2)[0] {
- found = true
- break
- }
- }
- if !found {
- ret = append(ret, e)
- }
- }
- return
-}
-
-func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
- root := h.Root
- if root == "" {
- root = "/"
- }
-
- if len(req.TransferEncoding) > 0 && req.TransferEncoding[0] == "chunked" {
- rw.WriteHeader(http.StatusBadRequest)
- rw.Write([]byte("Chunked request bodies are not supported by CGI."))
- return
- }
-
- pathInfo := req.URL.Path
- if root != "/" && strings.HasPrefix(pathInfo, root) {
- pathInfo = pathInfo[len(root):]
- }
-
- port := "80"
- if matches := trailingPort.FindStringSubmatch(req.Host); len(matches) != 0 {
- port = matches[1]
- }
-
- env := []string{
- "SERVER_SOFTWARE=go",
- "SERVER_NAME=" + req.Host,
- "SERVER_PROTOCOL=HTTP/1.1",
- "HTTP_HOST=" + req.Host,
- "GATEWAY_INTERFACE=CGI/1.1",
- "REQUEST_METHOD=" + req.Method,
- "QUERY_STRING=" + req.URL.RawQuery,
- "REQUEST_URI=" + req.URL.RequestURI(),
- "PATH_INFO=" + pathInfo,
- "SCRIPT_NAME=" + root,
- "SCRIPT_FILENAME=" + h.Path,
- "REMOTE_ADDR=" + req.RemoteAddr,
- "REMOTE_HOST=" + req.RemoteAddr,
- "SERVER_PORT=" + port,
- }
-
- if req.TLS != nil {
- env = append(env, "HTTPS=on")
- }
-
- for k, v := range req.Header {
- k = strings.Map(upperCaseAndUnderscore, k)
- joinStr := ", "
- if k == "COOKIE" {
- joinStr = "; "
- }
- env = append(env, "HTTP_"+k+"="+strings.Join(v, joinStr))
- }
-
- if req.ContentLength > 0 {
- env = append(env, fmt.Sprintf("CONTENT_LENGTH=%d", req.ContentLength))
- }
- if ctype := req.Header.Get("Content-Type"); ctype != "" {
- env = append(env, "CONTENT_TYPE="+ctype)
- }
-
- if h.Env != nil {
- env = append(env, h.Env...)
- }
-
- envPath := os.Getenv("PATH")
- if envPath == "" {
- envPath = "/bin:/usr/bin:/usr/ucb:/usr/bsd:/usr/local/bin"
- }
- env = append(env, "PATH="+envPath)
-
- for _, e := range h.InheritEnv {
- if v := os.Getenv(e); v != "" {
- env = append(env, e+"="+v)
- }
- }
-
- for _, e := range osDefaultInheritEnv[runtime.GOOS] {
- if v := os.Getenv(e); v != "" {
- env = append(env, e+"="+v)
- }
- }
-
- env = removeLeadingDuplicates(env)
-
- var cwd, path string
- if h.Dir != "" {
- path = h.Path
- cwd = h.Dir
- } else {
- cwd, path = filepath.Split(h.Path)
- }
- if cwd == "" {
- cwd = "."
- }
-
- internalError := func(err error) {
- rw.WriteHeader(http.StatusInternalServerError)
- h.printf("CGI error: %v", err)
- }
-
- cmd := &exec.Cmd{
- Path: path,
- Args: append([]string{h.Path}, h.Args...),
- Dir: cwd,
- Env: env,
- Stderr: os.Stderr, // for now
- }
- if req.ContentLength != 0 {
- cmd.Stdin = req.Body
- }
- stdoutRead, err := cmd.StdoutPipe()
- if err != nil {
- internalError(err)
- return
- }
-
- err = cmd.Start()
- if err != nil {
- internalError(err)
- return
- }
- defer cmd.Wait()
- defer stdoutRead.Close()
-
- linebody := bufio.NewReaderSize(stdoutRead, 1024)
- headers := make(http.Header)
- statusCode := 0
- for {
- line, isPrefix, err := linebody.ReadLine()
- if isPrefix {
- rw.WriteHeader(http.StatusInternalServerError)
- h.printf("cgi: long header line from subprocess.")
- return
- }
- if err == io.EOF {
- break
- }
- if err != nil {
- rw.WriteHeader(http.StatusInternalServerError)
- h.printf("cgi: error reading headers: %v", err)
- return
- }
- if len(line) == 0 {
- break
- }
- parts := strings.SplitN(string(line), ":", 2)
- if len(parts) < 2 {
- h.printf("cgi: bogus header line: %s", string(line))
- continue
- }
- header, val := parts[0], parts[1]
- header = strings.TrimSpace(header)
- val = strings.TrimSpace(val)
- switch {
- case header == "Status":
- if len(val) < 3 {
- h.printf("cgi: bogus status (short): %q", val)
- return
- }
- code, err := strconv.Atoi(val[0:3])
- if err != nil {
- h.printf("cgi: bogus status: %q", val)
- h.printf("cgi: line was %q", line)
- return
- }
- statusCode = code
- default:
- headers.Add(header, val)
- }
- }
-
- if loc := headers.Get("Location"); loc != "" {
- if strings.HasPrefix(loc, "/") && h.PathLocationHandler != nil {
- h.handleInternalRedirect(rw, req, loc)
- return
- }
- if statusCode == 0 {
- statusCode = http.StatusFound
- }
- }
-
- if statusCode == 0 {
- statusCode = http.StatusOK
- }
-
- // Copy headers to rw's headers, after we've decided not to
- // go into handleInternalRedirect, which won't want its rw
- // headers to have been touched.
- for k, vv := range headers {
- for _, v := range vv {
- rw.Header().Add(k, v)
- }
- }
-
- rw.WriteHeader(statusCode)
-
- _, err = io.Copy(rw, linebody)
- if err != nil {
- h.printf("cgi: copy error: %v", err)
- }
-}
-
-func (h *Handler) printf(format string, v ...interface{}) {
- if h.Logger != nil {
- h.Logger.Printf(format, v...)
- } else {
- log.Printf(format, v...)
- }
-}
-
-func (h *Handler) handleInternalRedirect(rw http.ResponseWriter, req *http.Request, path string) {
- url, err := req.URL.Parse(path)
- if err != nil {
- rw.WriteHeader(http.StatusInternalServerError)
- h.printf("cgi: error resolving local URI path %q: %v", path, err)
- return
- }
- // TODO: RFC 3875 isn't clear if only GET is supported, but it
- // suggests so: "Note that any message-body attached to the
- // request (such as for a POST request) may not be available
- // to the resource that is the target of the redirect." We
- // should do some tests against Apache to see how it handles
- // POST, HEAD, etc. Does the internal redirect get the same
- // method or just GET? What about incoming headers?
- // (e.g. Cookies) Which headers, if any, are copied into the
- // second request?
- newReq := &http.Request{
- Method: "GET",
- URL: url,
- Proto: "HTTP/1.1",
- ProtoMajor: 1,
- ProtoMinor: 1,
- Header: make(http.Header),
- Host: url.Host,
- RemoteAddr: req.RemoteAddr,
- TLS: req.TLS,
- }
- h.PathLocationHandler.ServeHTTP(rw, newReq)
-}
-
-func upperCaseAndUnderscore(r rune) rune {
- switch {
- case r >= 'a' && r <= 'z':
- return r - ('a' - 'A')
- case r == '-':
- return '_'
- case r == '=':
- // Maybe not part of the CGI 'spec' but would mess up
- // the environment in any case, as Go represents the
- // environment as a slice of "key=value" strings.
- return '_'
- }
- // TODO: other transformations in spec or practice?
- return r
-}
diff --git a/gcc-4.8.1/libgo/go/net/http/cgi/host_test.go b/gcc-4.8.1/libgo/go/net/http/cgi/host_test.go
deleted file mode 100644
index cb6f1df1f..000000000
--- a/gcc-4.8.1/libgo/go/net/http/cgi/host_test.go
+++ /dev/null
@@ -1,467 +0,0 @@
-// Copyright 2011 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-// Tests for package cgi
-
-package cgi
-
-import (
- "bufio"
- "fmt"
- "io"
- "net"
- "net/http"
- "net/http/httptest"
- "os"
- "os/exec"
- "path/filepath"
- "runtime"
- "strconv"
- "strings"
- "syscall"
- "testing"
- "time"
-)
-
-func newRequest(httpreq string) *http.Request {
- buf := bufio.NewReader(strings.NewReader(httpreq))
- req, err := http.ReadRequest(buf)
- if err != nil {
- panic("cgi: bogus http request in test: " + httpreq)
- }
- req.RemoteAddr = "1.2.3.4"
- return req
-}
-
-func runCgiTest(t *testing.T, h *Handler, httpreq string, expectedMap map[string]string) *httptest.ResponseRecorder {
- rw := httptest.NewRecorder()
- req := newRequest(httpreq)
- h.ServeHTTP(rw, req)
-
- // Make a map to hold the test map that the CGI returns.
- m := make(map[string]string)
- m["_body"] = rw.Body.String()
- linesRead := 0
-readlines:
- for {
- line, err := rw.Body.ReadString('\n')
- switch {
- case err == io.EOF:
- break readlines
- case err != nil:
- t.Fatalf("unexpected error reading from CGI: %v", err)
- }
- linesRead++
- trimmedLine := strings.TrimRight(line, "\r\n")
- split := strings.SplitN(trimmedLine, "=", 2)
- if len(split) != 2 {
- t.Fatalf("Unexpected %d parts from invalid line number %v: %q; existing map=%v",
- len(split), linesRead, line, m)
- }
- m[split[0]] = split[1]
- }
-
- for key, expected := range expectedMap {
- got := m[key]
- if key == "cwd" {
- // For Windows. golang.org/issue/4645.
- fi1, _ := os.Stat(got)
- fi2, _ := os.Stat(expected)
- if os.SameFile(fi1, fi2) {
- got = expected
- }
- }
- if got != expected {
- t.Errorf("for key %q got %q; expected %q", key, got, expected)
- }
- }
- return rw
-}
-
-var cgiTested, cgiWorks bool
-
-func check(t *testing.T) {
- if !cgiTested {
- cgiTested = true
- cgiWorks = exec.Command("./testdata/test.cgi").Run() == nil
- }
- if !cgiWorks {
- // No Perl on Windows, needed by test.cgi
- // TODO: make the child process be Go, not Perl.
- t.Skip("Skipping test: test.cgi failed.")
- }
-}
-
-func TestCGIBasicGet(t *testing.T) {
- check(t)
- h := &Handler{
- Path: "testdata/test.cgi",
- Root: "/test.cgi",
- }
- expectedMap := map[string]string{
- "test": "Hello CGI",
- "param-a": "b",
- "param-foo": "bar",
- "env-GATEWAY_INTERFACE": "CGI/1.1",
- "env-HTTP_HOST": "example.com",
- "env-PATH_INFO": "",
- "env-QUERY_STRING": "foo=bar&a=b",
- "env-REMOTE_ADDR": "1.2.3.4",
- "env-REMOTE_HOST": "1.2.3.4",
- "env-REQUEST_METHOD": "GET",
- "env-REQUEST_URI": "/test.cgi?foo=bar&a=b",
- "env-SCRIPT_FILENAME": "testdata/test.cgi",
- "env-SCRIPT_NAME": "/test.cgi",
- "env-SERVER_NAME": "example.com",
- "env-SERVER_PORT": "80",
- "env-SERVER_SOFTWARE": "go",
- }
- replay := runCgiTest(t, h, "GET /test.cgi?foo=bar&a=b HTTP/1.0\nHost: example.com\n\n", expectedMap)
-
- if expected, got := "text/html", replay.Header().Get("Content-Type"); got != expected {
- t.Errorf("got a Content-Type of %q; expected %q", got, expected)
- }
- if expected, got := "X-Test-Value", replay.Header().Get("X-Test-Header"); got != expected {
- t.Errorf("got a X-Test-Header of %q; expected %q", got, expected)
- }
-}
-
-func TestCGIBasicGetAbsPath(t *testing.T) {
- check(t)
- pwd, err := os.Getwd()
- if err != nil {
- t.Fatalf("getwd error: %v", err)
- }
- h := &Handler{
- Path: pwd + "/testdata/test.cgi",
- Root: "/test.cgi",
- }
- expectedMap := map[string]string{
- "env-REQUEST_URI": "/test.cgi?foo=bar&a=b",
- "env-SCRIPT_FILENAME": pwd + "/testdata/test.cgi",
- "env-SCRIPT_NAME": "/test.cgi",
- }
- runCgiTest(t, h, "GET /test.cgi?foo=bar&a=b HTTP/1.0\nHost: example.com\n\n", expectedMap)
-}
-
-func TestPathInfo(t *testing.T) {
- check(t)
- h := &Handler{
- Path: "testdata/test.cgi",
- Root: "/test.cgi",
- }
- expectedMap := map[string]string{
- "param-a": "b",
- "env-PATH_INFO": "/extrapath",
- "env-QUERY_STRING": "a=b",
- "env-REQUEST_URI": "/test.cgi/extrapath?a=b",
- "env-SCRIPT_FILENAME": "testdata/test.cgi",
- "env-SCRIPT_NAME": "/test.cgi",
- }
- runCgiTest(t, h, "GET /test.cgi/extrapath?a=b HTTP/1.0\nHost: example.com\n\n", expectedMap)
-}
-
-func TestPathInfoDirRoot(t *testing.T) {
- check(t)
- h := &Handler{
- Path: "testdata/test.cgi",
- Root: "/myscript/",
- }
- expectedMap := map[string]string{
- "env-PATH_INFO": "bar",
- "env-QUERY_STRING": "a=b",
- "env-REQUEST_URI": "/myscript/bar?a=b",
- "env-SCRIPT_FILENAME": "testdata/test.cgi",
- "env-SCRIPT_NAME": "/myscript/",
- }
- runCgiTest(t, h, "GET /myscript/bar?a=b HTTP/1.0\nHost: example.com\n\n", expectedMap)
-}
-
-func TestDupHeaders(t *testing.T) {
- check(t)
- h := &Handler{
- Path: "testdata/test.cgi",
- }
- expectedMap := map[string]string{
- "env-REQUEST_URI": "/myscript/bar?a=b",
- "env-SCRIPT_FILENAME": "testdata/test.cgi",
- "env-HTTP_COOKIE": "nom=NOM; yum=YUM",
- "env-HTTP_X_FOO": "val1, val2",
- }
- runCgiTest(t, h, "GET /myscript/bar?a=b HTTP/1.0\n"+
- "Cookie: nom=NOM\n"+
- "Cookie: yum=YUM\n"+
- "X-Foo: val1\n"+
- "X-Foo: val2\n"+
- "Host: example.com\n\n",
- expectedMap)
-}
-
-func TestPathInfoNoRoot(t *testing.T) {
- check(t)
- h := &Handler{
- Path: "testdata/test.cgi",
- Root: "",
- }
- expectedMap := map[string]string{
- "env-PATH_INFO": "/bar",
- "env-QUERY_STRING": "a=b",
- "env-REQUEST_URI": "/bar?a=b",
- "env-SCRIPT_FILENAME": "testdata/test.cgi",
- "env-SCRIPT_NAME": "/",
- }
- runCgiTest(t, h, "GET /bar?a=b HTTP/1.0\nHost: example.com\n\n", expectedMap)
-}
-
-func TestCGIBasicPost(t *testing.T) {
- check(t)
- postReq := `POST /test.cgi?a=b HTTP/1.0
-Host: example.com
-Content-Type: application/x-www-form-urlencoded
-Content-Length: 15
-
-postfoo=postbar`
- h := &Handler{
- Path: "testdata/test.cgi",
- Root: "/test.cgi",
- }
- expectedMap := map[string]string{
- "test": "Hello CGI",
- "param-postfoo": "postbar",
- "env-REQUEST_METHOD": "POST",
- "env-CONTENT_LENGTH": "15",
- "env-REQUEST_URI": "/test.cgi?a=b",
- }
- runCgiTest(t, h, postReq, expectedMap)
-}
-
-func chunk(s string) string {
- return fmt.Sprintf("%x\r\n%s\r\n", len(s), s)
-}
-
-// The CGI spec doesn't allow chunked requests.
-func TestCGIPostChunked(t *testing.T) {
- check(t)
- postReq := `POST /test.cgi?a=b HTTP/1.1
-Host: example.com
-Content-Type: application/x-www-form-urlencoded
-Transfer-Encoding: chunked
-
-` + chunk("postfoo") + chunk("=") + chunk("postbar") + chunk("")
-
- h := &Handler{
- Path: "testdata/test.cgi",
- Root: "/test.cgi",
- }
- expectedMap := map[string]string{}
- resp := runCgiTest(t, h, postReq, expectedMap)
- if got, expected := resp.Code, http.StatusBadRequest; got != expected {
- t.Fatalf("Expected %v response code from chunked request body; got %d",
- expected, got)
- }
-}
-
-func TestRedirect(t *testing.T) {
- check(t)
- h := &Handler{
- Path: "testdata/test.cgi",
- Root: "/test.cgi",
- }
- rec := runCgiTest(t, h, "GET /test.cgi?loc=http://foo.com/ HTTP/1.0\nHost: example.com\n\n", nil)
- if e, g := 302, rec.Code; e != g {
- t.Errorf("expected status code %d; got %d", e, g)
- }
- if e, g := "http://foo.com/", rec.Header().Get("Location"); e != g {
- t.Errorf("expected Location header of %q; got %q", e, g)
- }
-}
-
-func TestInternalRedirect(t *testing.T) {
- check(t)
- baseHandler := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
- fmt.Fprintf(rw, "basepath=%s\n", req.URL.Path)
- fmt.Fprintf(rw, "remoteaddr=%s\n", req.RemoteAddr)
- })
- h := &Handler{
- Path: "testdata/test.cgi",
- Root: "/test.cgi",
- PathLocationHandler: baseHandler,
- }
- expectedMap := map[string]string{
- "basepath": "/foo",
- "remoteaddr": "1.2.3.4",
- }
- runCgiTest(t, h, "GET /test.cgi?loc=/foo HTTP/1.0\nHost: example.com\n\n", expectedMap)
-}
-
-// TestCopyError tests that we kill the process if there's an error copying
-// its output. (for example, from the client having gone away)
-func TestCopyError(t *testing.T) {
- check(t)
- if runtime.GOOS == "windows" {
- t.Skipf("skipping test on %q", runtime.GOOS)
- }
- h := &Handler{
- Path: "testdata/test.cgi",
- Root: "/test.cgi",
- }
- ts := httptest.NewServer(h)
- defer ts.Close()
-
- conn, err := net.Dial("tcp", ts.Listener.Addr().String())
- if err != nil {
- t.Fatal(err)
- }
- req, _ := http.NewRequest("GET", "http://example.com/test.cgi?bigresponse=1", nil)
- err = req.Write(conn)
- if err != nil {
- t.Fatalf("Write: %v", err)
- }
-
- res, err := http.ReadResponse(bufio.NewReader(conn), req)
- if err != nil {
- t.Fatalf("ReadResponse: %v", err)
- }
-
- pidstr := res.Header.Get("X-CGI-Pid")
- if pidstr == "" {
- t.Fatalf("expected an X-CGI-Pid header in response")
- }
- pid, err := strconv.Atoi(pidstr)
- if err != nil {
- t.Fatalf("invalid X-CGI-Pid value")
- }
-
- var buf [5000]byte
- n, err := io.ReadFull(res.Body, buf[:])
- if err != nil {
- t.Fatalf("ReadFull: %d bytes, %v", n, err)
- }
-
- childRunning := func() bool {
- p, err := os.FindProcess(pid)
- if err != nil {
- return false
- }
- return p.Signal(syscall.Signal(0)) == nil
- }
-
- if !childRunning() {
- t.Fatalf("pre-conn.Close, expected child to be running")
- }
- conn.Close()
-
- tries := 0
- for tries < 25 && childRunning() {
- time.Sleep(50 * time.Millisecond * time.Duration(tries))
- tries++
- }
- if childRunning() {
- t.Fatalf("post-conn.Close, expected child to be gone")
- }
-}
-
-func TestDirUnix(t *testing.T) {
- check(t)
- if runtime.GOOS == "windows" {
- t.Skipf("skipping test on %q", runtime.GOOS)
- }
- cwd, _ := os.Getwd()
- h := &Handler{
- Path: "testdata/test.cgi",
- Root: "/test.cgi",
- Dir: cwd,
- }
- expectedMap := map[string]string{
- "cwd": cwd,
- }
- runCgiTest(t, h, "GET /test.cgi HTTP/1.0\nHost: example.com\n\n", expectedMap)
-
- cwd, _ = os.Getwd()
- cwd = filepath.Join(cwd, "testdata")
- h = &Handler{
- Path: "testdata/test.cgi",
- Root: "/test.cgi",
- }
- abswd, _ := filepath.EvalSymlinks(cwd)
- expectedMap = map[string]string{
- "cwd": abswd,
- }
- runCgiTest(t, h, "GET /test.cgi HTTP/1.0\nHost: example.com\n\n", expectedMap)
-}
-
-func TestDirWindows(t *testing.T) {
- if runtime.GOOS != "windows" {
- t.Skip("Skipping windows specific test.")
- }
-
- cgifile, _ := filepath.Abs("testdata/test.cgi")
-
- var perl string
- var err error
- perl, err = exec.LookPath("perl")
- if err != nil {
- t.Skip("Skipping test: perl not found.")
- }
- perl, _ = filepath.Abs(perl)
-
- cwd, _ := os.Getwd()
- h := &Handler{
- Path: perl,
- Root: "/test.cgi",
- Dir: cwd,
- Args: []string{cgifile},
- Env: []string{"SCRIPT_FILENAME=" + cgifile},
- }
- expectedMap := map[string]string{
- "cwd": cwd,
- }
- runCgiTest(t, h, "GET /test.cgi HTTP/1.0\nHost: example.com\n\n", expectedMap)
-
- // If not specify Dir on windows, working directory should be
- // base directory of perl.
- cwd, _ = filepath.Split(perl)
- if cwd != "" && cwd[len(cwd)-1] == filepath.Separator {
- cwd = cwd[:len(cwd)-1]
- }
- h = &Handler{
- Path: perl,
- Root: "/test.cgi",
- Args: []string{cgifile},
- Env: []string{"SCRIPT_FILENAME=" + cgifile},
- }
- expectedMap = map[string]string{
- "cwd": cwd,
- }
- runCgiTest(t, h, "GET /test.cgi HTTP/1.0\nHost: example.com\n\n", expectedMap)
-}
-
-func TestEnvOverride(t *testing.T) {
- cgifile, _ := filepath.Abs("testdata/test.cgi")
-
- var perl string
- var err error
- perl, err = exec.LookPath("perl")
- if err != nil {
- t.Skipf("Skipping test: perl not found.")
- }
- perl, _ = filepath.Abs(perl)
-
- cwd, _ := os.Getwd()
- h := &Handler{
- Path: perl,
- Root: "/test.cgi",
- Dir: cwd,
- Args: []string{cgifile},
- Env: []string{
- "SCRIPT_FILENAME=" + cgifile,
- "REQUEST_URI=/foo/bar"},
- }
- expectedMap := map[string]string{
- "cwd": cwd,
- "env-SCRIPT_FILENAME": cgifile,
- "env-REQUEST_URI": "/foo/bar",
- }
- runCgiTest(t, h, "GET /test.cgi HTTP/1.0\nHost: example.com\n\n", expectedMap)
-}
diff --git a/gcc-4.8.1/libgo/go/net/http/cgi/matryoshka_test.go b/gcc-4.8.1/libgo/go/net/http/cgi/matryoshka_test.go
deleted file mode 100644
index e1a78c8f6..000000000
--- a/gcc-4.8.1/libgo/go/net/http/cgi/matryoshka_test.go
+++ /dev/null
@@ -1,93 +0,0 @@
-// Copyright 2011 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-// Tests a Go CGI program running under a Go CGI host process.
-// Further, the two programs are the same binary, just checking
-// their environment to figure out what mode to run in.
-
-package cgi
-
-import (
- "fmt"
- "net/http"
- "os"
- "testing"
-)
-
-// This test is a CGI host (testing host.go) that runs its own binary
-// as a child process testing the other half of CGI (child.go).
-func TestHostingOurselves(t *testing.T) {
- h := &Handler{
- Path: os.Args[0],
- Root: "/test.go",
- Args: []string{"-test.run=TestBeChildCGIProcess"},
- }
- expectedMap := map[string]string{
- "test": "Hello CGI-in-CGI",
- "param-a": "b",
- "param-foo": "bar",
- "env-GATEWAY_INTERFACE": "CGI/1.1",
- "env-HTTP_HOST": "example.com",
- "env-PATH_INFO": "",
- "env-QUERY_STRING": "foo=bar&a=b",
- "env-REMOTE_ADDR": "1.2.3.4",
- "env-REMOTE_HOST": "1.2.3.4",
- "env-REQUEST_METHOD": "GET",
- "env-REQUEST_URI": "/test.go?foo=bar&a=b",
- "env-SCRIPT_FILENAME": os.Args[0],
- "env-SCRIPT_NAME": "/test.go",
- "env-SERVER_NAME": "example.com",
- "env-SERVER_PORT": "80",
- "env-SERVER_SOFTWARE": "go",
- }
- replay := runCgiTest(t, h, "GET /test.go?foo=bar&a=b HTTP/1.0\nHost: example.com\n\n", expectedMap)
-
- if expected, got := "text/html; charset=utf-8", replay.Header().Get("Content-Type"); got != expected {
- t.Errorf("got a Content-Type of %q; expected %q", got, expected)
- }
- if expected, got := "X-Test-Value", replay.Header().Get("X-Test-Header"); got != expected {
- t.Errorf("got a X-Test-Header of %q; expected %q", got, expected)
- }
-}
-
-// Test that a child handler only writing headers works.
-func TestChildOnlyHeaders(t *testing.T) {
- h := &Handler{
- Path: os.Args[0],
- Root: "/test.go",
- Args: []string{"-test.run=TestBeChildCGIProcess"},
- }
- expectedMap := map[string]string{
- "_body": "",
- }
- replay := runCgiTest(t, h, "GET /test.go?no-body=1 HTTP/1.0\nHost: example.com\n\n", expectedMap)
- if expected, got := "X-Test-Value", replay.Header().Get("X-Test-Header"); got != expected {
- t.Errorf("got a X-Test-Header of %q; expected %q", got, expected)
- }
-}
-
-// Note: not actually a test.
-func TestBeChildCGIProcess(t *testing.T) {
- if os.Getenv("REQUEST_METHOD") == "" {
- // Not in a CGI environment; skipping test.
- return
- }
- Serve(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
- rw.Header().Set("X-Test-Header", "X-Test-Value")
- req.ParseForm()
- if req.FormValue("no-body") == "1" {
- return
- }
- fmt.Fprintf(rw, "test=Hello CGI-in-CGI\n")
- for k, vv := range req.Form {
- for _, v := range vv {
- fmt.Fprintf(rw, "param-%s=%s\n", k, v)
- }
- }
- for _, kv := range os.Environ() {
- fmt.Fprintf(rw, "env-%s\n", kv)
- }
- }))
- os.Exit(0)
-}
diff --git a/gcc-4.8.1/libgo/go/net/http/cgi/testdata/test.cgi b/gcc-4.8.1/libgo/go/net/http/cgi/testdata/test.cgi
deleted file mode 100644
index 1b25bc299..000000000
--- a/gcc-4.8.1/libgo/go/net/http/cgi/testdata/test.cgi
+++ /dev/null
@@ -1,90 +0,0 @@
-#!/usr/bin/perl
-# Copyright 2011 The Go Authors. All rights reserved.
-# Use of this source code is governed by a BSD-style
-# license that can be found in the LICENSE file.
-#
-# Test script run as a child process under cgi_test.go
-
-use strict;
-use Cwd;
-
-binmode STDOUT;
-
-my $q = MiniCGI->new;
-my $params = $q->Vars;
-
-if ($params->{"loc"}) {
- print "Location: $params->{loc}\r\n\r\n";
- exit(0);
-}
-
-print "Content-Type: text/html\r\n";
-print "X-CGI-Pid: $$\r\n";
-print "X-Test-Header: X-Test-Value\r\n";
-print "\r\n";
-
-if ($params->{"bigresponse"}) {
- for (1..1024) {
- print "A" x 1024, "\r\n";
- }
- exit 0;
-}
-
-print "test=Hello CGI\r\n";
-
-foreach my $k (sort keys %$params) {
- print "param-$k=$params->{$k}\r\n";
-}
-
-foreach my $k (sort keys %ENV) {
- my $clean_env = $ENV{$k};
- $clean_env =~ s/[\n\r]//g;
- print "env-$k=$clean_env\r\n";
-}
-
-# NOTE: msys perl returns /c/go/src/... not C:\go\....
-my $dir = getcwd();
-if ($^O eq 'MSWin32' || $^O eq 'msys') {
- if ($dir =~ /^.:/) {
- $dir =~ s!/!\\!g;
- } else {
- my $cmd = $ENV{'COMSPEC'} || 'c:\\windows\\system32\\cmd.exe';
- $cmd =~ s!\\!/!g;
- $dir = `$cmd /c cd`;
- chomp $dir;
- }
-}
-print "cwd=$dir\r\n";
-
-# A minimal version of CGI.pm, for people without the perl-modules
-# package installed. (CGI.pm used to be part of the Perl core, but
-# some distros now bundle perl-base and perl-modules separately...)
-package MiniCGI;
-
-sub new {
- my $class = shift;
- return bless {}, $class;
-}
-
-sub Vars {
- my $self = shift;
- my $pairs;
- if ($ENV{CONTENT_LENGTH}) {
- $pairs = do { local $/; <STDIN> };
- } else {
- $pairs = $ENV{QUERY_STRING};
- }
- my $vars = {};
- foreach my $kv (split(/&/, $pairs)) {
- my ($k, $v) = split(/=/, $kv, 2);
- $vars->{_urldecode($k)} = _urldecode($v);
- }
- return $vars;
-}
-
-sub _urldecode {
- my $v = shift;
- $v =~ tr/+/ /;
- $v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
- return $v;
-}