Black Lives Matter. Support the Equal Justice Initiative.

Source file src/net/http/cgi/child.go

Documentation: net/http/cgi

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // This file implements CGI from the perspective of a child
     6  // process.
     7  
     8  package cgi
     9  
    10  import (
    11  	"bufio"
    12  	"crypto/tls"
    13  	"errors"
    14  	"fmt"
    15  	"io"
    16  	"io/ioutil"
    17  	"net"
    18  	"net/http"
    19  	"net/url"
    20  	"os"
    21  	"strconv"
    22  	"strings"
    23  )
    24  
    25  // Request returns the HTTP request as represented in the current
    26  // environment. This assumes the current program is being run
    27  // by a web server in a CGI environment.
    28  // The returned Request's Body is populated, if applicable.
    29  func Request() (*http.Request, error) {
    30  	r, err := RequestFromMap(envMap(os.Environ()))
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	if r.ContentLength > 0 {
    35  		r.Body = ioutil.NopCloser(io.LimitReader(os.Stdin, r.ContentLength))
    36  	}
    37  	return r, nil
    38  }
    39  
    40  func envMap(env []string) map[string]string {
    41  	m := make(map[string]string)
    42  	for _, kv := range env {
    43  		if idx := strings.Index(kv, "="); idx != -1 {
    44  			m[kv[:idx]] = kv[idx+1:]
    45  		}
    46  	}
    47  	return m
    48  }
    49  
    50  // RequestFromMap creates an http.Request from CGI variables.
    51  // The returned Request's Body field is not populated.
    52  func RequestFromMap(params map[string]string) (*http.Request, error) {
    53  	r := new(http.Request)
    54  	r.Method = params["REQUEST_METHOD"]
    55  	if r.Method == "" {
    56  		return nil, errors.New("cgi: no REQUEST_METHOD in environment")
    57  	}
    58  
    59  	r.Proto = params["SERVER_PROTOCOL"]
    60  	var ok bool
    61  	r.ProtoMajor, r.ProtoMinor, ok = http.ParseHTTPVersion(r.Proto)
    62  	if !ok {
    63  		return nil, errors.New("cgi: invalid SERVER_PROTOCOL version")
    64  	}
    65  
    66  	r.Close = true
    67  	r.Trailer = http.Header{}
    68  	r.Header = http.Header{}
    69  
    70  	r.Host = params["HTTP_HOST"]
    71  
    72  	if lenstr := params["CONTENT_LENGTH"]; lenstr != "" {
    73  		clen, err := strconv.ParseInt(lenstr, 10, 64)
    74  		if err != nil {
    75  			return nil, errors.New("cgi: bad CONTENT_LENGTH in environment: " + lenstr)
    76  		}
    77  		r.ContentLength = clen
    78  	}
    79  
    80  	if ct := params["CONTENT_TYPE"]; ct != "" {
    81  		r.Header.Set("Content-Type", ct)
    82  	}
    83  
    84  	// Copy "HTTP_FOO_BAR" variables to "Foo-Bar" Headers
    85  	for k, v := range params {
    86  		if !strings.HasPrefix(k, "HTTP_") || k == "HTTP_HOST" {
    87  			continue
    88  		}
    89  		r.Header.Add(strings.ReplaceAll(k[5:], "_", "-"), v)
    90  	}
    91  
    92  	uriStr := params["REQUEST_URI"]
    93  	if uriStr == "" {
    94  		// Fallback to SCRIPT_NAME, PATH_INFO and QUERY_STRING.
    95  		uriStr = params["SCRIPT_NAME"] + params["PATH_INFO"]
    96  		s := params["QUERY_STRING"]
    97  		if s != "" {
    98  			uriStr += "?" + s
    99  		}
   100  	}
   101  
   102  	// There's apparently a de-facto standard for this.
   103  	// https://web.archive.org/web/20170105004655/http://docstore.mik.ua/orelly/linux/cgi/ch03_02.htm#ch03-35636
   104  	if s := params["HTTPS"]; s == "on" || s == "ON" || s == "1" {
   105  		r.TLS = &tls.ConnectionState{HandshakeComplete: true}
   106  	}
   107  
   108  	if r.Host != "" {
   109  		// Hostname is provided, so we can reasonably construct a URL.
   110  		rawurl := r.Host + uriStr
   111  		if r.TLS == nil {
   112  			rawurl = "http://" + rawurl
   113  		} else {
   114  			rawurl = "https://" + rawurl
   115  		}
   116  		url, err := url.Parse(rawurl)
   117  		if err != nil {
   118  			return nil, errors.New("cgi: failed to parse host and REQUEST_URI into a URL: " + rawurl)
   119  		}
   120  		r.URL = url
   121  	}
   122  	// Fallback logic if we don't have a Host header or the URL
   123  	// failed to parse
   124  	if r.URL == nil {
   125  		url, err := url.Parse(uriStr)
   126  		if err != nil {
   127  			return nil, errors.New("cgi: failed to parse REQUEST_URI into a URL: " + uriStr)
   128  		}
   129  		r.URL = url
   130  	}
   131  
   132  	// Request.RemoteAddr has its port set by Go's standard http
   133  	// server, so we do here too.
   134  	remotePort, _ := strconv.Atoi(params["REMOTE_PORT"]) // zero if unset or invalid
   135  	r.RemoteAddr = net.JoinHostPort(params["REMOTE_ADDR"], strconv.Itoa(remotePort))
   136  
   137  	return r, nil
   138  }
   139  
   140  // Serve executes the provided Handler on the currently active CGI
   141  // request, if any. If there's no current CGI environment
   142  // an error is returned. The provided handler may be nil to use
   143  // http.DefaultServeMux.
   144  func Serve(handler http.Handler) error {
   145  	req, err := Request()
   146  	if err != nil {
   147  		return err
   148  	}
   149  	if handler == nil {
   150  		handler = http.DefaultServeMux
   151  	}
   152  	rw := &response{
   153  		req:    req,
   154  		header: make(http.Header),
   155  		bufw:   bufio.NewWriter(os.Stdout),
   156  	}
   157  	handler.ServeHTTP(rw, req)
   158  	rw.Write(nil) // make sure a response is sent
   159  	if err = rw.bufw.Flush(); err != nil {
   160  		return err
   161  	}
   162  	return nil
   163  }
   164  
   165  type response struct {
   166  	req            *http.Request
   167  	header         http.Header
   168  	code           int
   169  	wroteHeader    bool
   170  	wroteCGIHeader bool
   171  	bufw           *bufio.Writer
   172  }
   173  
   174  func (r *response) Flush() {
   175  	r.bufw.Flush()
   176  }
   177  
   178  func (r *response) Header() http.Header {
   179  	return r.header
   180  }
   181  
   182  func (r *response) Write(p []byte) (n int, err error) {
   183  	if !r.wroteHeader {
   184  		r.WriteHeader(http.StatusOK)
   185  	}
   186  	if !r.wroteCGIHeader {
   187  		r.writeCGIHeader(p)
   188  	}
   189  	return r.bufw.Write(p)
   190  }
   191  
   192  func (r *response) WriteHeader(code int) {
   193  	if r.wroteHeader {
   194  		// Note: explicitly using Stderr, as Stdout is our HTTP output.
   195  		fmt.Fprintf(os.Stderr, "CGI attempted to write header twice on request for %s", r.req.URL)
   196  		return
   197  	}
   198  	r.wroteHeader = true
   199  	r.code = code
   200  }
   201  
   202  // writeCGIHeader finalizes the header sent to the client and writes it to the output.
   203  // p is not written by writeHeader, but is the first chunk of the body
   204  // that will be written. It is sniffed for a Content-Type if none is
   205  // set explicitly.
   206  func (r *response) writeCGIHeader(p []byte) {
   207  	if r.wroteCGIHeader {
   208  		return
   209  	}
   210  	r.wroteCGIHeader = true
   211  	fmt.Fprintf(r.bufw, "Status: %d %s\r\n", r.code, http.StatusText(r.code))
   212  	if _, hasType := r.header["Content-Type"]; !hasType {
   213  		r.header.Set("Content-Type", http.DetectContentType(p))
   214  	}
   215  	r.header.Write(r.bufw)
   216  	r.bufw.WriteString("\r\n")
   217  	r.bufw.Flush()
   218  }
   219  

View as plain text